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
populating a table in a loop
Moderator: General Moderators
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
-
kettle_drum
- DevNet Resident
- Posts: 1150
- Joined: Sun Jul 20, 2003 9:25 pm
- Location: West Yorkshire, England
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++;
}- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
-
Illusionist
- Forum Regular
- Posts: 903
- Joined: Mon Jan 12, 2004 9:32 pm
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>";- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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>";
}
}
?>