dtny30176 2015-06-11 18:47
浏览 82
已采纳

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

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>
  • 写回答

2条回答 默认 最新

  • dsutuyxe088689 2015-06-11 18:59
    关注

    Solution: generate first the cred0 value and then call the image with a parameter of cred0:

    Display page:

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

    Image generator page:

    <?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);
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 速帮,学校需要在外上班没空
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义