2015-06-11 2 views
1

У меня есть графическое изображение, в котором я хотел бы передать переменную, чтобы представить высоту моего изображения (прямоугольник). Переменная является частью массива, который захватывает количество единиц в диапазоне от столбца в моей базе данных.Передача переменных на отдельную страницу PHP (Graphics Draw)

Я повторил переменную $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

Можете ли вы сказать, как и где вы отправляете значение переменной в свой графический php-код? –

+0

@anant kumar singh это то, что я пытался, не работал: define ('IMAGE_HEIGHT', $ cred0); –

+0

Я говорю о ваших двух php-кодах, которые вы показали? они находятся на одной странице или на другой странице? –

ответ

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

?> 
+0

Вы могли бы понять, что ищет @kumar. –

+0

ему нужно отправить параметр в файл генератора изображений php, значение этого параметра должно быть сгенерировано до эха тега img, иначе параметр всегда будет пустым –

+0

Я не рассматривал вопрос, я думал, что это о включая сценарии, а не доступ к uri. спасибо –

0

Это значит, что вы можете передать $cred0 в виде строки, попробуйте define('IMAGE_HEIGHT', (int)$cred0);. Обратите внимание, что перед тем, как использовать эту переменную, мы передаем ее переменной.

+0

хорошая точка - но я пробовал это на графической странице рисования, и это не имело значения. –

Смежные вопросы