Ravens PHP Scripts: Forums
 

 

View next topic
View previous topic
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> PHP
Author Message
Dawg
RavenNuke(tm) Development Team



Joined: Nov 07, 2003
Posts: 928

PostPosted: Sat Aug 20, 2005 2:13 pm Reply with quote

Greetings All,
How to I store the results from a query to a NUM array?

I can go to the database (mysql) and get the data I want.

I can print that data.

What I really need is the data in an array.

[php]$query = "SELECT Time, Height FROM `table_name` where date = DATE_FORMAT(CURRENT_DATE(), '%c/%d/%Y')";


$result = mysql_query($query) or die('Query failed: ' . mysql_error());

// Printing results in HTML
echo "<table>\n";


while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
printf("%s, %s <br>", $row[0], $row[1]); [/php]

That prints my data JUST like I need it....
00:00:00, 0.86
02:00:00, -0.75
04:00:00, 0.04
06:00:00, 2.58
08:00:00, 4.3
10:00:00, 3.36
12:00:00, 0.82
14:00:00, -0.98
16:00:00, -0.4
18:00:00, 2.31
20:00:00, 4.63
22:00:00, 4.31


But I need it in an array. Lile this....
[php]
$example_data = array(
array( "0:00",3.03),
array( "2:00",1.42),
array( "4:00",-1.11),
array( "6:00",0.52),
array( "8:00",2.17),
array( "10:00",3.34),
array( "12:00",3.12),
array( "14:00",1.65),
array( "16:00",0.22),
array( "20:00",1.91),
[/php]

How do I do that?

Thank You for your time!

Dawg
 
View user's profile Send private message
Raven
Site Admin/Owner



Joined: Aug 27, 2002
Posts: 17088

PostPosted: Sat Aug 20, 2005 3:01 pm Reply with quote

I don't fully understand what you are after, but you might look at serialization http://us3.php.net/manual/en/function.serialize.php
 
View user's profile Send private message
Dawg







PostPosted: Sat Aug 20, 2005 3:30 pm Reply with quote

Ok...Let me explain...

I am creating a graph....

This graph has to have it values like this....(I am using PhPlot to crate the graphs)

$example_data = array(
array( "0:00",3.03),
array( "2:00",1.42),
array( "4:00",-1.11),
array( "6:00",0.52),
array( "8:00",2.17),
array( "10:00",3.34),
array( "12:00",3.12),
array( "14:00",1.65),
array( "16:00",0.22),
array( "20:00",1.91),

I have the data in a MySQL database......that has a lot of other data with it.
ID...Date....Time...Height....Sunrise....Sunset....Twilight....So On....
There are 12 entries per date....1 entry for every 2 hours....Time and Height are the only ones that really change. All the rest are stay the same for that date.

I wrote this

$query = "SELECT Time, Height FROM `table_name` where date = DATE_FORMAT(CURRENT_DATE(), '%c/%d/%Y')";

to get the right data from the database. It more or less looks for todays date and retrives all the data for that date. It gets all 12 entries.
Time....Height
00:00:00, 0.86
02:00:00, -0.75
04:00:00, 0.04
06:00:00, 2.58
08:00:00, 4.3
10:00:00, 3.36
12:00:00, 0.82
14:00:00, -0.98
16:00:00, -0.4
18:00:00, 2.31
20:00:00, 4.63
22:00:00, 4.31

The full script in the post above will also print that data.....but I just did that to make sure I was getting the correct data.

PhPlot pulls it's data from an array to make the graph. What I am tring to do is get the returned data from the database in an array to create the graph.

Here is a link to PhPlot
http://www.phplot.com/

This will let you play around with it...
http://www.phplot.com/new/

I looked at serialize briefly but that does not seem to be what I am looking for....but I will go look again after I finish posting this.

Thank You for the help!
Dawg
 
Raven







PostPosted: Sat Aug 20, 2005 3:38 pm Reply with quote

The serialization suggestion was because I thought you wanted a way to store it in the database as 1 value. Obviously that's not what you're after.
 
montego
Site Admin



Joined: Aug 29, 2004
Posts: 9457
Location: Arizona

PostPosted: Sat Aug 20, 2005 6:51 pm Reply with quote

I'd check out this link for what you need:

http://www.php.net/manual/en/ref.array.php

_________________
Only registered users can see links on this board! Get registered or login!
Only registered users can see links on this board! Get registered or login! 
View user's profile Send private message Visit poster's website
Dawg







PostPosted: Sat Aug 20, 2005 10:54 pm Reply with quote

montego,
Thanks for the thought but I have read each of those about twice and some of them 4 or 5 times. I am thinking array_push is the way to go but.......I will think about it tomorrow....


Dawg
 
Raven







PostPosted: Sat Aug 20, 2005 11:27 pm Reply with quote

So are you saying that these values
Code:
00:00:00, 0.86

02:00:00, -0.75
04:00:00, 0.04
06:00:00, 2.58
08:00:00, 4.3
10:00:00, 3.36
12:00:00, 0.82
14:00:00, -0.98
16:00:00, -0.4
18:00:00, 2.31
20:00:00, 4.63
22:00:00, 4.31
need to be written as
Code:
$example_data = array(array("00:00:00", 0.86),array("02:00:00", -0.75),array("04:00:00", 0.04)); //and so on
 
Dawg







PostPosted: Sun Aug 21, 2005 5:49 am Reply with quote

Raven.....That is where I am stuck. There are 2 sides to the puzzle. The data and the graph.

So far on the data side....

I have the data in the database....

I know how to get the data out for the current day....

On the graph side....

I know how make a graph.....

but I don't know how to connect the two together.

The big question is how to get the data from the data base and into a format phplot can use. In the examples that I have they all use this structure...

$example_data = array(
array( "0:00",3.03),
array( "2:00",1.42),
array( "4:00",-1.11),
array( "6:00",0.52),
array( "8:00",2.17),
array( "10:00",3.34),
array( "12:00",3.12),
array( "14:00",1.65),
array( "16:00",0.22),
array( "20:00",1.91),

Sooooo....To awnser your question.......


Yes I need them

$example_data = array(array("00:00:00", 0.86),array("02:00:00", -0.75),array("04:00:00", 0.04)); //and so on

but they have to come from the database and into that format.....


I love this stuff. Figuring out a new puzzle all the time...I may not be anygood at coding (yet)....but it is a hell of a lot of fun trying to figure it all out! and I will figure it out....it may take some help here and there....but I guess we all have to learn some how.

Dawg
 
montego







PostPosted: Sun Aug 21, 2005 8:10 am Reply with quote

Dawg,

If this is a Nuke module you are writing, then something like this might work:

Code:



$example_data = array();

$query = "SELECT Time, Height FROM `table_name` where date = DATE_FORMAT(CURRENT_DATE(), '%c/%d/%Y')";

$result = $db->sql_query($query);

$i = 0;
while ( list($time, $height) = $db->sql_fetchrow($result) ) {

     $example_data[$i++][$time] = $height;

}



Now, I have not tested this, so don't know for certain that I have it just right, but what you could do is do a print_r($example_data); afterwards and see if it looks right.

Regards,
montego
 
Dawg







PostPosted: Sun Aug 21, 2005 12:13 pm Reply with quote

WOOT WOOT.....and the solution was....

while ( $row = mysql_fetch_array($result) ) {
$new_row = array($row[0],$row[1]);
$example_data[] = $new_row;
}

I about jumped out of my seat when it worked!!

Montego, Raven, Thank You both for helping...sometime just someone to discuss it with goes a really really long ways!!!

Thank You again...!

Dawg
 
montego







PostPosted: Sun Aug 21, 2005 9:42 pm Reply with quote

Dance-Y
 
Display posts from previous:       
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> PHP

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2007 phpBB Group
All times are GMT - 6 Hours
 
Forums ©