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:

[code lang=”php”]
0){
echo “

“;
echo “

“;
while($row = mysql_fetch_array($sql)){
echo “

“;
}
echo “

“.$row[“col1″].”

“;
}
?>
[/code]

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.

[code lang=”php”]
0){
echo “

“;
echo “

“;
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 " “;
} else {
echo “

“;
}

}
}
echo “

“;
}
?>
[/code]

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
Filed under: TutorialsTagged with: , , ,

38 Comments

  1. i really thanks to u

    it is very simple code for this

    i use it and its working very well

  2. 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?

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. You made some Good points there. I did a search on the topic and found most people will agree.

  8. 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.

  9. 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?

  10. 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!

  11. interesting post, pretty much covered it all for me, thanks.

  12. I wasn’t aware of some of the information that you mentioned so I want to just say thank you.

  13. Awesome article! thanks for the good read!

  14. 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?

  15. Fascinating article with A number of useful advises. Thank you very much, I?l Begin Examining your blog.

  16. information to my Company 😉 Please keep it up. I look for your Articles on my iphone rss feed every day.

  17. Data to my Company 😉 Please Maintain it up. I look for your Articles on my iph1 rss feed every Day time.

  18. The blog was absolutely fantastic! Lots of great information and inspiration, both of which we all need!

  19. I do agree with all the ideas you have presented in your post. They are very convincing and will definitely work. Thanks for the post.

  20. Great post, I concur completely and appreciate the time you took to write it. Cheers!

  21. I like to require breaks during the day and browse by means of some blogs to see what others are saying. This weblog appeared in my searches and I could not help but clicking on it. I’m happy I did because it was a very enjoyable read.

  22. Excellent summary, bookmarked the site with hopes to read more!

  23. Pretty useful post. I’m doing research on this vocation right now and this helped. Thanks for your unique take on this topic.

  24. A thoughtful insight and ideas I will use on my blog. You’ve obviously spent a lot of time on this. Congratulations!

  25. Dude, I absolutely liked reading this post. You have convinced me to subscribe to your blog, but where can I find the RSS feed?

  26. Good blog post! I love your weblog, is it okay to try it for Rss or atom?

  27. This is the simplest code to select and display records from MySQL database table and display in PHP.

    $cn=mysql_connect($db_host,$db_user,$db_password) or die(mysql_error());.
    mysql_select_db($db_name,$cn) or die(mysql_error());.

    $sql = “SELECT field_name FROM table_name”;.
    $rs = mysql_query($sql) or die(mysql_error());.

    while($row = mysql_fetch_array($rs)){.

    echo $field_name = $row["field_name"];.
    echo “</br>”;

    }
    mysql_free_result($rs);

    Source:
    <a href="http://phphelp.co/2012/04/26/how-to-select-and-display-mysql-records-data-in-php/">http://phphelp.co/2012/04/26/how-to-select-and-display-mysql-records-data-in-php/</a&gt;

    OR

    <a href="http://addr.pk/acfd">http://addr.pk/acfd</a&gt;

  28. Thank you for this very good tutorial


Add a Comment

Your email address will not be published. Required fields are marked *

Comment *
Name *
Email *
Website