How to show more than one result in while loop
hello friends..
<?while($row=(mysql_fetch_array(result))
{?>
<tr>
<td><?echo $row['photo']?></td>
<td><?echo $row['info']?></td>
</tr>
<?}?>
above code i use for fetching data.. one result in one row at a time..!!!
now i m try to show two result in one row..!!!
in other words.. two different data in one row..!!!
how can i do that...???
nish123
Junior Poster in Training
83 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
How about:
<?PHP
while($row=(mysql_fetch_array(result)) {
echo "
<td>$row['photo']</td><td>$row['info']</td>
";
if ($row=(mysql_fetch_array(result)) {
echo "
<td>$row['photo']</td><td>$row['info']</td>
";
}
echo "</tr>";
}
?>
I find it messy to go in and out of PHP so I modified it to make it all PHP.
chrishea
Nearly a Posting Virtuoso
1,428 posts since Sep 2008
Reputation Points: 210
Solved Threads: 230
How about:
<?PHP
while($row=(mysql_fetch_array(result)) {
echo "
<td>$row['photo']</td><td>$row['info']</td>
";
if ($row=(mysql_fetch_array(result)) {
echo "
<td>$row['photo']</td><td>$row['info']</td>
";
}
echo "</tr>";
}
?>
I find it messy to go in and out of PHP so I modified it to make it all PHP.
Just no. For two reasons: Reason 1) You're duplicating code, if he wants to change the td's to dd's or something like that he has to change it in two places, Reason 2) I'm not even going to begin with how much more ugly that looks when compared to exiting PHP
The best way to go about it is to keep a counter and whenever it's divisible by 2 you've looped twice so end the row and start another one.
<tr>
<?php $i = 1; while($row=mysql_fetch_array(result)): ?>
<?php if (!($i++ % 2) ): ?>
</tr><tr>
<?php endif ?>
<td><?php echo $row['photo'] ?></td>
<td><?php echo $row['info'] ?></td>
<?php endwhile ?>
</tr>
ShawnCplus
Code Monkey
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268