Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Feb 2009
    Location
    Los Angeles
    Posts
    43
    Thanks
    2
    Thanked 1 Time in 1 Post

    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-13-2009 at 12:16 AM.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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'];


  • #3
    New Coder
    Join Date
    Feb 2009
    Location
    Los Angeles
    Posts
    43
    Thanks
    2
    Thanked 1 Time in 1 Post
    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?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •