Page 1 of 1

$_GET Not working as expected: Undefined index: image_id

Posted: Mon Nov 17, 2008 11:58 pm
by ExpertAlmost
I have read several docs concerning $_Get. My understanding is that it is a global. But I get a notice message: Notice: Undefined index: image_id in C:\AppServ\www\MM_Main.php on line 94 when using it. Though my images appear just fine. Why am I getting the messages and how do I get rid of them?

My code with the image_id's (index.php)

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My PHP Catastrophe</title>
</head>
<body>
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL); 
require("MM_Main.php");
show_calcs($SlowAry,"Slow Array", $Symbol, $SlowMaxBar);
?>
<p>
<img src="MM_Main.php?image_id=1" border="0">
<img src="MM_Main.php?image_id=2" border="0">
<img src="MM_Main.php?image_id=3" border="0">
<img src="MM_Main.php?image_id=4" border="0">
</p>
</body>
</html>
My the end of my code in my main php program, main.php, (which calls other functions to open files reading data, do calculations, creates my four images.)

Code: Select all

//determine which data from the array to use
get_context($FastBar, $SlowAry, $MedmAry, $FastAry, $SlowMaxBar, $MedmMaxBar, $FastMaxBar, 
    $SymbLTF, $SymbTbl, $MedmParentBar, $SlowParentBar);  
//create the image using the data, returns an image resource
$CrwdMvImg4 = display_CrwdMv($SlowAry, $MedmAry, $FastAry, $SlowParentBar, $MedmParentBar, $FastBar);
//display my four images
header("Content-Type: image/png");
print "Image ID={$_GET["image_id"]}<br/>";   //this line appears 1 time and with no value
if ($_GET["image_id"] == 1) imagepng($CrwdMvImg1); 
else if ($_GET["image_id"] == 2) imagepng($CrwdMvImg2);
else if ($_GET["image_id"] == 3) imagepng($CrwdMvImg3);
else if ($_GET["image_id"] == 4) imagepng($CrwdMvImg4);
?>
Shouldn't the print statement show me the image id and shouldn't it appear four times? I only appears once! I get four images...Also, if I remove the "require main.php" line, I do not get the message and my images appear fine. But if I try to also output text from anywhere, the images become blank boxes.

Any assistance or insight would be greatly appreaciated! Thank you :)

Re: $_GET Not working as expected: Undefined index: image_id

Posted: Tue Nov 18, 2008 2:30 am
by novice4eva
Whenever you set header, the content should correspond to the header. In your code you have set the header to "image/png", now the browser treats whatever output from that page to be image but in your case you have echoed some string which is not image, nor is it the content of image so its invalid!! If you want to show the image id do it in the index.php. Besides when putting header, you should not echo any content prior to calling header, not even comments are forgiven. Also please be specific:
Also, if I remove the "require main.php" line
but it is MM_Main.php, those would be two different pages wouldn't it??

Re: $_GET Not working as expected: Undefined index: image_id

Posted: Tue Nov 18, 2008 2:41 am
by novice4eva
Umm regarding the main question about GET parameter. Well expand the line

Code: Select all

require("MM_Main.php");

with actual code, since your index.php must not have any get parameter set specially the image_id so when the execution comes to this line

Code: Select all

if ($_GET["image_id"] == 1)
obviously $_GET["image_id"] is not set, to make the point just try this URL index.php?image_id=1, it should avoid your specific $_GET warning but then you will get other warnings for sure.
Having said that, i would recommend you to not include "MM_Main.php", let the page do what it is supposed to do: return an image. :D

Re: $_GET Not working as expected: Undefined index: image_id

Posted: Tue Nov 18, 2008 9:54 am
by ExpertAlmost
Thank you for the help! I think I have some conceptual confusion about the idea of "page." I am very new to PHP and very very new to HTLM. My background is in C.

I removed the print statement prior to the header setting. That helped! :)

The reason I have "require MM_Main.php" in index.php (line 12) is because if I do not, I cannot output the text data I want to see using the "show_symbtbl($SymbTbl);" function call (line 13). I get a "Fatal error: Call to undefined function show_symbtbl() in C:\AppServ\www\index.php..." But if I comment out the "require MM_Main.php" in index.php, I do not get the notices. But then also no text.

My goal at this point is to output various data and debug print statements calculated in MM_Main.php (and other php files containing functions required by MM_Main.php). THEN once the text is output to my webpage, to display my graphics on the same webpage. I see webpages with text and images and I am trying to do the same.

Why, even with the notices, are my four images even displaying when image_id is not set? How do I get text and my images to output to the same webpage?

Thank you for sharing your experience and time! :bow:

My index.php file:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My PHP Catastrophe</title>
</head>
<body>
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL); 
 
require("MM_Main.php");  // cut this & next line, no warnings, graphics ok. But fatal error error if try to output text data
show_symbtbl($SymbTbl);  // function to output text data, from array. function needs MM_Main.php
?>
 
<p>
<img src="MM_Main.php?image_id=1" border="0">
<img src="MM_Main.php?image_id=2" border="0">
<img src="MM_Main.php?image_id=3" border="0">
<img src="MM_Main.php?image_id=4" border="0">
</p>
</body>
</html>
 
code from MM_Main.php, my main program for file handling, reading, data calculations, image building. The image_id code is at the end. The four images are in the image resources: $CrwdMvImg1...$CrwdMvImg4

