Jump to content

[SOLVED] Simple variable problem


misterm

Recommended Posts

       

        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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

$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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.