Jump to content

While loop to display items


Nothadoth

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.
Link to comment
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]

Link to comment
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]
Link to comment
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]
Link to comment
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.
Link to comment
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]
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.