Hi there!
I have a problem when using $_POST variables in the following bar-graph. It seems as it cannot take values form a form, use them and then apear the graph in the resulting page.
The only thing I take is the page with the
--header
--the image's(bar-graph) borders
--footer
My code, if that can help is:
<?php include("header.php"); php?>
<?php
include("library.inc");

if ($_POST!='All') {
echo"Category: ";
category($_POST);
} else {
echo"The..........";
}
?>
<IMG SRC="b.php" HEIGHT="250" WIDTH="480">
<?php include("footer.php"); php?>

This is the b.php file:
<?php
include("library.inc");
dbconnection();
if ($_POST!='All') {
$column_name=$_POST;
$query="SELECT doc_ex.year,count(*)
FROM document AS doc_ex,citation, document AS doc_in
WHERE doc_ex.year between 1996 AND 2005
AND citation.lab_doc_no=doc_in.doc_no AND doc_in.category='$column_name'
GROUP BY doc_ex.year";
} else {
$query="SELECT doc_ex.year,count(*)
FROM document AS doc_ex,citation, document AS doc_in
WHERE doc_ex.year between 1996 AND 2005
AND citation.lab_doc_no=doc_in.doc_no
GROUP BY doc_ex.year";
}
$result = mysql_query($query) or die("Error in database interaction<BR>". mysql_error());
$strData='$data = array(';
while ($row = mysql_fetch_row($result)) {
$strData.="\"" .$row[0] . "\"" .'=> ' . $row[1] . ',';
}
$strData .=');';
eval($strData);
// create image
$width = 480;
$height = 250;
$image = imagecreate($width, $height);

// colors
$white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$navy = imagecolorallocate($image, 0x00, 0x00, 0x80);
$black = imagecolorallocate($image, 0x00, 0x00, 0x00);
$gray = imagecolorallocate($image, 0xC0, 0xC0, 0xC0);

// layout
$maxval = max($data);
$nval = sizeof($data);

$vmargin = 20; // top (bottom) vertical margin for title (x-labels)
$hmargin = 38; // left horizontal margin for y-labels

$base = floor(($width - $hmargin) / $nval); // distance between columns

$ysize = $height - 2 * $vmargin; // y-size of plot
$xsize = $nval * $base; // x-size of plot

// y labels and grid lines
$labelfont = 2;
$ngrid = $maxval; // number of grid lines ******$maxval/2

$dydat = ($maxval) / $ngrid; // data units between grid lines
$dypix = $ysize / ($ngrid+1 ); // pixels between grid lines

for ($i = 0; $i <= ($ngrid + 1); $i++) {
// iterate over y ticks

// height of grid line in units of data
$ydat = (int)($i * $dydat);

// height of grid line in pixels
$ypos = $vmargin + $ysize - (int)($i*$dypix);

$txtsz = imagefontwidth($labelfont) * strlen($ydat); // pixel-width of label
$txtht = imagefontheight($labelfont); // pixel-height of label

$xpos = (int)(($hmargin - $txtsz) / 2);
$xpos = max(1, $xpos);

imagestring($image, $labelfont, $xpos,
$ypos - (int)($txtht/2), $ydat, $black);

if (!($i == 0) && !($i > $ngrid)) {
imageline($image, $hmargin - 3,
$ypos, $hmargin + $xsize, $ypos, $gray);
// don't draw at Y=0 and top
}
}

// columns and x labels
$padding = 3; // half of spacing between columns
$yscale = $ysize / (($ngrid+1) * $dydat); // pixels per data unit

for ($i = 0; list($xval, $yval) = each($data); $i++) {

// vertical columns
$ymax = $vmargin + $ysize;
$ymin = $ymax - (int)($yval*$yscale);
$xmax = $hmargin + ($i+1)*$base - $padding;
$xmin = $hmargin + $i*$base + $padding;

imagefilledrectangle($image, $xmin, $ymin, $xmax, $ymax, $navy);

// x labels
$txtsz = imagefontwidth($labelfont) * strlen($xval);

$xpos = $xmin + (int)(($base - $txtsz) / 2);
$xpos = max($xmin, $xpos);
$ypos = $ymax + 3; // distance from x axis

imagestring($image, $labelfont, $xpos, $ypos, $xval, $black);
}

// plot frame
imagerectangle($image, $hmargin, $vmargin,
$hmargin + $xsize, $vmargin + $ysize, $black);

// flush image
header("Content-type: image/gif");
imagegif($image);
imagedestroy($image);
?>
Thanks! Cali.

Recommended Answers

All 2 Replies

I know why you're complaining about your POST variables not working, heh!

NEVER do and IfThen Statement with ONE equals sign. This will always evaluate the statement to be true. Do this instead:

<?php
if ($_POST['COL'] !== 'All') {
};
?>

Heh, hope it works now! =)

Thank you for the suggestion. You must be right!
I tried that but unfortunatelly it did not work!
I also tried not to use the IMG tag but make one file that contains the whole code. The problem is that the code which must show the graph does not accept any other code before it!
I had no problem when using GD library and show the results as a poll. BUT I must use a bar graph!
Any ideas?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.