Displaying data in rows in a table

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
gfinney1972
Forum Newbie
Posts: 3
Joined: Wed Nov 07, 2007 1:50 pm

Displaying data in rows in a table

Post by gfinney1972 »

Hi

I'm hoping someone can help as I've hit a brick wall. I've searched sites, googled, tried tutorials, tried borrowing code from tutorials and customising it but I'm no closer to a solution.

Basically I have a database of bands and I want to display a photo of all the bands in a table, I want three columns of images displaying until the MYSQL table reaches its end. ie there is no set number of rows to the table.

Can anybody help me out as I'm losing my mind trying to sort this out..

Cheers


Graham
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

try a combination of $i++ and $i % 3 in your loop to test whether it's time for a new row
gfinney1972
Forum Newbie
Posts: 3
Joined: Wed Nov 07, 2007 1:50 pm

Post by gfinney1972 »

Thanks for your quick response but I'm completely stuck on how to code this.

I can display all my data one after the other but I cannot even begin to understand how to code it into table format..
gfinney1972
Forum Newbie
Posts: 3
Joined: Wed Nov 07, 2007 1:50 pm

Post by gfinney1972 »

Thanks for your quick response but I'm completely stuck on how to code this.

I can display all my data one after the other but I cannot even begin to understand how to code it into table format..
User avatar
churt
Forum Commoner
Posts: 39
Joined: Wed Oct 04, 2006 9:59 am

Loop

Post by churt »

I did not have time to test so if it fails to work let me know and I will troubleshoot it. Hope this helps.

Code: Select all

<?php
//Connect to database and perform query for required data
mysql_connect("host","user","password");
mysql_select_db("dbname");
$query="select * from bands_table";
$result=mysql_query($query);


$ct=1;//Initialize Counter

$ptable="<table>";//Init table variable..ptable is just what I decided to call it...name it whatever you want
//Loop through results
while($row=mysql_fetch_array($result, MYSQL_ASSOC){
   if($ct==1) $ptable.="<tr>";//If $ct==1 Start new row.  Use '.=' to append data to variable
   $ptable.="<td><img src=".$row['imageurl_from_table']."></img></td>";//Display Image
   if($ct==3){ $ptable.="</tr>"; $ct=0; }//After the third image, end row and set $ct=0.  When $ct++ fires it will increment to 1 so that the next row will start properly on the next loop.
   $ct++;//Increment $ct
}
if($ct!==1) $ptable.="</tr>";//If $ct==1 at the end of the loop it meens it ended on a 3, set $ct=0 and incremented $ct to 1.  Therefore no end row would be needed.  Otherwise it is, so we test for $ct not equal to 1.
$ptable.="</table>";//End Table
echo $ptable;//Display Table

?>
Post Reply