PHP Display Mysql Rows with Column Limit
November 6, 2009 # 2:47 PM # Tutorials # 34 CommentsThis 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 |
Popularity: 28% [?]

Subscribe RSS
Comment RSS







[...] width of a table and displaying several rows organized in that table. Originally posted here: PHP Display Mysql Rows with Column Limit « bgallz.org | Templates … Share and [...]
P thanks to your ideas , i’d adore to adhere to your weblog as usually as i can.possess a good day
My English is not good, but to see the article you write a good feel of your
i really thanks to u
it is very simple code for this
i use it and its working very well
Hey – nice blog, just looking around some blogs, seems a pretty nice platform you are using. I’m currently using WordPress for a few of my sites but looking to change one of them over to a platform similar to yours as a trial run. Anything in particular you would recommend about it?
I can see that you are an expert at your field! I am launching a website soon, and your information will be very useful for me.. Thanks for all your help and wishing you all the success in your business.
This is my first time i visit here. I found so many entertaining stuff in your blog, especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the leisure here! Keep up the excellent work.
I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post.
Great post, thanks. Like your blog design too.
Thanks for this great website. I am trying to read some more posts but I cant get your website to display properly in my Opera Browser. Thanks again.
A topic close to my heart thanks. A good quick read.
You made some Good points there. I did a search on the topic and found most people will agree.
Making use of article directories makes for an excellent way to get more visitors to your website. I know that article writing is very tiring, it ultimately is worth your time which you put into it.
Hey – great blog, just looking around some blogs, seems a really nice platform you are using. I’m currently using WordPress for a few of my blogs but looking to change one of them over to a platform similar to yours as a trial run. Anything in particular you would recommend about it?
Excellent blog! I really love how it’s easy on my eyes as well as the facts are well written. I am wondering how I can be notified whenever a new post has been made. I have subscribed to your rss feed which must do the trick! Have a nice day!
interesting post, pretty much covered it all for me, thanks.
I wasn’t aware of some of the information that you mentioned so I want to just say thank you.
Awesome article! thanks for the good read!
Hey – nice blog, just looking around some blogs, seems a pretty nice platform you are using. I’m currently using WordPress for a few of my sites but looking to change one of them over to a platform similar to yours as a trial run. Anything in particular you would recommend about it?
Will it be acceptable to use your RSS feed?