Database Articles
  Home arrow Database Articles arrow Page 3 - Multicolumn Output from a Database with PHP
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  
Forums Sitemap 
Download TestComplete 
JMSL Numerical Library 
IBM® developerWorks
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 / 2
    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 - Vertical Display


    (Page 3 of 4 )

    OK, now let's do a vertical display. Let's take a look at our new code.

    <?php
    $columns 
    4;

    mysql_connect('localhost','','');
    mysql_select_db('test');
    $query "SELECT stuff FROM mystuff ORDER BY stuff";
    $result mysql_query($query);

    $num_rows mysql_num_rows($result);

    //we are going to set a new variables called $rows
    $rows ceil($num_rows $columns);

    //to do this display, we will need to run another loop
    //this loop will populate an array with all our values
    while($row mysql_fetch_array($result)) {
        
    $data[] = $row['stuff'];
    }

    echo 
    "&lt;TABLE BORDER=\"0\"&gt;\n";

    //here we changed the condition to $i &lt; $rows
    for($i 0$i &lt$rows$i++) {

        echo 
    "&lt;TR&gt;\n";
        
        
    //here will run another loop for the amount of columns
        
    for($j 0$j &lt$columns$j++) {
            if(isset(
    $data[$i + ($j $rows)])) {
                echo 
    "&lt;TD&gt;" $data[$i + ($j $rows)] . "&lt;/TD&gt;\n";
            }
        }
        echo 
    "&lt;/TR&gt;\n";
    }
    echo 
    "&lt;/TABLE&gt;\n";
    ?>

    OK, let's take a look at the modifications one by one.

    $rows = ceil($num_rows / $columns);

    We have set a new variable called $rows. We get this value from the number of rows in our data set divided by the number of columns we want. The ceil function rounds that number up to the next whole number.

    while($row = mysql_fetch_array($result)) {
        $data[] = $row['stuff'];
    }

    Next we take all the data from the result set and put it in an array.

    for($i = 0; $i &lt; $rows; $i++) {

    Or for loop condition changed to only loop for the number of rows we will need.

    for($j = 0; $j &lt; $columns; $j++) {
        if(isset($data[$i + ($j * $rows)])) {
            echo "&lt;TD&gt;" . $data[$i + ($j * $rows)] . "&lt;/TD&gt;\n";
        }
    }

    Here is what does the magic. What we are doing here is running a for loop for the amount of columns we want. Then we take which column we are on (0, 1, or 2) and multiply that times the amount of rows in our table. Then we add that the the row we are on to get which piece of data to display.

    As you can see, we no longer need to check whether or not to display the start and end of a row, we always do as the outer for loop is on a per row basis.

    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-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT