Can anyone help – this is driving me mad.
I am calling a mysql table and wish to display the results (or check the results with an if statement) more than once. But in this loop (see below) it only calls the table and its rows as an array in the first instance (of $i=1). This code will build 20 divs called Box but only populate the first one with table data. Why?
I think that I should be using a foreach loop within the outer loop (I have got this to work in wordPress with that btw) but can’t figure out the syntax. Any help would be really appreciated.
$i=1;
while ($i <= 20)
{
echo "<div class=\"Box ".$i."\">";
echo $i;
echo "<br>";
while ($row = mysql_fetch_array($result))
{
echo $row['name'];
}
echo "</div>";
$i++;
}
If I understand, you are attempting to use the same result resource multiple times in a loop.
After looping over the result resource the first time, you need to rewind it back to its original position to be able to loop over it again. It can be rewound with
mysql_data_seek($result, 0), but a better strategy is to load the whole thing into an array before your loops and then iterate the array in each loop: