Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do i tile my loop results in a table?

Status
Not open for further replies.

jasonindus

Programmer
Dec 7, 2006
27
GB
i'm using a while loop to display the contents from my database. i can get the results to display in a row or in a column, but how do i get them to loop as a tiled format...

for example i would like to display the contents 3 to a column, then start a new row to display the next 3 results, so forth, so forth.

i want it to do something like this...

<tr>
<td></td>
<td></td>
<td></td>
</tr>

<tr>
<td></td>
<td></td>
<td></td>
</tr>

}

</table>



 
You would count the number of items and when you reach the requested number you create a new table row, reset the counter and start over. It is a simple case of a few ifs.

Of course, you could simply float the elements and get the same functionality (as long as only three of them fit) on the fly.
 
a for loop will do the trick..

for ($i = 0, $i < 3, $i++){
if ($i = 2){
echo 'NEW HEADER';
}
echo
' <tr>
<td></td>
<td></td>
<td></td>
</tr>
';
}

}


 
sorry forgot to mention that u have to repeat the for loop.. if you get the info out of a db it should be easy with while(list($variable) = mysql_fetch_row($result))
 
sorry i'm still kind of lost on this... can you piece the whole code together so i can see?

For example, if I have 9 records in my database, I want a table which is 3 colums wide and 3 rows high. In each cell, there should be one post... with 15 records, the table should be 3 columns wide and 5 rows high... etc

i'm using a while loop to pull the record (array) from the database and into a table, then i want to split the table at the third column and start a new row...

eg.

record 1 | record 2 | record 3 |
--------------------------
record 4 | record 5 | record 6 |
--------------------------
record 7 | record 8 | record 9 |

and so on...

know that I mean?? here's my code as it currently stands...


Code:
$sql = "SELECT * FROM 02_store_products WHERE prod_class = '$prod_class'";
$result = $db->query($sql);


echo '<div align="center">';
echo '<table class="table_no_line_height" width="445" cellspacing="1" cellpadding="5"> ';
echo '<tr valign="top"> ';

while ($row = $result->fetch()) {
	
echo '<td width="85"><img src="prod_img/'.$row['prod_img_sm'].'"><br>'.$row['prod_name'].'<br>&#163; '.$row['price'].'</td>';

}

echo '</tr>';
echo '</table>';
echo '</div>';
 
Code:
echo '<div align="center">';
echo '<table class="table_no_line_height" width="445" cellspacing="1" cellpadding="5"> ';
echo '<tr valign="top"> ';

[red]$count = 0;[/red]
while ($row = $result->fetch()) {
[red]if($count ==  3){
   echo "</tr></tr>";
   $count = 0;
}[/red]
    
echo '<td width="85"><img src="prod_img/'.$row['prod_img_sm'].'"><br>'.$row['prod_name'].'<br>&#163; '.$row['price'].'</td>';

[red]$count++;[/red]
}

echo '</tr>';
echo '</table>';
echo '</div>';
 
hi bam720... when i run your code i get this error...

Fatal error: Call to a member function on a non-object in /homepages/26/d99029449/htdocs/store/index.php on line 90
 
sorry bam, ignore that..;. its working perfectly... thanks for helping me out... very much appreciated!!!!
 
you might consider abandoning the table and using css to render your product tablets. this will look much better on reduced width browsers (the tablets will fall under each other). alternatively at the least render your table with css table rules.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top