Home » Tutorials

PHP Display Mysql Rows with Column Limit

This little script is very useful for having a set width of a table and displaying several rows organized in that table.

It is easy to just display a table full of rows for all the data in a mysql table. Something like this:

<?php

$sql = mysql_query("SELECT * FROM table1");
if(mysql_num_rows($sql) > 0){
echo "<table width=\"500\" border=\"1\" cellpadding=\"3\" cellspacing=\"0\">";
echo "<tr>";
while($row = mysql_fetch_array($sql)){
echo "<td width=\"100\" align=\"center\">".$row["col1"]."</td>";
}
echo "</tr></table>";
}
?>

This will grab all content of the mysql table and put the data from column 1 as a table column in this table.

Let’s say we have some images we want to display. The table is 500 pixels wide. So if your images are no larger than 100 pixels, you would want to display 5 columns per row in your table. This will give it good organization and flow in your webpage.

<?php

$sql = mysql_query("SELECT * FROM table1");
if(mysql_num_rows($sql) > 0){
echo "<table width=\"500\" border=\"1\" cellpadding=\"3\" cellspacing=\"0\">";
echo "<tr>";
for($i=0;$i<=mysql_num_rows($sql);$i=$i+1){
while($row = mysql_fetch_array($sql)){
$i++;
if($i%5==0){
// use the operator to find the remainder of $i / 5 (%)
// if the remainder is 0; it is a multiple of 5, so make a new row.
echo "<td width=\"100\" align=\"center\"><img src=\"".$row["col1"]."\" border=\"0\" style=\"max-width:100px\" /></td></tr><tr>";
} else {
echo "<td width=\"100\" align=\"center\"><img src=\"".$row["col1"]."\" border=\"0\" style=\"max-width:100px\" /></td>";
}

}
}
echo "</tr></table>";
}
?>

The two codes will appear like so:

Before Column Limit:

Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7 Image 8 Image 9 Image 10

After Column Limit:

Image 1 Image 2 Image 3 Image 4 Image 5
Image 6 Image 7 Image 8 Image 9 Image 10
1

Popularity: 28% [?]

Share/Bookmark this!

34 Comments

Leave a reply

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally recognized avatar, please register at Gravatar.

Side Notes

This entry was posted by on November 6, 2009 at 2:47 PM and filed under Tutorials category.

You can add your comments or trackback from your own site. To keep you updated to the latest discussion, you can subscribe to these comments via RSS.

Recent Entries

Pages

Recent Comments

Resources

Questions & Answers

Just started! Have not answered any questions.

Tag Cloud

background body CSS database date dropdown email error_reporting favicon file filesize form format function global hosting HTML image Javascript limit link linkbar mysql numbers online option ordinalize pagination pattern photoshop PHP resize rows script search snowing stylesheet switch table thumbnail time timestamp upload validate variables

Sponsors