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

将变量传递到单独的PHP页面(图形绘制),php,function,Php,Function,我有一个图形绘制图像,我想在其中传递一个变量来表示图像的高度(一个矩形)。该变量是数组的一部分,该数组从数据库中的列中获取范围内的单位数 我回显了变量$cred0,它在我的HTML页面中显示了一个数字/值。我想将变量/值传递给另一个用PHP绘制图形的页面。现在,当我用数字设置高度时,图像显示良好: define('IMAGE_WIDTH', 50); define('IMAGE_HEIGHT', 200); 但当我用变量替换高度值时,图像不会输出: define('IMAGE_

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

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

define('IMAGE_WIDTH', 50);    
define('IMAGE_HEIGHT', 200);    
但当我用变量替换高度值时,图像不会输出:

define('IMAGE_WIDTH', 50);     
define('IMAGE_HEIGHT', $cred0);   
我猜变量没有正确地传递到graphic draw.php页面中。我已经尝试创建一个include,但是没有显示正确的结果。关于如何将此变量传递到另一个页面并使用它替换高度值,有什么想法吗

这是带有图形绘制的页面的PHP(bar\u chart\u image.PHP):


这是包含变量信息和值的页面(creditLimitTable.php):


家
#表11{
高度:100px;
}   

信用额度表 信用额度


这是因为您可能将
$cred0
作为字符串传递,请尝试
定义('IMAGE\u HEIGHT',(int)$cred0)。请注意,我们在使用变量之前将其强制转换为整数。

这是因为您可能将
$cred0
作为字符串传递,请尝试
define('IMAGE\u HEIGHT',(int)$cred0)。请注意,在使用该变量之前,我们将其强制转换为intager。

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

家
#表11{
高度:100px;
}   
“/>

信用额度表 信用额度

图像生成器页面:

<?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值,然后使用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>

家
#表11{
高度:100px;
}   
“/>

信用额度表 信用额度

图像生成器页面:

<?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);

?>



您能告诉我如何以及在何处将变量值发送到图形php代码吗?@anant kumar singh这是我尝试的,没有成功:define('IMAGE_HEIGHT',$cred0);我说的是你展示的两个php代码?它们是在同一个页面上,还是在不同的页面上?你是想在不同的页面上使用define两次吗?@anant kumar singh它们在两个不同的页面上(我在上面的代码中添加了页面名称,如果这有助于其他人看到它们是两个不同的页面)您能告诉我如何以及在何处将变量值发送到图形php代码中吗?@anant kumar singh这是我尝试的,没有成功:define('IMAGE_HEIGHT',$cred0);我说的是你展示的两个php代码?它们是在同一个页面上,还是在不同的页面上?你是想在不同的页面上使用define两次吗?@anant kumar singh它们在两个不同的页面上(我在上面的代码中添加了页面名称,如果这有助于其他人看到它们是两个不同的页面)您能告诉我如何以及在何处将变量值发送到图形php代码中吗?@anant kumar singh这是我尝试的,没有成功:define('IMAGE_HEIGHT',$cred0);我说的是你展示的两个php代码?它们是在同一个页面上,还是在不同的页面上?你是想在不同的页面上使用define两次吗?@anant kumar singh它们在两个不同的页面上(我在上面的代码中添加了页面名称,如果这有助于其他人看到它们是两个不同的页面)您可以理解@kumar在寻找什么。他需要向php图像生成器文件发送一个参数,该参数的值必须在img标记回音之前生成,否则该参数将始终为空。我没有考虑这个问题,我认为这是关于包含脚本而不是uri访问。感谢他的作品。我取出了
$data=”“
是的,但是您需要$data部分来生成表格的行,您可以理解@kumar在寻找什么。他需要向php图像生成器文件发送一个参数,该参数的值必须在img标记的回显之前生成,否则该参数将始终为空。我没有考虑这个问题,我认为这是关于包含脚本而不是uri访问。感谢他的作品。我取出了
$data=”“
是的,但是您需要$data部分来生成表格的行,您可以理解@kumar在寻找什么。他需要向php图像生成器文件发送一个参数,该参数的值必须在img标记的回显之前生成,否则该参数将始终为空。我没有考虑这个问题,我认为这是关于包含脚本而不是uri访问。感谢他的作品。我拿出
$data=”“