PHP


Displaying Records in Multiple Columns Using PHP
By Richard Clark
16-Jan-10
Views: 22378

It is a very common technique to display data read from the database in multiple columns. The data can be text, image or mixture of both. This article will guide you presenting data in multiple columns using PHP.
 
Displaying Records in Multiple Columns Using PHP (Page 1 of 1)

For this article, i have selected a table members, containing each member's pic and some other text detail. The members will be shown in multiple columns as shown in the picture below.

The technique is to use 2 loops (one nested inside the other). The outter loop creates rows and the inner loop creates columns. The only thing we have to take care of is the inner loop creates equal number of columns for each row. If there are no more records the inner loop should print a blank column. Have a look at the code blow.

<?
	$query="select * from members";
	$result=mysql_query($query);
 
	$cols=3;		// Here we define the number of columns
	echo "<table>";	// The container table with $cols columns
	do{
		echo "<tr>";
		for($i=1;$i<=$cols;$i++){	// All the rows will have $cols columns even if
									// the records are less than $cols
			$row=mysql_fetch_array($result);
			if($row){
				$img = $row['image_path'];
 ?>
        <td>
            <table>
                <tr valign="top">
                    <td><img src="images/<?=$img ?>" /></td> <!-- columns can have both text and images -->
                    <td>
                        <b><?=$row['name'] ?></b><br />
                        <?=$row['email'] ?><br />
                        <?=$row['password'] ?><br />
                    </td>
                    <td width="50">&nbsp;</td>	<!-- Create gap between columns -->
                </tr>
           </table>
        </td>
<?
			}
			else{
				echo "<td>&nbsp;</td>";	//If there are no more records at the end, add a blank column
			}
		}
	} while($row);
	echo "</table>";
 ?>

We can easily achieve above result with div structure also. But to understand the technique, i have used tables. Hope it helps. Thank you!

RSS stands for Really Simple Syndication is an XML format used by publishers to keep their audience updated by publishing frequent content via RSS and content readers display latest news and content from RSS feeds, content receivers use newsreaders or content aggregators . Read More PHP RSS Feed Tutorial

Comments
Ahmad
[12-Feb-2010]
#1

Thank u so much. it is exactly what i need.

 

thanks once again.

jogesh
[20-Aug-2010]
#2

thanks a lot dear

 

pls continue post

arian
[09-Dec-2010]
#3

thank u.

it is good!

Khang
[27-Dec-2010]
#4

THANK YOU VERY MUCH..I need it indeed..There are a lot of Forums or Blogs have title as this topic but them guide wrongly with titleself.

neo
[06-Feb-2011]
#5

Thank alot bro, you are angel

sunny
[19-May-2011]
#6

Great Post . i appreciate this code as i was searching this long time

vishal
[20-May-2011]
#7

great code !!!!!!!!!!  Keep posting..

well can u do a thing more with this code if u can give the pagination option also it will be great help .

cata
[06-Jun-2011]
#8

great very helpfull thanks!

Kathy
[23-Jun-2011]
#9

Thank you so much!!!

lewis
[27-Jul-2011]
#10

this code is great but is there anyway to create this grid system and have a limited grid and a next page link button???????
any replies highly appreciated :)

zeerussia
[29-Jul-2011]
#11

thanks a lot.

mahesh
[28-Sep-2011]
#12

thanks a lot and great post.

Brian
[05-Oct-2011]
#13

Hi,

I am new to php.

Could you show the same using

's instead of tables? And if you don't mind, could you also include how to define the number of rows as well?

 

Thank you,
Brian

Shahbaz Ahmed
[13-Oct-2011]
#14

hi , i m beggginer in php i mtrying to make lik ethis display dynamic record in column, but not getting help and this code is not working

please can anybody guide me

thank

santosh
[31-Oct-2011]
#15

This code showing data in row wise not column wise as you mentioned above.

DAVE
[12-Nov-2011]
#16

great script , my question how do yu create to link to a specific id and how do yu store those images

Julian
[17-Nov-2011]
#17

Thank you soo much. Worked perfectly. Good work

Tylor
[23-Nov-2011]
#18

Great work! I used this as a starting point, and then tweaked it for my own needs. The only question I have is how to make it validate to the W3C standards. Normally I wouldn't care, but I'm using it in a web class.

dave
[24-Nov-2011]
#19

nice tutorial ! is there any way you can link to an id and get all the information like address and all that.

please help

alexrabe
[24-Nov-2011]
#20

