I have a graphics draw image in which I would like to pass in a variable to represent the height of my image (a rectangle). The variable is part of an array that grabs the number of units in a range from a column in my database.
I echoed the variable $cred0
and it is displaying a number/value in my HTML page. I want to pass the variable/value to another page which draws my graphic in PHP. As of now, the image is showing up fine when I set the height with a number:
define('IMAGE_WIDTH', 50);
define('IMAGE_HEIGHT', 200);
But when I replace the value of the height with the variable, the image does not output:
define('IMAGE_WIDTH', 50);
define('IMAGE_HEIGHT', $cred0);
I am guessing the variable is not being passed into the the graphic draw .php page correctly. I have tried creating an include but that does not display the proper results. Any ideas on how I can pass this variable into another page and use it to replace the value of the height?
This is the PHP for the page with the graphic draw (bar_chart_image.php):
<?php
define('IMAGE_WIDTH', 50); // width of image
define('IMAGE_HEIGHT', 200); // height of image
// Create the image
$img = imagecreatetruecolor(IMAGE_WIDTH, IMAGE_HEIGHT);
// Background colors
$background_color = imagecolorallocate($img, 255, 224, 224); // pink
$font_color = imagecolorallocate($img, 117, 109, 109); // gray
$line_color = imagecolorallocate($img, 42, 143, 193); // blue
$pixel_color = imagecolorallocate($img, 0, 0, 0); // black
// Fill the background
imagefilledrectangle($img, 0, 0, IMAGE_WIDTH, IMAGE_HEIGHT, $background_color);
// Lines
for ($i = 0; $i < 10; $i++) {
imageline($img, 0, rand() % IMAGE_WIDTH, IMAGE_WIDTH, rand() % IMAGE_HEIGHT, $line_color);
}
// Random dots
for($i = 0; $i < 100; $i++) {
imagesetpixel($img, rand() % IMAGE_WIDTH, rand() % IMAGE_HEIGHT, $pixel_color);
}
// Output the image as a PNG using a header
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);
?>
This is the page that contains the variable information and values (creditLimitTable.php):
<?php
require_once("./includes/database_connection.php");
error_reporting(E_ALL);
ini_set('display_errors', 1);
// VARIABLES FOR CREDIT LIMIT RANGES
$cred0 = '';
$cred1_50000 = '';
$cred50001_75000 = '';
$cred75001_100000 ='';
$cred_100000 = '';
// QUERY TO GET DATA FROM CREDIT LIMIT COLUMN
$credit_limit = 'SELECT creditLimit FROM customers ORDER BY customerNumber ASC';
$result = mysqli_query($dbc, $credit_limit)
or die ('Error querying database');
?>
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Home</title>
<link type="text/css" rel="stylesheet" href="classic_cars.css" />
<style>
#table11 {
height: 100px;
}
</style>
</head>
<body>
<p><img src="bar_chart_image.php" /></p>
<?php
require_once("./includes/navigation.php");
?>
<h1>Credit Limit Table</h1>
<div id="table11">
<table border= "1">
<tr>
<td>Credit Limit</td>
</tr>
<?php
while($row = mysqli_fetch_array($result)) {
$creditLimit = $row['creditLimit'];
// SHOW COLUMN WITH DATA
echo "<tr>
<td>$creditLimit</td>
</tr>";
// ++ INCREMENT INTO ARRAY IF VALUE IN COLUMN IS WITHIN CERTAIN RANGE
if($creditLimit == 0) {
$cred0++;
}
if($creditLimit >= 1 && $creditLimit <= 50000) {
$cred1_50000++;
}
if($creditLimit>= 50001 && $creditLimit <= 75000) {
$cred50001_75000++;
}
if($creditLimit >= 75001 && $creditLimit <= 100000) {
$cred75001_100000++;
}
if($creditLimit > 100000) {
$cred_100000++;
}
// ARRAY
$credit_data = array(
array('0', $cred0),
array('1 to 50,000', $cred1_50000),
array('50,001 to 75,000', $cred50001_75000),
array('75,001 to 100,000', $cred75001_100000),
array('100,000', $cred_100000)
);
} // end while loop
?>
<!-- DISPLAY HOW MANY TIMES A NUMBER IN SPECIFIED RANGE SHOWS UP -->
<p><?php echo $cred0; ?></p>
<p><?php echo $cred1_50000; ?></p>
<p><?php echo $cred50001_75000; ?></p>
<p><?php echo $cred75001_100000; ?></p>
<p><?php echo $cred_100000; ?></p>
</table>
<?php
require_once("./includes/footer.php");
mysqli_close($dbc);
?>
</body>
</html>