Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fighting an "Undefined Variable"

Status
Not open for further replies.

webdev007

Programmer
Sep 9, 2005
168
This does well the job
but I may not find why it generates an "Undefined var" error?

$result_final .= "<img src='".$thumb_dir. "/tb_".$new_pic."' /($counter)> File Added<p>";

thank you
 
There are several reasons why you could be getting the "Undefined" message:

[ul]
[li]You never created the variable.[/li]
[li]You created the variable in a statment ( if(){} ) and did not create the variable outside of that scope.[/li]
[li]You created the variable in a different script in a different session (you could use sessions to remedy this issue).[/li]
[li]You created the variable in a different script but did not declare it as a global.[/li]
[li]You created the variable as a global but are using the variable in a function (did not do global $VARNAME again).[/li]
[/ul]

The list can go on. If those are not the issues, please post the code relevant to this issue.

 
Thanks,
Yes I already looked for similar possibilities
but disregarded that I used the var as $result_final.
(with a DOT)
so you are correct it lies in declaring it
but how can I declare it since it is nothing before executing the rest of the line after $result_final.
Further in order to do its job the declaration should include $counter
 
the dot means "concatenate with the existing value of this variable".

if the variable has not previously been instantiated then the warning/notice will be generated.

this is easy to avoid e.g.
Code:
$fruits = array("apple", "orange", "banana");
$output = ''; //instantiate the variable
foreach ($fruits as $fruit){
 $output .= "\t<option value=\"$fruit\">$fruit</option>\r\n";
}
echo "<select name=\"fruits\">\r\n$output\r\n</select>";

if you comment out line 2, you will get the warning/notice.
 
As I mentioned earlier I used a concatenation
that works but does create an error because the var is not declared
the real question is not how to transform the existing in a
foreach()
but how declaring the var to use my line with the $counter
thank you
 
I see you have a single quote in there..
Also, why the $count variable after the closing slash for the <img> ?

Code:
$result_final .= "<img src=\"{$thumb_dir}/tb_{$new_pic}\" />{$counter} File Added<p>";

Olav Alexander Mjelde
Admin & Webmaster
 
Thanks, it still results in undefined var
and the final result is (for example uploading two images in one shot) twice the same first image and once the second one

And you are correct using $counter was redundant since I am still above the $counter++ end.
 
Jpadie was just giving you an example, you need to instantiate the variable before you start concatenating it.

Just place $result_final=''; before you start to use it to concatenate values.
If you are unsure where to place it,perhaps
If you show us a bit more code, we can tell you where you need to add the $result_final='';



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I tried it even before posting

if I declare the var as '';
then indeed there is no error
but the upload is limited to the very first img
I found a way around but I am not satisfied with it
just echo it works fine.
<<<<
// Save the thumbs
$sub_dir="/thumbs";
$thumb_dir=$image_dir.$sub_dir;
$function_to_write( $destination_handle, $thumb_dir."/tb_".$new_pic);
ImageDestroy($destination_handle );

//////// Load in DB thumb's name ////////////////////
$pre="tb_";
$th=$pre.$new_pic;
mysql_query( "UPDATE img_2 SET thumb_name='".CleanDb($th)."'
WHERE img_id='".CleanDb($new_id)."'" );
//////// end updating DB ///////////////////////////

echo"<img src='".$thumb_dir. "/tb_".$new_pic."' border='0'> File Added<p>";
//$final_result='';
//$final_result .="<img src=\"{$thumb_dir}/tb_{$new_pic}\" border=\"0\"> File Added<p>";
//echo"$final_result";

$counter++;
}
>>>>
 

place the [red]$final_result='';[/red] just before you start the loop;

Wherever that is.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Indeed it makes sense, thanks for showing the way.
 
If you use a FOR loop, you can also use the variable in the scope. Not sure if this was clear enough put..

ps. I dont see why you need toe $count either? for debugging?

If your loop only uploads one image, you might have a loop problem. Be aware that the array most likely starts at item 0, not item 1.

Code:
for ($i = 0; $i < count($foo); $i++) {
    echo $i . "<br />\n";
  }

(take this as psuedocode)

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top