Topic awaiting preservation: php loop through database and limit number of entries? (Page 1 of 1) |
|
---|---|
Maniac (V) Inmate From: there...no..there..... |
posted 03-19-2009 01:53
Going to try to explain this as best I can. I have a bunch of stuff in a database and I need to display this info within a table. This isn't a problem at all but, I need it to only display 3 td tags per line and then move on to the row. code: <table> <tr> <td>My First Row</td> <td>My First Row 2</td> <td>My First Row 3</td> </tr> <tr> <td>My Second Row</td> <td>My Second Row 2</td> <td>My Second Row 3</td> </tr> </table>
code: for ($i = 1; $i <= $num; $i++){ $chairArray = mysql_fetch_array($chairResult); $chairName = $chairArray['name']; $chairDescription = $chairArray['description']; $chairImage = $chairArray['image']; echo "<tr>"; echo "<td>"; echo "<img src=\"images/catalog/ottomans/thumbs/$chairImage\" width=\"157\" height=\"147\" />"; echo "<br />"; echo $chairName; echo "</td>"; echo "</tr>"; }
|
Maniac (V) Mad Scientist with Finglongers From: Germany |
posted 03-19-2009 10:19
code: ?> <tr><td>header</td><td>header</td><td>header3</td><?php //no </tr>... for ($i = 1; $i <= $num; $i++){ $chairArray = mysql_fetch_array($chairResult); $chairName = $chairArray['name']; $chairDescription = $chairArray['description']; $chairImage = $chairArray['image']; if ($i % 3 == 0) { echo "</tr>"; echo "<tr>"; } echo "<td>"; echo "<img src=\"images/catalog/ottomans/thumbs/$chairImage\" width=\"157\" height=\"147\" />"; echo "<br />"; echo $chairName; echo "</td>"; } ?></tr><?php
|
Maniac (V) Inmate From: there...no..there..... |
posted 03-19-2009 12:14
thanks. quote:
|
Bipolar (III) Inmate From: Phoenix |
posted 03-23-2009 23:18
When the loop is done, do not neglect to deal with the remainder of 3, so if you looped once through the last mod 3 test, you'd have a remainder of 2, you'd need to either: |
Maniac (V) Inmate From: there...no..there..... |
posted 03-24-2009 20:35
or just do code: for ($i = 0; $i <= $num -1; $i++){
|