Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
so i am having trouble making a line graph in php from data in a mysql database. can anyone help me out with this?

i can create an image with a line on it, but when i try to pull the data from the database, it only gives me the first row in the database
 
Can you provide your code and more details?

here is my code. there is more than that, but i wanted to show the main part of the image and database part. i didn't put the database connection on here, but it is in the code and works.

Code:
$result = mysql_query("SELECT elevation FROM lakedata");
	if (!$result) die('Could not query:' . mysql_error());
	
	//$array;
	$i=0;
	
	while($row = mysql_fetch_array($result))
	{
		$array[] = $row[$i];
		$i++;	
	}
	echo $array[1];

	// set up image
	$height = 800;
	$width = 1000;
	$im = imagecreatetruecolor($width, $height);
	$white = imagecolorallocate ($im, 255, 255, 255);
	$blue = imagecolorallocate ($im, 0, 0, 255);
	$red = imagecolorallocate ($im, 64, 0, 0);
	
	// draw on image
	imagefill($im, 0, 0, $blue);
	// y-axis
	imageline($im, 100, 0, 100, $height-100, $white);
	// x-axis
	imageline($im, 100, $height-100, $width, $height-100, $white);
	
	imageline($im, 125, $array[0]*.5, 325, $array[0]*.5, $white);
	imageline($im, 325, $array[0]*.5, 325, $array[0]*.5, $red);
	imagestring($im, 4, 50, 150, 'Sales', $white);
	
	// output image
	Header ('Content-type: image/png');
	imagepng ($im);
	
	//clean up
	imagedestroy($im);
	
	mysql_close($con);
 
Try this, unless you're expecting an array for each record. The $i wasn't needed. The while loop is already taking care of that.
PHP:
	while($row = mysql_fetch_array($result))
	{
		$array[] = $row;
	}

Edit: $i is needed, just not the $i++
 
Try this, unless you're expecting an array for each record. The $i wasn't needed. The while loop is already taking care of that.
PHP:
	while($row = mysql_fetch_array($result))
	{
		$array[] = $row;
	}

well, i get this error now: Fatal error: Unsupported operand types

i get this from this line:

Code:
imageline($im, 125, $array[0]*.5, 325, $array[0]*.5, $white);

what is the best way to call a number from the array?
 
That's my bad. The $i was fine, you simply didn't need to $i++ at the end of the loop.

PHP:
while($row = mysql_fetch_array($result))
    {
        $array[] = $row[$i];
    }

Though you may be better off accessing $row['elevation'] rather then $i.
 
That's my bad. The $i was fine, you simply didn't need to $i++ at the end of the loop.

PHP:
while($row = mysql_fetch_array($result))
    {
        $array[] = $row[$i];
    }

Though you may be better off accessing $row['elevation'] rather then $i.

thanks. but it still doesn't work right. the first imageline works:

Code:
imageline($im, 125, $array[0]*.5, 325, $array[1]*.5, $white);

but this one doesn't:

Code:
imageline($im, 325, $array[1]*.5, 325, $array[2]*.5, $white);

should i call $row['elevation']? basically, elevation is a column in a database. i want to plot each data point in that column on the image. draw a line from one point to the next. a line graph.

but i have to draw it to scale.
 
should i call $row['elevation']? basically, elevation is a column in a database. i want to plot each data point in that column on the image. draw a line from one point to the next. a line graph.

but i have to draw it to scale.

Right, using $row['elevation'] would access that column element. It's just a more straight forward way of accessing the right array index since sometimes your query results in multiple columns. Have tried printing out the contents of $array to make sure it's getting the values you believe are there? You could do a print statement in the while loop as it's going through the results. That would be the easiest nest step in troubleshooting.
 
Right, using $row['elevation'] would access that column element. It's just a more straight forward way of accessing the right array index since sometimes your query results in multiple columns. Have tried printing out the contents of $array to make sure it's getting the values you believe are there? You could do a print statement in the while loop as it's going through the results. That would be the easiest nest step in troubleshooting.