Code: Select all

<?php
# This is the main module for all Market Mirror PHP code. 
ini_set('display_errors', 1);
error_reporting(E_ALL); 
 
# All required PHP files for Market Mirror
require("MM_Define.php");
require("MM_Matrix.php");
require("MM_Data.php");
require("MM_Calc.php");
require("MM_Display.php");
//require("MM_Output.php");
 
$Symbol = "USDJPY";
$LeadTF = "10MIN";
$SymbLTF = $Symbol."_".$LeadTF;
$SymbTbl = array(); $SlowAry = array(); $MedmAry = array(); $FastAry = array();
load_symbtbl($SymbTbl);
$SlowMaxBar = get_data($SlowAry, "SLOW", $Symbol, $SymbTbl, $SymbLTF);
$MedmMaxBar = get_data($MedmAry, "MEDM", $Symbol, $SymbTbl, $SymbLTF);
$FastMaxBar = get_data($FastAry, "FAST", $Symbol, $SymbTbl, $SymbLTF);
$AllMaxBar = max($SlowMaxBar, $MedmMaxBar, $FastMaxBar);
// Cycle through the rows of each array for all calcs
for ($Row = BAR1; $Row <= $AllMaxBar; $Row++) {
    // Calc slow array values
    if ($Row <= $SlowMaxBar) {
        calc_TR($SlowAry, $Row, $Prec);
        calc_RR($SlowAry, $Row, $Prec);
        calc_CrwdMv($SlowAry, $Row, $Prec);
        calc_CMPlot($SlowAry, $Row);
    }
}
//Using a given FAST bar number, get the two parents barnumber
$FastBar = 2000;
get_context($FastBar, $SlowAry, $MedmAry, $FastAry, $SlowMaxBar, $MedmMaxBar, $FastMaxBar, 
    $SymbLTF, $SymbTbl, $MedmParentBar, $SlowParentBar);
$CrwdMvImg1 = display_CrwdMv($SlowAry, $MedmAry, $FastAry, $SlowParentBar, $MedmParentBar, $FastBar);
$FastBar = 2200;
get_context($FastBar, $SlowAry, $MedmAry, $FastAry, $SlowMaxBar, $MedmMaxBar, $FastMaxBar, 
    $SymbLTF, $SymbTbl, $MedmParentBar, $SlowParentBar);
$CrwdMvImg2 = display_CrwdMv($SlowAry, $MedmAry, $FastAry, $SlowParentBar, $MedmParentBar, $FastBar);
$FastBar = 2400;
get_context($FastBar, $SlowAry, $MedmAry, $FastAry, $SlowMaxBar, $MedmMaxBar, $FastMaxBar, 
    $SymbLTF, $SymbTbl, $MedmParentBar, $SlowParentBar);
$CrwdMvImg3 = display_CrwdMv($SlowAry, $MedmAry, $FastAry, $SlowParentBar, $MedmParentBar, $FastBar);
$FastBar = 2600;
get_context($FastBar, $SlowAry, $MedmAry, $FastAry, $SlowMaxBar, $MedmMaxBar, $FastMaxBar, 
    $SymbLTF, $SymbTbl, $MedmParentBar, $SlowParentBar);
$CrwdMvImg4 = display_CrwdMv($SlowAry, $MedmAry, $FastAry, $SlowParentBar, $MedmParentBar, $FastBar);
 
header("Content-Type: image/png");
if ($_GET["image_id"] == 1) imagepng($CrwdMvImg1); 
else if ($_GET["image_id"] == 2) imagepng($CrwdMvImg2);
else if ($_GET["image_id"] == 3) imagepng($CrwdMvImg3);
else if ($_GET["image_id"] == 4) imagepng($CrwdMvImg4);
?>
 

Re: $_GET Not working as expected: Undefined index: image_id

Posted: Tue Nov 18, 2008 10:05 pm
by novice4eva
The function that you require : load_symbtbl() must be in one of these

Code: Select all

 
require("MM_Define.php");
require("MM_Matrix.php");
require("MM_Data.php");
require("MM_Calc.php");
require("MM_Display.php");
 
so in your index page you can include just the php file which has this function(and obviate using MM_Main.php). And its a personal taste, but i prefer require_once/include_once, you can search for the difference in the web.
As for displaying text, i got the hint that you wanted to display the image IDs, this is simple:

Code: Select all

 
<?php
$images = array(1,2,3,4);
foreach($images as $val)
{
echo '<div>
<div>Image Id is : '.$val.'</div>
<div><img src="MM_Main.php?image_id='.$val.'" border="0"></div>
</div>';
}
?>
 
Finally i would suggest not putting

Code: Select all

 
ini_set('display_errors', 1);
error_reporting(E_ALL); 
 
in the MM_Main.php because well it even pops out warnings(warnings are not fatal errors) and the images might not get displayed, but off course turn them off when all the errors have been tackled :D

Re: $_GET Not working as expected: Undefined index: image_id

Posted: Tue Nov 18, 2008 10:33 pm
by ExpertAlmost
FANTASTIC!!!

That took care of it. Thank you so much novice4eva :bow:

As your stated location is Nepal, I would be inclined to say your merit has certainly increased with your generous offering of time and effort. :teach:

Much appreciated. :)