Can you provide your code and more details?
$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; }
imageline($im, 125, $array[0]*.5, 325, $array[0]*.5, $white);
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.
imageline($im, 125, $array[0]*.5, 325, $array[1]*.5, $white);
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.
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.
echo $array[$i];
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?
//$array
In the code you presented before you have
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.PHP://$array
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
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.
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.
$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++ }