Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-12-2009, 09:40 PM   PM User | #1
mikemacx
New Coder

 
Join Date: Feb 2009
Location: Los Angeles
Posts: 40
Thanks: 2
Thanked 1 Time in 1 Post
mikemacx is an unknown quantity at this point
PHP grouped output assistance

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!

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!

Last edited by mikemacx; 03-12-2009 at 11:16 PM..
mikemacx is offline   Reply With Quote
Old 03-12-2009, 10:21 PM   PM User | #2
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
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:

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

__________________
Fumigator is offline   Reply With Quote
Old 03-14-2009, 02:06 AM   PM User | #3
mikemacx
New Coder

 
Join Date: Feb 2009
Location: Los Angeles
Posts: 40
Thanks: 2
Thanked 1 Time in 1 Post
mikemacx is an unknown quantity at this point
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:

PHP Code:
   <tr>

<?php
$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>";
 

 
$row_count++; 
}
 
?>
            </tr>
RESULT:



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

PHP Code:
$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'] ; 
}

            
?> 
RESULT:


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?
mikemacx is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:37 AM.


Advertisement
Log in to turn off these ads.