Home arrow MySQL arrow Page 5 - Displaying Multiple Records Per Row in a MySQL Query Result Set

The Look we want and the Secret that gets you there - MySQL

Ever wonder how you can query a database and display the result set in something other than a one record per row layout? Keeping in mind that the simple answer is always the best one, I found a solution that keeps to that premise and solves an issue that seemed impossible not long ago. In a word, the answer, lies in the loop. An additional one, that is. But first, lets define and explain the general look and functionality of our project.

TABLE OF CONTENTS:
  1. Displaying Multiple Records Per Row in a MySQL Query Result Set
  2. Getting Connected
  3. Digging Deeper
  4. Getting the Query Results to Display Horizontally
  5. The Look we want and the Secret that gets you there
By: Peter Cole
Rating: starstarstarstarstar / 47
May 19, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Between the header and footer in show_pictures_h.php, the php code and html shown above will parse to look like this:

Records in MySQL

The trick, discovered only after repeated dead ends is the nested loop. The first for loop finds out how many rows to display and prints the <tr> tags. The second nested loop finds out how many <td> tags to print in each row. And because we already defined this to equal to 5, the inside loop prints out 5 table cells before giving control back over to the outside loop which then prints another row (if need be) and then lets the inside loop do its thing again until both loops are satisfied and no longer return a true value.

Because the data we want to display resides in the table cells, we don't break out the array values that contain all that data until we are inside the 2nd loop.

Cha ching! In our case we only wanted the thumbnail image and the caption. Each thumbnail however is surrounded with a hyperlink that requests something along these lines:

http://localhost/gallery/enlarge_image.php?ID=22&CAT=3&AID=2

Clicking on the thumbnail image opens another page with a larger version of the image. Nothing special here... just another query that displays the results of the SELECT string parameters (imageID, catalogID and albumID) attached to the thumbnail hyperlink. The code behind the enlarge_image.php page is almost identical to what we have looked at here. The only difference is in the html and the CSS used to display the one large image rather than loop thru an array of returned rows.

Records in MySQL

You may see all of this in action at Mooney Mountain Guides Please be aware that all mountaineering images displayed here are © 2004 by Art Mooney.

Hope this helps you with your PHP/MySQL projects and remember, keep it simple.



 
 
>>> More MySQL Articles          >>> More By Peter Cole
 

blog comments powered by Disqus
   

MYSQL ARTICLES

- Xeround Releases Free Version of MySQL Cloud...
- Oracle Announces New MySQL Specialization
- Constant Contact Chooses SkySQL for MySQL Su...
- Revoke Statement in MySQL
- The Grant Statement in MySQL
- SuccessBricks Announces ClearDB Availability...
- Building a PHP ORM: Deploying a Blog
- TROSYS Launches Free MySQL Manager and Admin...
- Building an ORM in PHP: Domain Modeling
- Building an ORM in PHP
- MySQL Leads Open Source Market, Gets Cluster...
- Oracle Announces Milestone Release for MySQL
- How to Stop SQL Injection Attacks
- New Defragmentation Solution for SQL Server
- Comparison of MyISAM and InnoDB MySQL Databa...


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

Dev Shed Tutorial Topics: