Database Articles

  Home arrow Database Articles arrow Page 2 - Multicolumn Output from a Database wit...
DATABASE ARTICLES

Multicolumn Output from a Database with PHP
By: Matt Wade
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2002-07-17

    Table of Contents:
  • Multicolumn Output from a Database with PHP
  • Horizontal Display
  • Vertical Display
  • Something to think about

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    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 
    "&lt;TABLE BORDER=\"0\"&gt;\n";
    while(
    $row mysql_fetch_array($result)) {
        echo 
    "&lt;TR&gt;&lt;TD&gt;" $row['stuff'] . "&lt;/TD&gt;&lt;/TR&gt;\n";
    }
    echo 
    "&lt;/TABLE&gt;\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 
    "&lt;TABLE BORDER=\"0\"&gt;\n";

    //changed this to a for loop so we can use the number of rows
    for($i 0$i &lt$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 "&lt;TR&gt;\n";
        }
        echo 
    "&lt;TD&gt;" $row['stuff'] . "&lt;/TD&gt;\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 "&lt;/TR&gt;\n";
        }
    }
    echo 
    "&lt;/TABLE&gt;\n";
    ?>

    That looks a bit more complicated, but let's break it down and look at the new additions.

    $columns = 2;

    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 &lt; $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 "&lt;TR&gt;\n";
    }

    This is the code that starts a row.

    echo "&lt;TD&gt;" . $row['stuff'] . "&lt;/TD&gt;\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 "&lt;/TR&gt;\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.

    More Database Articles Articles
    More By Matt Wade

    blog comments powered by Disqus

    DATABASE ARTICLES ARTICLES

    - Completing a Book Inventory Management System
    - Uploading Images for a Book Inventory Manage...
    - Finishing the Add Book Story for a Book Inve...
    - Integration Testing for a Book Inventory Man...
    - User Stories for a Book Inventory Management...
    - Unit Testing a Book Inventory Management Sys...
    - Testing a Book Inventory Management System
    - Implementing Models for a Book Inventory Man...
    - Book Inventory Application: Publishers and B...
    - Handling Publishers in a Book Inventory Mana...
    - Publisher Administration for Book Inventory ...
    - Book Inventory Management
    - Using the SQL Reference Manual
    - Using Oracle SQL Developer with SQL Statemen...
    - Fixing Errors with Oracle SQL Developer


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap