Database Articles

  Home arrow Database Articles arrow Page 4 - 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 - Something to think about


    (Page 4 of 4 )

    Shortly after posting this tutorial, I was asked the question "What if I want to print out more than one piece of data in these multiple columns?" Well, here's the same code from the last page but modified to print more than one piece of data.

    <?php
    $columns 
    4;

    mysql_connect('localhost','root','LRMSys2k');
    mysql_select_db('test');

    //change the query to get another field from the database
    $query "SELECT stuff, morestuff FROM mystuff ORDER BY stuff";
    $result mysql_query($query);

    $num_rows mysql_num_rows($result);


    $rows ceil($num_rows $columns);

    while(
    $row mysql_fetch_array($result)) {
        
    $data[] = $row['stuff'];
        
        
    //store the other field into an array
        
    $data2[] = $row['morestuff'];
    }

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

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

        echo 
    "&lt;TR&gt;\n";
        
        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 out the field
                
    echo "&lt;TD&gt;" $data2[$i + ($j $rows)] . "&lt;/TD&gt;\n";
            }
        }
        echo 
    "&lt;/TR&gt;\n";
    }
    echo 
    "&lt;/TABLE&gt;\n";
    ?>

    As you can see, there was very little modification. Just need to store the field in an array also, and then echo it out.

    Well, that's it. I hoped you learned something and can put it to good use!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.
    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 5 - Follow our Sitemap