View Full Version : PHP grouped output assistance

03-12-2009, 09:40 PM
I'm very new to MYSQL and PHP, but thanks to these forums, I'm advancing quite well. Glad i found this forum!

Big thanks to user aedrin for showing an example of column output! :thumbsup:

I have a general question. I'm trying to list a page of art galleries in one state.My MYSQL table consists of (gallery_id(Primary), city_name , gallery_name, and web_link).

I've gotten as far as listing all of them in separate columns, but they are all together. I'm not asking for code, rather, I'm asking for someone to lead me in the direction of the subject I should be studying to output the array in groups by city_name. From my HTML site, here is visual of lists of two different types on the same page depending on how large the city galleries are in number:

A typical table row is structures as:

gallery_id = 1 city_name = Los Angeles gallery_name = acuna-hansen gallery web_link = http://www.ahgallery.com/ state = California country = United States continent = North America

Major City:

And then grouped cities in other places:

Where do I begin to find my answer? Is this a MYSQL query format, a PHP output format, or both? Is this duel output format possible on one page using PHP?

Big up to the Coding Forums! :D

03-12-2009, 10:21 PM
The way I would do it is sort the results by city first in the query, so each city's images are grouped together. Then as you loop through the results, use a variable to save off the city to a variable. Use this save-off variable to see if the city has changed, and when it does change, generate the code that ends one group of images and begins another.

This is kind-of pseudo-code:

//initialize save-off variable
$previousCity = "";
//loop through images
foreach ($images as $img) {
if ($img['city'] != $previousCity) {
$previousCity = $img['city'];

03-14-2009, 02:06 AM
I'm just inches from making this work! . Thanks for that code, but I'm still stuck at a certain point.

I have two if statements codes that do just what I want. The first one breaks the data into three columns, but lumped together without city_name headers:


$row_count = 0;
$columns = 3;

while ($row = @ mysql_fetch_assoc($result))

if ($row_count == $columns) {
echo "</tr><tr>";
$row_count = 0; }

echo "<td width=30%><a href=\"{$row["web_link"]}\" target=_blank>{$row["gallery_name"]}</a></td>";



This statement will save-off the city_name headers and only list them once until a new city_name:

$prev_art='xxxxxxxx'; //a dummy to display the first artist

while ($row = @ mysql_fetch_assoc($result))

if ($prev_art != $row['city_name']){
echo "\t<tr><td colspan=3><br><b><font size=5><Font color= \"#400040\">{$row["city_name"]}</font></b><HR color= \"#400040\"></td></tr>\n";

echo "<tr><td width=30%><a href=\"{$row["web_link"]}\" target=_blank>{$row["gallery_name"]}</a></td></tr>";

$prev_art = $row['city_name'] ;



Unfortunately, I can't seem to get them to work together at the same time. I tried making them into two separate WHILE...LOOP statements, but I just got the headers and nothing else.
What is the magic glue to make these two play nicely together?