Page 1 of 1
populating a table in a loop
Posted: Thu Jul 29, 2004 4:26 pm
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
Posted: Thu Jul 29, 2004 4:47 pm
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++;
}
Posted: Thu Jul 29, 2004 4:52 pm
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?
Posted: Thu Jul 29, 2004 5:18 pm
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
Posted: Thu Jul 29, 2004 8:28 pm
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>";
Posted: Fri Jul 30, 2004 10:47 am
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
Posted: Fri Jul 30, 2004 10:59 am
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>";
}
}
?>