Development Articles
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOtherDevelopment Articles

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 19th, 2004, 10:57 AM
Admin Admin is offline
Developer Shed
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Posts: 961 Admin User rank is Sergeant Major (2000 - 5000 Reputation Level)Admin User rank is Sergeant Major (2000 - 5000 Reputation Level)Admin User rank is Sergeant Major (2000 - 5000 Reputation Level)Admin User rank is Sergeant Major (2000 - 5000 Reputation Level)Admin User rank is Sergeant Major (2000 - 5000 Reputation Level)Admin User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Article Discussion: Displaying Multiple Records Per Row in a MySQL Query Result Set

Ever wonder how you can query a database and display the result set in something other than a one record per row layout? I've been trying to figure that out for some time and to date had not been able to find the answer courtesy of someone else's hard work. 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. The project I was working on for a client dealt with a photo database that involved four related tables, the schema for which follows in a dump from the popular open source mysql manager phpMyAdmin: I've added a bit of extra comment to each table to give you an idea what each table is designed to do.


Read the full article here: Displaying Multiple Records Per Row in a MySQL Query Result Set

Reply With Quote
  #2  
Old May 19th, 2004, 11:08 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 18,581 r937 User rank is General 2nd Grade (Above 100000 Reputation Level)r937 User rank is General 2nd Grade (Above 100000 Reputation Level)r937 User rank is General 2nd Grade (Above 100000 Reputation Level)r937 User rank is General 2nd Grade (Above 100000 Reputation Level)r937 User rank is General 2nd Grade (Above 100000 Reputation Level)r937 User rank is General 2nd Grade (Above 100000 Reputation Level)r937 User rank is General 2nd Grade (Above 100000 Reputation Level)r937 User rank is General 2nd Grade (Above 100000 Reputation Level)r937 User rank is General 2nd Grade (Above 100000 Reputation Level)r937 User rank is General 2nd Grade (Above 100000 Reputation Level)r937 User rank is General 2nd Grade (Above 100000 Reputation Level)r937 User rank is General 2nd Grade (Above 100000 Reputation Level)r937 User rank is General 2nd Grade (Above 100000 Reputation Level)r937 User rank is General 2nd Grade (Above 100000 Reputation Level)r937 User rank is General 2nd Grade (Above 100000 Reputation Level)r937 User rank is General 2nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 1 Day 38 m 54 sec
Reputation Power: 1100
nicely written article

i don't do php so i can't comment on that part of it (except to note in passing that nested looping in coldfusion is trivial compared to the code in this article)

the sql looks fine

i really have to wonder about the use of an html TABLE to display the results

the article ends with ".... and remember, keep it simple"

i totally agree with this

use css to float the images and you can ditch the entire TABLE

not only will that allow the page to be liquid, but it's way simpler html, to say nothing of the effect it would have on the php...
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon

Reply With Quote
  #3  
Old May 24th, 2004, 11:54 AM
vbraca vbraca is offline
Web/DB *****/Designer
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: Serbia
Posts: 1 vbraca User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 m 7 sec
Reputation Power: 0
Send a message via ICQ to vbraca Send a message via MSN to vbraca Send a message via Yahoo to vbraca
Few days ago I've been facing a similar chalange from wife. She had a task to create a simple web photo album with categories and ultimately she wanted her categories and thumbnails displayed in two columns.
Took me an hour or so to find a solution by using modulus (%) operator combined with If statement.
Solution was much simpler than this one - if mysql_query row divided by number of columns gives modules different than 0 I would simply add HTML code for new column and insert record data, otherwise I would do the same but this time I would also add HTML row close tag and start a new row.
Anyway, though a little bit complicated your solution seems nice to me only that it was achieved with different means.

Reply With Quote
  #4  
Old May 26th, 2004, 07:29 AM
neutcomp neutcomp is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: The Netherlands
Posts: 10 neutcomp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to neutcomp
Hum and now!

I have made 5 files and the db_config.php

index.php, index2.php, image_gallery.php, show_picture_h.php and show_picture_v.php

index.php = with the show_picture_h and _v
index2.php = with the enlarge_image.php
image_gallery =
Code:
<?php
	require("includes/db_config.php");

	if(isset($_GET['CAT'])) {
		// clean up query string
		$CAT = htmlspecialchars($_GET['CAT']);  
		
		// get image count in each album in the chosen category
		$sql ="SELECT categories.catNAME, albums.catID, albums.locID, albums.albumID, albums.albumNAME, albums.albumPIC, images.thumbPATH, location.locNAME, COUNT(images.albumID) AS NUM FROM categories, albums, images, location WHERE images.catID = '$CAT' AND images.albumID = albums.albumID AND albums.locID = location.locID AND categories.catID = '$CAT' GROUP BY albums.albumNAME ORDER BY albums.albumNAME ASC";  

		$result = @mysql_query($sql, $connection);
	}
?>

show_picture_h.php = with the htmlspecialchars
show_picture_v.php = little code

Code:
<?
	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;';
	}
?>


But I mis the enlarge_image.php version! Or am I making a mistake? And what file must be loaded first (The gallery version)? Where does the images go (/images/thumb, images/gallery).

Thanxx
Bjorn

neutcomp@hotmail.com (MSN) If thats easier

Reply With Quote
  #5  
