1

Possible Duplicate:
Multiple index variables in PHP foreach loop

I am trying to create a before and after image display using mysqli, php and html. I am pulling the records from the db with mysqli and php, and am trying to view the results in an Unordered List using a foreach loop.

I have my 2 arrays which are created above in a while loop of a mysqli query. The array will only contain 5 results (for pagination, may change later).

$before_array[] = '
<div class="highslide-gallery">
<li>
<a href="'.$beforeurl.'" class="highslide" onclick="return hs.expand(this)">
<img src="'.$beforeurl.'" width="100px" height="125px" alt="Highslide JS">
</a>
</li>';

$after_array[] = '
<li><a href="'.$afterurl.'" class="highslide" onclick="return hs.expand(this)">
<img src="'.$afterurl.'" width="100px" height="125px" alt="Highslide JS">
</a>
</li>';

and in the view of the page I have the following code to display the before and after pictures.

<div id="hoverwrap">
<ul class="hoverbox">;

<?php 
foreach ($before_array as $value) {
    echo $value; 
 }

foreach ($after_array as $value) {
  echo $value;
}
?>

</ul>

</div><!-- end of hoverwrap div-->

With the current code it is displaying the pictures : before - before - before - before - before - after - after - after - after - after.

Where I would like it to diplay: before - after - before - after etc etc.

I was thinking a while loop but no matter which way I try and implement it, it results in a similiar display. Any help or pointers in the right direction will be greatly appreciated.

Thanks.

Community
  • 1
  • 1
Nik
  • 201
  • 1
  • 3
  • 11
  • You can do that with one array, just concatenate the string. Apart from that, looping over tow arrays at once has already been asked (and answered) multiple times before. – hakre May 18 '12 at 22:13

1 Answers1

1

A for loop will do the trick here:

$x = count($before_array);
for($i = 0; $i < $x; $i++) {
    print $before_array[$i];
    print $after_array[$i];
}

Why can't you print it in the mysql loop itself?

Paolo Bergantino
  • 480,997
  • 81
  • 517
  • 436
  • You Sir are a genius. Thanks heaps, have been messing around with this for a while now :) I was trying to seperate as much php logic and displaying - html as possible, just makes it easier to read what is happening with the pages. Thanks again – Nik May 17 '12 at 06:31