looping though an array and saving to a single variable

I’m trying to loop through an array from a database, wrap each required element in html and adding the result to the end of a variable (concatenating?) but I’m struggling. My code looks something like this:

do
{
$thenumber=$image_array[NUMBER];
$image_bag=("<img src=“http://www.foobar.com/images/$thenumber.jpg” height=“140” />");
$all_images=($image_bag.$image_bag);
}
while ($image_array= mysql_fetch_array($result))

The array has 20 numbers in it and I would like everything to end up in the variable $all_images, but I’m clearly stuck at the point in red

Any help at all would be a great help indeed,

thanking you in anticipation,

Hi there,

Firstly the brackets in your variable setting are not required. Consider using the following code:
[php]
$all_images = ‘’;
do
{
$thenumber = $image_array[‘number’];
$all_images .= ‘’;
}
while ($image_array= mysql_fetch_array($result))

echo $all_images;[/php]

Smokey, that’s great - it does exactly what I need it to do and I can now look carefully at it and hopefully understand what’s happening here. Thanks so much for such a useful and speedy response!

No problem, basically I’ve initalised the variable at the top ( $all_images = ‘’; ) and then in each loop I append the variable with the new string using .=

You may have also seen the following:
[php]
//Remove the = from each line for what it’s doing (e.g. $num1 -= $num2; same as $num1 = $num1 - $num2;)
$num1 += $num2;
$num1 -= $num2;
$num1 *= $num2;
$num1 /= $num2;
$num1 %= $num2;

//I’ve used:
$string .= $another_string //This is the same as $string = $string.$another_string[/php]

Smokey, thanks again, can’t begin to tell you how much this is all going to help - much appreciated! Jem

Sponsor our Newsletter | Privacy Policy | Terms of Service