Jump to content


Photo

While loop to display items


  • Please log in to reply
12 replies to this topic

#1 Nothadoth

Nothadoth
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 15 August 2006 - 05:44 PM

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-direc...iewCat&catId=18

Like that, where there are so many per line.

#2 bpops

bpops
  • Members
  • PipPipPip
  • Advanced Member
  • 232 posts

Posted 15 August 2006 - 06:12 PM

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..

x=1;
while(-----------){
//whatever you want here
if(x == 4){
  echo "</tr><tr>";
  x = 1;
}else{
  x++;
}
}



#3 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 15 August 2006 - 06:28 PM

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:

$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>';


#4 Nothadoth

Nothadoth
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 15 August 2006 - 09:34 PM

It works. Thank you very much

#5 Nothadoth

Nothadoth
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 15 August 2006 - 09:44 PM

Ahh!

its not working now:

Please tell me what's wrong. Look at the page: http://www.finalfant...ting/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

    $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++;
    
}


#6 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 15 August 2006 - 09:50 PM

<?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++;
}
}
?>

I always find these things difficult to do without testing them, but try that.

#7 Nothadoth

Nothadoth
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 15 August 2006 - 09:56 PM

Nope :S

http://www.finalfant...ting/browse.php

#8 bpops

bpops
  • Members
  • PipPipPip
  • Advanced Member
  • 232 posts

Posted 15 August 2006 - 10:01 PM

from GingerRobot's post, change the line
echo '</td><td>';
to
echo '</tr><tr>';


#9 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 15 August 2006 - 10:03 PM

Whoops

#10 Nothadoth

Nothadoth
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 15 August 2006 - 10:07 PM

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.finalfant...ting/browse.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)) { 
  
  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>";


#11 Nothadoth

Nothadoth
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 15 August 2006 - 10:22 PM

I've added in all the categories now. All of the rows work but the first one. How come?

#12 Nothadoth

Nothadoth
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 16 August 2006 - 12:05 AM

Anyone?

Sorry to bumb. Only I need this soon - It's for a job.

#13 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 16 August 2006 - 03:40 PM

You've just got to swap it around like i did before...

<?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>";
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users