Display Data in Multiple Columns from Database using PHP

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.

 

isplay Data in Multiple Columns

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

8 COMMENTS

  1. Hanamichi Sakuragi says:

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

    Reply
  2. Larry Wishon says:

    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.

    Reply
    1. Amit Sonkhiya says:

      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.

      Reply
      1. Larry Wishon says:

        Thanks for your quick reply, Amit. I am working on a project for a client where this will be a necessity. I will start playing around with it and see what I can do. I think there are many out there that can benefit from these. Thanks again,

        Reply

Leave a Reply

Enclose a code block like: <pre><code>Your Code Snippet</code></pre>.