Display Data in Multiple Columns from Database using PHP

isplay Data in Multiple Columns

In some scenario we need to display data in multiple columns in a HTML table where each row from database keeps data for a single column of HTML table. The data can be text, image or mixture of both. This article will guide you display data in multiple columns from database using PHP.

 

Example of this kind of arrangement is product catalog which you can see in picture above. The products are displayed in columns along with image and product name.

We have used a while loop to achieve that. The only thing to keep in mind is to print blank columns to maintain equal number of column in each row after displaying last record from database.  For that we have used for loop after end of while.

The code along with comments will let you assist to understand the code and to display data in multiple columns:

The above result can easily be achieved with div structure or HTML lists also. But to understand the technique, i have used tables. Hope it will help you to display data in multiple columns from database using PHP.

You Might Interested In

  • Larry Wishon

    This has been very helpful. I would like to be able to have two responsive columns. Any quick advice on how this would be achieved? I realize I would probably use divs instead of tables. But not real clear how to integrate that into the above code.

    • Amit Sonkhiya

      Hi,

      I will write a post in day one or two to use the same with div.However you can replace tags as the following:

      table with parent container
      tr with row in container
      td with columns in each row

      Also you would need to apply some CSS for responsive columns.

  • Hanamichi Sakuragi

    sir can you please explain this line of codes “$img = “http://yourdomain.com/assets/”.$row[‘image_name’];” cause i cant show the images and what data should i enter in the database. this is the location of my images, src=”imgreyvibal.jpg”.