Jump to content

Archived

This topic is now archived and is closed to further replies.

Nothadoth

While loop to display items

Recommended Posts

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=18

Like that, where there are so many per line.

Share this post


Link to post
Share on other sites
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 here
if(x == 4){
  echo "</tr><tr>";
  x = 1;
}else{
  x++;
}
}
[/code]

Share this post


Link to post
Share on other sites
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>&nbsp;</td>';
$x++;
}
}

echo '
</tr>
</table>';[/code]

Share this post


Link to post
Share on other sites
Ahh!

its not working now:

Please tell me what's wrong. Look at the page: http://www.finalfantasyfan.net/igbltd/shop/Computing/browse.php

On 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 table
mysql_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]

Share this post


Link to post
Share on other sites
[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 table
mysql_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.

Share this post


Link to post
Share on other sites
from GingerRobot's post, change the line
[code=php:0]
echo '</td><td>';
[/code]
to
[code=php:0]
echo '</tr><tr>';
[/code]

Share this post


Link to post
Share on other sites
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 table
mysql_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]

Share this post


Link to post
Share on other sites
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 table
mysql_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]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.