Old May 27th, 2004, 03:51 PM
Bergius Bergius is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 5 Bergius User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Smile Another way

Uhm, I didn't look at this article too closely, but for what I saw, I felt that there's an easier way to do it, namely:

Code:
<?php
    $pics = array('some kind of picture array' => 'filename.jpg', [etc...]);
    echo '<table><tr>';
    foreach ($pics as $label => $file) {
        echo '<td>'.$label.'<br/><img src="'.$file.'"/></td>';
        if ($i++%5 == 4 && count($pics) > $i) $output[] = '</tr><tr>';
    }
    
    echo '</tr></table>';
?>


... where the 5 in %5 is how many columns you want. You'd have to equip this with bells and whistles (and the database-to-array stuff, but that's not more than half a dozen additional rows of PHP code), but the columns thingy (if I've understood the topic of the article correctly) shouldn't be harder than this (and putting a colspan in the last td/filling with empty tds).

Did I miss something? :) If so, please excuse a first-time poster.

Last edited by Bergius : May 27th, 2004 at 03:56 PM. Reason: A bug or two ;)

Reply With Quote
  #6  
Old June 15th, 2004, 01:13 PM
pcole8787 pcole8787 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Posts: 8 pcole8787 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 26 m 23 sec
Reputation Power: 0
Quote:
Originally Posted by neutcomp
I have made 5 files and the db_config.php

index.php, index2.php, image_gallery.php, show_picture_h.php and show_picture_v.php

index.php = with the show_picture_h and _v
index2.php = with the enlarge_image.php
image_gallery =
Code:
<?php
	require("includes/db_config.php");

	if(isset($_GET['CAT'])) {
		// clean up query string
		$CAT = htmlspecialchars($_GET['CAT']);  
		
		// get image count in each album in the chosen category
		$sql ="SELECT categories.catNAME, albums.catID, albums.locID, albums.albumID, albums.albumNAME, albums.albumPIC, images.thumbPATH, location.locNAME, COUNT(images.albumID) AS NUM FROM categories, albums, images, location WHERE images.catID = '$CAT' AND images.albumID = albums.albumID AND albums.locID = location.locID AND categories.catID = '$CAT' GROUP BY albums.albumNAME ORDER BY albums.albumNAME ASC";  

		$result = @mysql_query($sql, $connection);
	}
?>

show_picture_h.php = with the htmlspecialchars
show_picture_v.php = little code

Code:
<?
	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;';
	}
?>


But I mis the enlarge_image.php version! Or am I making a mistake? And what file must be loaded first (The gallery version)? Where does the images go (/images/thumb, images/gallery).

Thanxx
Bjorn

neutcomp@hotmail.com (MSN) If thats easier


Sorry to not have included the necessary code for the enlarge_image.php, the nature of the article was the the multiple records per row. Either way, here's the code for the popup page.

Code:
<?php 
require("../../includes/db_config.php");
if(isset($_GET['CAT']) && ($_GET['AID'])) {
	$CAT = htmlspecialchars($_GET['CAT']);  // clean up query string
	$AID = htmlspecialchars($_GET['AID']);  // clean up query string
	$imgID =  htmlspecialchars($_GET['ID']);  // clean up query string

	$sqlP = "
		SELECT images.imgID, images.locID, images.imgTITLE, images.popPATH, images.popNAME, images.copyright, albums.albumNAME 
		FROM albums, images 
		WHERE images.imgID = '$_GET[ID]' AND albums.albumID = images.albumID "; 
	$result = @mysql_query($sqlP, $connection) or die ("Could not execute your query");
	$check_num=@mysql_num_rows($result);	
?>

that's your header before the html and body tags.
Then using your layout you'll need this code to display the enlarged image.

Code:
			<?php
			if(!$check_num) {
				echo "The image you requested is unavailable";
				}
			else {
				while ($row = mysql_fetch_array($result)) {
					$imgID = $row['imgID'];
					$location = $row['locID'];
					$albumNAME = stripslashes($row['albumNAME']);
					$caption = stripslashes($row['imgTITLE']);
					$path = $row['popPATH'];
					$image = $row['popNAME'];
					$copyright = $row['copyright']; 
	echo "<tr><td align=\"center\" bgcolor=\"#CFC9B4\"><a href=\"javascript:history.go(-1);\" class=\"smtext\">Back</a>&nbsp;&nbsp;&nbsp;&nbsp;<strong>$albumNAME</strong></td></tr>";		
	echo "<tr><td align=\"center\" bgcolor=\"#000000\" bgcolor=\"#000000\"><img src=\"../../$path/$location/$image\" border=\"0\" vspace=\"5\" hspace=\"5\"></td></tr>";
	echo "<tr><td align=\"center\" bgcolor=\"#CFC9B4\">$caption</td></tr>";
	echo "<tr><td class=\"smtext\" align=\"center\" bgcolor=\"#CFC9B4\">Copyright © $copyright ~ Your Name ~ All Rights Reserved</td></tr>";	
		
		}
	}		
}	

			?>

Then comes the rest of your layout and the closing body and html tags. Hope that helps
Peter

Reply With Quote
Reply

Viewing: Dev Shed ForumsOtherDevelopment Articles > Article Discussion: Displaying Multiple Records Per Row in a MySQL Query Result Set


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





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