Database Articles
  Home arrow Database Articles arrow Page 2 - Multicolumn Output from a Database wit...
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
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 / 3
    2002-07-17

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

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    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


       · How can I accomplish this without useing tables with php and css?ThanksSean...
       · Hi, vey nice script just what I was lookin for.Ive been trying to create link...
       · &lt;? include(&quot;open_session.php&quot;); if...
       · Great piece of code, I'm a newbie. How can I change the code to create a border...
       · Great piece of code, I'm a newbie. How can I change the code to create a border...
       · [php]&lt;?php$q = mysql_query(&quot;SELECT `stuff` FROM `stuff` ORDER BY...
       · 
     

    DATABASE ARTICLES ARTICLES

    - More on Query Optimization for Oracle Databa...
    - Query Optimization in Oracle
    - Clusters and Other Data Structures for Oracle
    - Using Indexes with an Oracle Database
    - The Basics of Data Structures in Oracle
    - Oracle Data Structures
    - Best Practices for PL/SQL Variables
    - What`s Code Without Variables?
    - Clauses, Sorting, and SQL Queries
    - The From Clause and SQL Queries
    - Query Primer
    - Full Text Searches and Strings
    - Searching with Strings
    - Pattern Matching with Strings
    - Working with Cases of Strings





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT