我有一个图形绘制图像,其中我想传入一个变量来表示我的图像的高度(一个矩形)。该变量是一个数组的一部分,该数组可以获取数据库中某列范围内的单位数。
我回显了变量$cred0
,它在我的HTML页面中显示了一个数字/值。我想将变量/值传递给另一个用PHP绘制图形的页面。截至目前,当我用数字设置高度时,图像显示正常:
define('IMAGE_WIDTH', 50);
define('IMAGE_HEIGHT', 200);
但是当我用变量替换高度值时,图像不会输出:
define('IMAGE_WIDTH', 50);
define('IMAGE_HEIGHT', $cred0);
我猜测变量没有正确传递到图形绘制.php页面。我尝试过创建一个include但是没有显示正确的结果。关于如何将此变量传递到另一个页面并使用它来替换高度值的任何想法?
这是带有图形绘制的页面的PHP(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);
?>
这是包含变量信息和值的页面(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>
答案 0 :(得分:0)
那是因为你可能会将$cred0
作为字符串传递,请尝试define('IMAGE_HEIGHT', (int)$cred0);
。请注意,我们在使用它之前将变量转换为intager。
答案 1 :(得分:0)
解决方案:首先生成cred0值,然后使用参数cred0:
调用图像显示页面:
<?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>
<?php
$data = "";
while($row = mysqli_fetch_array($result)) {
$creditLimit = $row['creditLimit'];
// SHOW COLUMN WITH DATA
$data .="<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
?>
<p><img src="bar_chart_image.php?cred0=<?php echo $cred0; ?>" /></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 echo $data; ?>
<!-- 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>
图像生成器页面:
<?php
define('IMAGE_WIDTH', 50); // width of image
define('IMAGE_HEIGHT', $_GET['cred0']); // 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);
?>