ok, when i print out the objects in the array like this:

Code:
echo $array[$i];

it prints all the data correctly. that is in the while loop.

but when i get outside the while loop, the data is gone except $array[0], but $array[1] has nothing.

any idea on what i'm doing wrong?
 
ok, when i print out the objects in the array like this:

Code:
echo $array[$i];

it prints all the data correctly. that is in the while loop.

but when i get outside the while loop, the data is gone except $array[0], but $array[1] has nothing.

any idea on what i'm doing wrong?

In the code you presented before you have
PHP:
//$array
which is commented out. If that variable is not defined elsewhere then the issue here is a scoping issuing, meaning that $array only last as long as that while loop since that's where it was defined. I saw that commented line before and meant to ask about it, but forgot until I went back to look at the code more.
 
In the code you presented before you have
PHP:
//$array
which is commented out. If that variable is not defined elsewhere then the issue here is a scoping issuing, meaning that $array only last as long as that while loop since that's where it was defined. I saw that commented line before and meant to ask about it, but forgot until I went back to look at the code more.

thanks. okay, i think it works now.

but i need to scale the numbers and then draw the lines. right now it's just a straight line on the screen. how can i scale the numbers to fit on the image correctly?
 
thanks. okay, i think it works now.

but i need to scale the numbers and then draw the lines. right now it's just a straight line on the screen. how can i scale the numbers to fit on the image correctly?

Glad that got at the source of the problem. Your new question, I don't really know what you're working with to give much advice. It'll likely be math related and I don't know your skills there. Below is a basic example.

image = 800 x 600 (pixels)
datapoint = 50 (max 100) x 10 (max 100)

Place datapoint at: (50/100)*800 x (10/100)*600
 
Glad that got at the source of the problem. Your new question, I don't really know what you're working with to give much advice. It'll likely be math related and I don't know your skills there. Below is a basic example.

image = 800 x 600 (pixels)
datapoint = 50 (max 100) x 10 (max 100)

Place datapoint at: (50/100)*800 x (10/100)*600

thanks! i feel like i'm getting somewhere!

my image is height = 800, width = 1000, but i drew a line for x-axis and y-axis, so we're only using 700 and 900, instead of 800 and 1000.

can i use max and min in php?
 
can i use max and min in php?

How are you wanting to use min and max? I don't know what your elevation data looks like (as in what data ranges exist) and how you want to place it on the image. Also not sure what the purpose is of placing the lines and whatnot on the image.
 
How are you wanting to use min and max? I don't know what your elevation data looks like (as in what data ranges exist) and how you want to place it on the image. Also not sure what the purpose is of placing the lines and whatnot on the image.

basically i want to graph the elevation as it changes. it doesn't change much, less than a foot. the range is like 1063.39 - 1065.83 for right now.
 
basically i want to graph the elevation as it changes. it doesn't change much, less than a foot. the range is like 1063.39 - 1065.83 for right now.

Here's some pseudo code to work from. Should be pretty easy to follow.
Code:
$imgX = 900
$imgY = 700
$min = 1060
$max = 1066
$range = $max - $min

$array(1060.5, 1061.1, 1061.8, 1063.0, 1064.9)
$len = count($array)

$i = 1
loop ($i) {
  // Coord is an x by y value in pixels
  $Coord = (($imgX / $len * $i), (($array[0] - $min) / $range * $imgX))
  $i++
}
 
Here's some pseudo code to work from. Should be pretty easy to follow.
Code:
$imgX = 900
$imgY = 700
$min = 1060
$max = 1066
$range = $max - $min

$array(1060.5, 1061.1, 1061.8, 1063.0, 1064.9)
$len = count($array)

$i = 1
loop ($i) {
  // Coord is an x by y value in pixels
  $Coord = (($imgX / $len * $i), (($array[0] - $min) / $range * $imgX))
  $i++
}

thanks! i'll see what i can do with it
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.