Re: Variable representation

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, Apr 1, 2012 at 10:52 PM, Ron Piggott
<ron.piggott@xxxxxxxxxxxxxxxxxx> wrote:
> Hi Everyone:
>
> I am assigning the value of 4 images to variables following a database query:
>
> $image_1 = stripslashes( $row['image_1'] );
> $image_2 = stripslashes( $row['image_2'] );
> $image_3 = stripslashes( $row['image_3'] );
> $image_4 = stripslashes( $row['image_4'] );
>
> What I need help with is how to represent the variable using $i for the number portion in the following WHILE loop.  I am not sure of how to correctly do it.  I am referring to: $image_{$i}
>
> ===
> $i = 1;
> while ( $i <= 4 ) {
>
>    if ( trim( $image_{$i} ) <> "" ) {
>
>        echo "<li><a href=\"http://www.theverseoftheday.info/store-images/"; . $image_{$i} . "\" title=\"Image " . $i . "\">Image " . $i . "</a></li>\r\n";
>
>    }
>
> ++$i;
> }
> ===
>
> How do I substitute $i for the # so I may use a WHILE loop to display the images?  (Not all 4 variables have an image.)
>
> Ron Piggott
>
>
>
> www.TheVerseOfTheDay.info

While this doesn't answer your question directly (I will get to it in
a moment), you might be better able to use $image as an array and
assign the values as:

$image[1] = stripslashes( $row['image_1'] );
$image[2] = stripslashes( $row['image_2'] );
$image[3] = stripslashes( $row['image_3'] );
$image[4] = stripslashes( $row['image_4'] );

While noting that arrays in php start indexing at 0, that doens't
really matter al that much in your implementation. Assuming you need
to set the four image variables separate from the part where you emit
the HTML stuff, you can write a loop:

foreach (range(1,4) as $i) {
    $image[$i] = stripslashes($row['image_'.$i]);
}

which might be slightly less obvious to some, but I think is better code.

Then:

foreach (range(1,4) as $i) {
    if (!empty(trim($images[$i]))) {
        echo "<li><a
href=\"http://www.theverseoftheday.info/store-images/"; . $image[$i] .
"\" title=\"Image " . $i . "\">Image " . $i . "</a></li>\r\n";
    }
}

(If you don't need the four images outside of the place you're
emitting the HTML, you don't even need to use the intermediate
$image[] array, really, and can combing the whole thing in one swell
foop:

foreach (range(1,4) as $i) {
    if (!empty($image = trim(stripslashes($row['image_'.$i])))) {
       echo "<li><a
href=\"http://www.theverseoftheday.info/store-images/"; . $image . "\"
title=\"Image " . $i . "\">Image " . $i . "</a></li>\r\n";
    }
}

And skip the array altogether.)

As for doing what you originally asked, that requires doing an eval()
on the statement utilizing string interpolation, like so:

  eval('echo "image $i is $image_' . $i . '".PHP_EOL;');

but I think that's a bit harder to read and understand what's going
on. When you have to add in escaped quotes and such, it gets much
hairier.

To utilize it in the loop you have above, I'd split the echoes up:

   echo "<li><a href=\"http://www.theverseoftheday.info/store-images/";;
   eval ('echo "$image_" . $i;');
   echo "\" title=\"Image " . $i . "\">Image " . $i . "</a></li>\r\n";

so that the eval portion is doing only what needs to be interpolated
and evaled. The rest of the output is fine the way it is.

Note that if you did this:

   echo "<li><a href=\"http://www.theverseoftheday.info/store-images/";
. eval('echo "$image_" . $i;') . "\" title=\"Image " . $i . "\">Image
" . $i . "</a></li>\r\n";

the part in the eval would get written out first, then the rest of the
echoed string, which is why you would need to split them up first.

Generally, I think it's best to completely avoid using eval unless
there is no other way to do what you want.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux