MySQL
  Home arrow MySQL arrow Page 4 - Displaying Multiple Records Per Row in...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
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? 
MYSQL

Displaying Multiple Records Per Row in a MySQL Query Result Set
By: Peter Cole
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 44
    2004-05-19

    Table of Contents:
  • Displaying Multiple Records Per Row in a MySQL Query Result Set
  • Getting Connected
  • Digging Deeper
  • Getting the Query Results to Display Horizontally
  • The Look we want and the Secret that gets you there

  • 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Displaying Multiple Records Per Row in a MySQL Query Result Set - Getting the Query Results to Display Horizontally


    (Page 4 of 5 )

    So now we get to the crux of our discussion. How do we get the query results to display horizontally?

    Good question and one that took a day of tinkering to figure out. There were a few things we did know. First off, we decided how many images per row we wished to see. That value is stored in the $thumbcols variable. By dividing the $num variable that the query returns (number of rows in the result set) by the value of $thumbcols, we get the number of rows the html will display. In this case we just happen to have 11 images in the 'Huntington Ravine' album and since $thumbcols == 5, we need 3 rows. But, because the division returns a number closer to 2 than 3, the PHP round function rounds down to 2. By adjusting the variable definition to add 1 to the round results, we get around the closer to 2 than 3 problem.

    But you say, what if the round function returns a whole number or a number closer to 3, won't there be an extra row added to the display? Not if we use a conditional to write out a table cell only if there is actually an image to display. This simple bit of code prints only what is needed to display all the images in the result set.

    if(!empty($image)) {
    // echo the actual data to the screen
    print
     "<a href="enlarge_image.php?ID=$ID&CAT=$catID&AID=$albumID">
     <img src="../../$path/$location/$image" border="0" alt="">
     </a>
     <br clear="all">
     $caption";
     }
    else {
     print '&nbsp;';
     }

    Simple enough. Then at last we come to the code needed to put a result set row into a table cell and then write out five table cells per html row. We'll start with a bit of fail safe code in case we find ourselves arriving at http://localhost/gallery/show_pictures_h.php without a query string attached to the URL.

    <table width="680" border="0" cellpadding="3" cellspacing="0">
    <tr>
    <td valign="top" align="center">
    <!-- gallery logo -->
    <img src="../../images/gallery/image_gallery_300x40.png" width="300" height="40" alt="">
    <br clear="all">
    <div style="margin-right:15px; margin-left:15px; padding:8px; line-height:120%;">
    <div align="center">
    <strong>

    <?php
    // just in case no query string is part of the URL request, the page will display an alert to pick a category
    // which is always displayed as part of the page header
    print
     (isset($NAME) ? $NAME. ' - (Click to Enlarge)' : 'Please select a category to view');
    ?>
       

    Records in MySQL

    </strong>
    </div>
    <?php
    print '<table width="650" border="0" cellpadding="5" cellspacing="0">';
    if(!empty($num)) {
     // if the query is successful print out a row of table beta
     print '
      <tr><td colspan="5" bgcolor="#cdcdcd">
       <strong>DATABASE RETURNED => # of Rows:'.$thumbrows. ' and # of Images:'.$num.'</strong>
       }
      </td>
     </tr>';
    // function to display multiple table cells before starting a new row
    function display_table() {
     // make variables available outside of the function
     global $num, $result, $thumbrows, $thumbcols;
     // format the number of rows to be printed using a loop
     for($r=1;$r<=$thumbrows;$r++) { 
     print '<tr>';
     // format the number of columns to be printed in each row using a 2nd for loop
     for($c=1;$c<=$thumbcols;$c++) {
     print '<td bgcolor="#999999" align="center" valign="top">';
     $row = @mysql_fetch_array($result); // break out the array of values from the returned query
      $ID = $row['imgID'];
      $catID = $row['catID'];
      $albumID = $row['albumID'];
      $location = $row['locID'];
      $albumNAME = $row['albumNAME'];
      $caption = stripslashes($row['imgTITLE']);
      $path = $row['thumbPATH'];
      $image = $row['thumbNAME'];
      $copyright = $row['copyright'];

    if(!empty($image)) {
     // echo the actual data to the screen
     print"
      <a href="enlarge_image.php?ID=$ID&CAT=$catID&AID=$albumID">
       <img src="../../$path/$location/$image" border="0" alt="">
      </a>
      <br clear="all">
      $caption";
      }
     else {
      print '&nbsp;';
     }
      print '</td>';
      }
     print '</tr>';
     }
    }
    display_table(); // call function to do this wonderful thing
    print '</table>';
    ?>
       </div>
       <br clear="all">
      </td>
     </tr>
    </table>
       

    More MySQL Articles
    More By Peter Cole


     

       

    MYSQL ARTICLES

    - MySQL Table Prefix Changer Tool in PHP
    - Using the SIGNAL Statement for Error Handling
    - Error Handling Examples
    - Error Handling
    - Completing a Search Engine with MySQL and PH...
    - Paginating Result Sets for a Search Engine B...
    - Building a Search Engine with MySQL and PHP 5
    - Using Boolean Operators for Full Text and Bo...
    - PHP, MySQL and the PEAR Database
    - Working with PHP and MySQL
    - Getting PHP to Talk to MySQL
    - Creating an RSS Reader: the Reader
    - MySQL Security Overview
    - Creating the Admin Script for a PHP/MySQL Bl...
    - Creating the Blog Script for a PHP/MySQL Blo...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway