populating a table in a loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

populating a table in a loop

Post by John Cartwright »

Hmm I've done this before but it's slipping my mind.

I return some search results from a table and want to display the picutres in rows of 4... and I also want to populate the empty columns..

for example when thereare like 6 results returned, there will be 2 empty columns?

I know lots of people have done this beofre let me know if someone can Help me out
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Something like:

Code: Select all

$result = mysql_query("SELECT * FROM pics");
$loop=1;
while($row = mysql_fetch_assoc($result)){
   if($loop==1){
      echo "<tr>";
   }
   echo "
      <td><img src="$row[image]"></td>
   ";
   if($loop==4){
      echo "</tr>";
      $loop = 0
   }
   $loop++;
}
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

thanks alot kettle, I did the same thing except a little bit off..
thanks alot :)


but what if the search doesnt return a multiple of 4?

then the closing </tr> will never happen?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

also what if the search results is less than 4....

then the table will be alot less wide and look off on my page
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

use a for loop sorta like

Code: Select all

$result = mysql_query("SELECT * FROM pics");
echo "table border=0>";
for($i=0;$i<mysql_num_rows($result);$i++){
   echo "<tr>";
   $row = mysql_fetch_assoc($result);
   for($j=0;$j<4;$j++){
      if(!empty($row['img'])){
         echo "<td><img src="{$row['img']}" /></td>";
      }
   }
   echo "</tr>";
}
echo "</table>";
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

hmm i see where this is going but illusionist yours outputs each template 4 times then proceeds to the next row... i duno i might just stick with the previous code.. but out of all my experiecne I am ashamed I cannot figure this out so if anyone would like to furthur the solution ty very much
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

this is what I have at this point but would still like to solve the problem of of populating the empty columns with nothing like "no img" or somehing

Code: Select all

<?php
			$i = 0;
			$totalcount = 0;
			
			$result = mysql_query("SELECT * FROM `template` WHERE `catagory` = '".$_GET["search"]."'")
						or die("[templates.php] : ". mysql_error());
						
			echo "<table border='0' width='100%' cellpadding='3' cellpadding='5'>";
			
			while ($row = mysql_fetch_array($result))
			{
				$i++; 
				$totalcount++;
				
				if($i==1)
				{ 
					echo "<tr>"; 
				} 
   				
				echo "<td>
							<img src='small_pics/".$row["title"].".jpg' border='1'><br>".$row["title"]; 
				echo "</td>";
						
   				if($i==4)
				{ 
      				echo "</tr>"; 
      				$i = 0;
   				} 
   				elseif ($totalcount == mysql_num_rows($result))
				{										
					echo "</tr>";
				}
			
				
			} 
?>
Post Reply