Hi,

Thanks!
 I really looking for the same code as I have to print data in two columns.

 

Thanks a lot

Corina
[30-Dec-2011]
#21

Just looking for this script for over two years ;-)

ThanX

Prem
[20-Jan-2012]
#22

Superb post.. Thanx a lot....Wish you all the very best...

brian
[20-Feb-2012]
#23

thanks alot how can i create pagination with thats script

mukesh
[13-Mar-2012]
#24

include("db_connection.php");// database connection.......
if(isset($_GET['p'])){
$newp = $_GET['p'];
}else{
$newp='1';
}
$plimit = "10";// set your page limit here
$strSQL = mysql_query("SELECT * FROM `users`");
$totalrows = mysql_num_rows($strSQL);
$pnums = ceil ($totalrows/$plimit);// function which calculate the total no of pages
$start = ($newp-1) * $plimit;
$starting_no = $start + 1;
if ($totalrows - $start < $plimit) { $end_count = $totalrows;
} elseif ($totalrows - $start >= $plimit) { $end_count = $start + $plimit; }
$query="select * from members limit $start,$plimit";
$result=mysql_query($query);
 
$cols=3; // Here we define the number of columns
echo ""; // The container table with $cols columns
do{
echo "
";
for($i=1;$i<=$cols;$i++){ // All the rows will have $cols columns even if
// the records are less than $cols
$row=mysql_fetch_array($result);
if($row){
$img = $row['image_path'];
 ?>
       
}
else{
echo "
"; //If there are no more records at the end, add a blank column
}
}
} while($row);
echo "
           
               
                   
                   
                   
               
           
                       
                       
                       
                   
 
       
 
";
 ?>
               
viewing - of entries
           
{?>
  
 
?>

jitendra
[02-Apr-2012]
#25

great code !!!!!!!!!!  Keep posting..

well can u do a thing more with this code if u can give the pagination option also it will be great help .

Rekha
[29-May-2012]
#26

thanks a lot sir...

Kunle A Thaiwo
[18-Jun-2012]
#27

Thank you so much i really appreciate this , awww this is just for me thank you so much once again 

johan
[27-Aug-2012]
#28

thks bro

thiyagusathya
[03-Sep-2012]
#29

hi,

this is nice code,very help full or me..... Thanks a lot

JCane
[29-Nov-2012]
#30

WOW! this is really simple, nice but great script!

Thanks for posting! keep sharing!

amir
[14-Dec-2012]
#31

Thanks for your great tutorial, thank you very very much

fooz
[16-Jan-2013]
#32

its now working

 

Jim
[30-Mar-2013]
#33

I am not new to php but I struggle with it. I have put this code into a page, set the mysql request and get something like not expecting $results. I have tried to check if $results is valid and I get not connected to database. If I use the same mysql request with different code/printout it fuctions and returnes the data. sometimes I get a unexpecdted end of page. What the heck am I doing wrong. thanks JIm 

Kistappa
[04-May-2013]
#34

Display records in multiple columns is giving problem. I am using mssql database. Iam having field "PHOTO" column. I wrote program in php for display the image is working with simple code. But When using the display records in multiple columns in php, the image is not displaying in the table column. The following code used.

			$row=mysql_fetch_array($result);
			if($row){
				$img = $row['image_path'];
 ?>
        
            "top">
Can anybody help in this regard. I am so much thankfull to you.

Kistappa.P
DIO
NIC,Rangareddy District
"image/$im ?>" />

Kistappa
[04-May-2013]
#35

Iam using linux ubuntu server. I am writting code in  php. I have SQL server R 2008.

I am connecting to the database using php in ubuntu server. The above code is very beautiful. I used the same code but image is not display in the table. Where as without table with simple code, i can able to display the image. Could you help to display the image in the above code?

Kistappa.P

    DIO

NIC,Rangareddy District.

Kistappa
[07-May-2013]
#36

Iam using linux ubuntu server. I am writting code in  php. I have SQL server R 2008.

I am connecting to the database using php in ubuntu server. The above code is very beautiful. I used the same code but image is not display in the table. Where as without table with simple code, i can able to display the image. Could you help to display the image in the above code?

Kindly help in this regard.

Kistappa.P

    DIO

NIC,Rangareddy District.

Leave a Comment
Age (Required, will not be shown)
Name
Email (Required, will not be shown)
Website (Optional, starting with http://)
 
Are you human ?

Enter the code shown above