将变量传递到单独的 PHP 页面(图形绘制)


Passing Variables to a Separate PHP page (Graphics Draw)

我有一个图形绘制图像,我想在其中传入一个变量来表示图像的高度(矩形)。该变量是数组的一部分,该数组从数据库中的列中获取范围内的单位数。

我回显了变量$cred0,它在我的 HTML 页面中显示一个数字/值。我想将变量/值传递给另一个页面,该页面在 PHP 中绘制我的图形。截至目前,当我用数字设置高度时,图像显示正常:

define('IMAGE_WIDTH', 50);    
define('IMAGE_HEIGHT', 200);    

但是当我用变量替换高度的值时,图像不会输出:

define('IMAGE_WIDTH', 50);     
define('IMAGE_HEIGHT', $cred0);   

我猜变量没有正确传递到图形绘制.php页面。我尝试创建一个包含,但这没有显示正确的结果。关于如何将此变量传递到另一个页面并使用它来替换高度值的任何想法?

这是带有图形绘制 (bar_chart_image.php) 的页面的 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>

解决方案:首先生成 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);
?>

这是因为您可能会将$cred0作为字符串传递,请尝试define('IMAGE_HEIGHT', (int)$cred0);。请注意,我们在使用变量之前将其强制转换为标记器。