misterm Posted May 17, 2008 Share Posted May 17, 2008 Hi I guess somebody will have a really simple answer to this. I have tried variable variables and still can't get it to work. I am putting together a small ecard script. I have 1 variable that contains a numberic value: $imageid I am trying to show a header message above the ecard image relative to the the number stored in $imageid. This would be very simple as an if and elseif statement: <? $cardHeader1 = "Happy Birthday"; $cardHeader2 = "Merry Christmas"; $cardHeader3 = "Merry Anniversary"; ?> <? if ($imageid == "1") { echo $cardHeader1; } elseif ($imageid == "2") { echo $cardHeader2; } elseif ($imageid == "3") { echo $cardHeader3; } ?> but if I have 100s of headers, it would be huge. Is there an easier way to dynamically say if the image id number matches the card header number then print the contents of the card header? Any help would be much appreciated. Many thanks Mr M Quote Link to comment Share on other sites More sharing options...
ShimmyShine Posted May 17, 2008 Share Posted May 17, 2008 Indeed there is $i = 1; while($imageid == $i) { echo $cardHeader."".$i; $i++; } I believe that should work although i think theres a problem with echo $cardHeader."".$i; Lol Hope this helps you Shimmy Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 17, 2008 Share Posted May 17, 2008 $image = "cardHeader" . $i; echo sprintf("<img src=\"%s\" />", $$image); Note the two $$. Quote Link to comment Share on other sites More sharing options...
phpzone Posted May 17, 2008 Share Posted May 17, 2008 Dont forget your .jpg/.gif etc. eg. echo sprintf("<img src=\"%s.jpg\" />", $$image); Quote Link to comment Share on other sites More sharing options...
misterm Posted May 17, 2008 Author Share Posted May 17, 2008 Hi I can't seem to get that to work. The php to display the image is all done and dusted, I just need to display the relevant header based on the value of $imageid :/ Thanks again Mr M Dont forget your .jpg/.gif etc. eg. echo sprintf("<img src=\"%s.jpg\" />", $$image); Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted May 17, 2008 Share Posted May 17, 2008 $i = 1; while($imageid == $i) { echo $cardHeader."".$i; $i++; } Shimmy ouch! infinite loop! to build a dynamic variable, you can use this: $img_name = ${"cardHeader".$i}; // then echo "<IMG SRC='$img_name'>"; Quote Link to comment Share on other sites More sharing options...
ShimmyShine Posted May 17, 2008 Share Posted May 17, 2008 $i = 1; while($imageid == $i) { echo $cardHeader."".$i; $i++; } I see now how its infinite! Lol woops This would work though $i = 1; $ie = #; // Replace # with the ending number of images. while($imageid == $i && $i <= $ie) { echo $cardHeader."".$i; $i++; } Shimmy Quote Link to comment Share on other sites More sharing options...
misterm Posted May 17, 2008 Author Share Posted May 17, 2008 Thanks Guys Maybe the infinite loop thing is why I just had to reboot a few times :/ hee hee. Hmmm, that still only displays the numeric value of $imageid and not the contents of $imageid :/ I'm gonna be bald me thinks. $i = 1; while($imageid == $i) { echo $cardHeader."".$i; $i++; } I see now how its infinite! Lol woops This would work though $i = 1; $ie = #; // Replace # with the ending number of images. while($imageid == $i && $i <= $ie) { echo $cardHeader."".$i; $i++; } Shimmy Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 17, 2008 Share Posted May 17, 2008 You need to echo it out in <img /> tags, dude. Quote Link to comment Share on other sites More sharing options...
misterm Posted May 17, 2008 Author Share Posted May 17, 2008 Hi DarkWater I'm not trying to do anything with the image itself, as that is already working correctly, I'm just trying to print the text header above the image based on the the variable $imageid. Kind of: Print the text stored in variable $cardHeader1 if $imageid is equal to 1 and so on. Then I want to print each header text based on the imaged id: if $imageid = 1 print the text stored in $cardHeader1 if $imageid = 2 print the text stored in $cardHeader2 if $imageid = 3 print the text stored in $cardHeader3 if $imageid = 4 print the text stored in $cardHeader4 if $imageid = 5 print the text stored in $cardHeader5 if $imageid = 6 print the text stored in $cardHeader6 if $imageid = 7 print the text stored in $cardHeader7 if $imageid = 8 print the text stored in $cardHeader8 if $imageid = 9 print the text stored in $cardHeader9 if $imageid = 10 print the text stored in $cardHeader10 if $imageid = 11 print the text stored in $cardHeader11 if $imageid = 12 print the text stored in $cardHeader12 Thanks Mr M You need to echo it out in <img /> tags, dude. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 17, 2008 Share Posted May 17, 2008 $header = "cardHeader" . $imageid; echo $$header; Quote Link to comment Share on other sites More sharing options...
misterm Posted May 17, 2008 Author Share Posted May 17, 2008 And it was that simple... That works exactly how I want. Many thanks for that DarkWater. And thanks to Shimmy, PHPZone and BlueSky too... You're all officers and gentlemen (bows and tilts hat). Now to find a good toupee... Mr M $header = "cardHeader" . $imageid; echo $$header; Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 17, 2008 Share Posted May 17, 2008 A lot of people actually don't know how to use variable variables (that's what they're called), to be honest. Quote Link to comment Share on other sites More sharing options...
ShimmyShine Posted May 17, 2008 Share Posted May 17, 2008 I have never even heard of a variable variable Shimmy Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.