Re: Variable representation

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

 



It would better to just use an array, and then iterate through that.

$images[] =  stripslashes( $row['image_1'] );
$images[] =  stripslashes( $row['image_2'] );
$images[] =  stripslashes( $row['image_3'] );
$images[] =  stripslashes( $row['image_4'] );

foreach( $images as $k => $v ) {
    $k++; // increment k since it starts at 0, instead of 1
    if ( strlen( trim( $v ) ) ) {
        echo "<li><a href=\"http://www.theverseoftheday.info/store-images/";
. $v . "\" title=\"Image " . $k . "\">Image " . $k . "</a></li>\r\n";
    }
}

Adam.

On Sun, Apr 1, 2012 at 8: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
>



-- 
Adam Randall
http://www.xaren.net
AIM: blitz574
Twitter: @randalla0622

"To err is human... to really foul up requires the root password."

[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