Nothadoth Posted August 15, 2006 Share Posted August 15, 2006 I am trying to use a While loop to display all items in a database for a shop. However I would like it to be 4 items per line until it goes to a new line. This is easy without tables. However I wanted to use tables so I could show the title of the item above and price below the item image. So if I did this with a While loop it would make each table go to a new line - but I want 4 on a line.So if I wanted to use tables (unless there is a better way) then after every 4 items (and at the end of the last item) i'd have to put " </tr> <tr>"How would this be done?I hope it was clear, I couldn't explain easily. eg: http://www.mpc-direct.com/index.php?act=viewCat&catId=18Like that, where there are so many per line. Link to comment https://forums.phpfreaks.com/topic/17650-while-loop-to-display-items/ Share on other sites More sharing options...
bpops Posted August 15, 2006 Share Posted August 15, 2006 I'd just have some counter, and have the counter add one evertime the loop runs. when it hits 4, have it add the </tr><tr>, then reset itself..[code=php:0]x=1;while(-----------){//whatever you want hereif(x == 4){ echo "</tr><tr>"; x = 1;}else{ x++;}}[/code] Link to comment https://forums.phpfreaks.com/topic/17650-while-loop-to-display-items/#findComment-75256 Share on other sites More sharing options...
hitman6003 Posted August 15, 2006 Share Posted August 15, 2006 Adding to what bpops said, you might want to add a check at the end to make sure that all of the <td>s are there:[code]$x = 1;echo ' <table> <tr>';while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo ' <td> ' . $row['objectname'] . '<br /> <img src="' . $row['objectimage'] . '"><br /> ' . $row['objectprice'] . ' </td>'; $x++; if (x == 4) { echo " </tr> <tr>"; $x = 1; }}if ($x < 4) { while ($x <= 4) { echo ' <td> </td>'; $x++; }}echo ' </tr> </table>';[/code] Link to comment https://forums.phpfreaks.com/topic/17650-while-loop-to-display-items/#findComment-75269 Share on other sites More sharing options...
Nothadoth Posted August 15, 2006 Author Share Posted August 15, 2006 It works. Thank you very much Link to comment https://forums.phpfreaks.com/topic/17650-while-loop-to-display-items/#findComment-75337 Share on other sites More sharing options...
Nothadoth Posted August 15, 2006 Author Share Posted August 15, 2006 Ahh!its not working now:Please tell me what's wrong. Look at the page: http://www.finalfantasyfan.net/igbltd/shop/Computing/browse.phpOn the first line there is only 3. And the left items after the first row are lower than the rest. Why?! Please help[code] $cc = 1; print "<table border='0' cellpadding='0' cellspacing='0' style='border-collapse: collapse; margin-top: 15' width='100%'><tr>"; // Connect to Navigation database tablemysql_connect('localhost','hidden','hidden'); mysql_select_db('noth_igbltduk'); $querycats = mysql_query("SELECT * FROM productcategories_comp ORDER BY id");while($cats = mysql_fetch_array($querycats)) { if ($cc == 4) { print "</tr><tr> <td width='25%' valign='top'> <table border='0' cellpadding='0' cellspacing='0' style='border-collapse: collapse; margin-top: 20' bordercolor='#111111' width='100%'> <tr> <td width='100%' valign='top' valign='top'><center><b>".$cats['name']."</b></center></td> </tr> <tr> <td width='100%' valign='top'><center><a href='".$ROOT."shop/".$cats['parent']."/browse.php?mode=".$cats['name']."'><img src='".$ROOT."shop/".$cats['parent']."/".$cats['thumb_img']."' border='0'></a></center></td> </tr> </table> </td>"; $cc = 1; } else { print "<td width='25%' valign='top'> <table border='0' cellpadding='0' cellspacing='0' style='border-collapse: collapse' bordercolor='#111111' width='100%'> <tr> <td width='100%' valign='top'><center><b>".$cats['name']."</b></center></td> </tr> <tr> <td width='100%' valign='top'><center><a href='".$ROOT."shop/".$cats['parent']."/browse.php?mode=".$cats['name']."'><img src='".$ROOT."shop/".$cats['parent']."/".$cats['thumb_img']."' border='0'></a></center></td> </tr> </table> </td>"; $cc++; }[/code] Link to comment https://forums.phpfreaks.com/topic/17650-while-loop-to-display-items/#findComment-75345 Share on other sites More sharing options...
GingerRobot Posted August 15, 2006 Share Posted August 15, 2006 [code]<?php $cc = 1; print "<table border='0' cellpadding='0' cellspacing='0' style='border-collapse: collapse; margin-top: 15' width='100%'><tr>"; // Connect to Navigation database tablemysql_connect('localhost','hidden','hidden'); mysql_select_db('noth_igbltduk'); $querycats = mysql_query("SELECT * FROM productcategories_comp ORDER BY id");while($cats = mysql_fetch_array($querycats)) { print "<td width='25%' valign='top'> <table border='0' cellpadding='0' cellspacing='0' style='border-collapse: collapse' bordercolor='#111111' width='100%'> <tr> <td width='100%' valign='top'><center><b>".$cats['name']."</b></center></td> </tr> <tr> <td width='100%' valign='top'><center><a href='".$ROOT."shop/".$cats['parent']."/browse.php?mode=".$cats['name']."'><img src='".$ROOT."shop/".$cats['parent']."/".$cats['thumb_img']."' border='0'></a></center></td> </tr> </table> </td>";if($cc ==4){echo '</td><td>';$cc == 1;}else{$cc++;}}?>[/code]I always find these things difficult to do without testing them, but try that. Link to comment https://forums.phpfreaks.com/topic/17650-while-loop-to-display-items/#findComment-75349 Share on other sites More sharing options...
Nothadoth Posted August 15, 2006 Author Share Posted August 15, 2006 Nope :Shttp://www.finalfantasyfan.net/igbltd/shop/Computing/browse.php Link to comment https://forums.phpfreaks.com/topic/17650-while-loop-to-display-items/#findComment-75353 Share on other sites More sharing options...
bpops Posted August 15, 2006 Share Posted August 15, 2006 from GingerRobot's post, change the line [code=php:0]echo '</td><td>';[/code]to[code=php:0]echo '</tr><tr>';[/code] Link to comment https://forums.phpfreaks.com/topic/17650-while-loop-to-display-items/#findComment-75356 Share on other sites More sharing options...
GingerRobot Posted August 15, 2006 Share Posted August 15, 2006 Whoops Link to comment https://forums.phpfreaks.com/topic/17650-while-loop-to-display-items/#findComment-75357 Share on other sites More sharing options...
Nothadoth Posted August 15, 2006 Author Share Posted August 15, 2006 Yeah I have. And editted it a bit now. But if you look, the 4th of the last item still isn't working:http://www.finalfantasyfan.net/igbltd/shop/Computing/browse.php[code] $cc = 1; print "<table border='0' cellpadding='0' cellspacing='0' style='border-collapse: collapse; margin-top: 15' width='100%'><tr>"; // Connect to Navigation database tablemysql_connect('localhost','hidden','hidden'); mysql_select_db('noth_igbltduk'); $querycats = mysql_query("SELECT * FROM productcategories_comp ORDER BY id");while($cats = mysql_fetch_array($querycats)) { if ($cc == 4) {print "</tr><tr>";$cc = 1;} else {$cc++;} print "<td width='25%' valign='top'> <table border='0' cellpadding='0' cellspacing='0' style='border-collapse: collapse' bordercolor='#111111' width='100%'> <tr> <td width='100%' valign='top'><center><b>".$cats['name']."</b></center></td> </tr> <tr> <td width='100%' valign='top'><center><a href='".$ROOT."shop/".$cats['parent']."/browse.php?mode=".$cats['name']."'><img src='".$ROOT."shop/".$cats['parent']."/".$cats['thumb_img']."' border='0'></a></center></td> </tr> </table> </td>";} print "</tr></table>";[/code] Link to comment https://forums.phpfreaks.com/topic/17650-while-loop-to-display-items/#findComment-75359 Share on other sites More sharing options...
Nothadoth Posted August 15, 2006 Author Share Posted August 15, 2006 I've added in all the categories now. All of the rows work but the first one. How come? Link to comment https://forums.phpfreaks.com/topic/17650-while-loop-to-display-items/#findComment-75367 Share on other sites More sharing options...
Nothadoth Posted August 16, 2006 Author Share Posted August 16, 2006 Anyone?Sorry to bumb. Only I need this soon - It's for a job. Link to comment https://forums.phpfreaks.com/topic/17650-while-loop-to-display-items/#findComment-75434 Share on other sites More sharing options...
GingerRobot Posted August 16, 2006 Share Posted August 16, 2006 You've just got to swap it around like i did before...[code]<?php $cc = 1; print "<table border='0' cellpadding='0' cellspacing='0' style='border-collapse: collapse; margin-top: 15' width='100%'><tr>"; // Connect to Navigation database tablemysql_connect('localhost','hidden','hidden'); mysql_select_db('noth_igbltduk'); $querycats = mysql_query("SELECT * FROM productcategories_comp ORDER BY id");while($cats = mysql_fetch_array($querycats)) { print "<td width='25%' valign='top'> <table border='0' cellpadding='0' cellspacing='0' style='border-collapse: collapse' bordercolor='#111111' width='100%'> <tr> <td width='100%' valign='top'><center><b>".$cats['name']."</b></center></td> </tr> <tr> <td width='100%' valign='top'><center><a href='".$ROOT."shop/".$cats['parent']."/browse.php?mode=".$cats['name']."'><img src='".$ROOT."shop/".$cats['parent']."/".$cats['thumb_img']."' border='0'></a></center></td> </tr> </table> </td>";if ($cc == 4) {print "</tr><tr>";$cc = 1;} else {$cc++;}}print "</tr></table>";?>[/code] Link to comment https://forums.phpfreaks.com/topic/17650-while-loop-to-display-items/#findComment-75723 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.