Multicolumn Output from a Database with PHP - Horizontal Display
(Page 2 of 4 )
OK, first off let's take a look at how we would fetch some data from a database and display it in a table without multiple columns.
<?php mysql_connect('localhost','',''); mysql_select_db('test'); $query = "SELECT stuff FROM mystuff ORDER BY stuff"; $result = mysql_query($query); echo "<TABLE BORDER=\"0\">\n"; while($row = mysql_fetch_array($result)) { echo "<TR><TD>" . $row['stuff'] . "</TD></TR>\n"; } echo "</TABLE>\n"; ?> |
The preceeding code will connect to a database and display all the data in a single column table. With the data that I am using, it comes out looking like :
apple baby belt brother doctor father grape grapefruit mother orange pear shirt shoe sister tie |
So, what we want to do is break that into two columns so that apple and baby are on one line, belt and brother on the next, and so on.
The key to making this happen is the modulus operator. The modulus operator returns the remainder of a division problem. With the modulus operator, we will be able to tell how many columns we have output and know when to start a new row. Let's look at the code.
<?php //set the number of columns $columns = 2;
mysql_connect('localhost','',''); mysql_select_db('test'); $query = "SELECT stuff FROM mystuff ORDER BY stuff"; $result = mysql_query($query);
//we add this line because we need to know the number of rows $num_rows = mysql_num_rows($result); echo "<TABLE BORDER=\"0\">\n";
//changed this to a for loop so we can use the number of rows for($i = 0; $i < $num_rows; $i++) { $row = mysql_fetch_array($result); if($i % $columns == 0) { //if there is no remainder, we want to start a new row echo "<TR>\n"; } echo "<TD>" . $row['stuff'] . "</TD>\n"; if(($i % $columns) == ($columns - 1) || ($i + 1) == $num_rows) { //if there is a remainder of 1, end the row //or if there is nothing left in our result set, end the row echo "</TR>\n"; } } echo "</TABLE>\n"; ?> |
That looks a bit more complicated, but let's break it down and look at the new additions.
Here we are just setting how many columns we want.
$num_rows = mysql_num_rows($result); |
Just grabbing the number of rows in the result set.
for($i = 0; $i < $num_rows; $i++) { $row = mysql_fetch_array($result); |
Here we switched to a for loop so that we can keep track of which record we are on. We also moved the row retrieval to the inside of the for loop.
if($i % $columns == 0) { //if there is no remainder, we want to start a new row echo "<TR>\n"; } |
This is the code that starts a row.
echo "<TD>" . $row['stuff'] . "</TD>\n"; |
All we did here is remove the starting and ending of rows from the HTML
if(($i % $columns) == ($columns - 1) || ($i + 1) == $num_rows) { //if there is a remainder of 1, end the row //or if there is nothing left in our result set, end the row echo "</TR>\n"; } |
And this is the code that ends a row.
With the same data set, here is the output I get from the above script with two columns set:
apple baby belt brother doctor father grape grapefruit mother orange pear shirt shoe sister tie |
As you can see, this horizontal multi-column output is very simple. Now on to vertical display.
Next: Vertical Display >>
More Database Articles Articles
More By Matt Wade