SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2007
    Location
    Oslo, Norway
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    While loop not working

    Hi folks,

    I have been struggling with this code a while. The code lists artists and curators for exhibits for a specific gallery (GET $id), but I'm having trouble grouping the artists. It works almost fine when there are no curators attached to the exhibit, but as soon as there are curators it mixes the output. Here is an example that shows what I mean: http://norwegianartyearbook.no/the_l...exh.php?id=190
    Artists from exhibits with curators are shown in bold.
    How can I group the list of artists so the curators will be shown at the end? I did try to make a loop inside the loop but with no luck.

    Appriciate any help. Thanks!

    Here is the code:
    PHP Code:
    $result_array = array();
    $counter 0;
    $id $_GET['id'];

    // List of exhibits per gallery
    $row 1;
    $result mysql_query
        
    "SELECT p.id
            , p.title
            , p.info
            , p.galleryid
            , p.start_date
            , p.end_date
            , p.date
            , a.aname
            , a.exhibitid 
            , k.cname
            , k.exhibitid 
            FROM exhibit AS p
            LEFT JOIN artist AS a ON p.id=a.exhibitid
            LEFT JOIN curator AS k ON p.id=k.exhibitid
            WHERE p.galleryid='
    $id'
            ORDER BY p.start_date, a.id asc" 
    );
    $saveTitle '';   
    $saveArtist '';   
    $saveCurator '';   

    // initialize the variables used for checking any changes
    while ($list mysql_fetch_array($result)) { 
       
    $exhibit '';
       
    $artists '';
       
    $curators '';
      
    $title $list['title'];
      
    $info $list['info'];
      
    $date $list['date'];
      
    $art $list['aname'];
      
    $cur $list['cname'];
      
    $start $list['start_date'];
      
    $end $list['end_date'];
      
    $timestamp strtotime($start);
      
    $start date("d.m.y",$timestamp);
      
    $timestamp strtotime($end);
      
    setlocale(LC_TIME"no_NO.ISO_8859-1");
      
    $end date("d.m.y",$timestamp);

       
    // check if the exhibit name has changed. If it did, display the exhibit info and save the new exhibit name 
       
    if ($list['title'] != $saveTitle) {  
         
    // display exhibit info
         
    $exhibit "<span class='gallinfo'></p>$start-$end </span><span class='galltitle'>".$list['title']." </span><span class='gallinfo'><em>".$list['info']." </em></span><br /><span class='gallinfo'>Artists:</span>";  
         
    // save new exhibit name  
         
    $saveTitle $list['title'];
       } 
       if (
    $list['aname'] != $saveArtist) {  
       
    // display all artists  
       
    $artists "<span class='gallinfo'>".$list['aname'].",</span>"
         
    $saveArtist $list['aname'];
       }
       if (empty(
    $list['cname'])) {  
       
    $result_array[] = "$exhibit $artists"
       }
       if (
    $list['title'] != $saveTitle) {  
         
    // display exhibit info
         
    $exhibit "<span class='gallinfo'></p>$start-$end </span><span class='title'>".$list['title']." </span><span class='gallinfo'>".$list['info']." </span><br /><span class='gallinfo'>Artists:</span>";  
         
    // save new exhibit name  
         
    $saveTitle $list['title'];
       } 
       if (
    $list['aname'] != $saveArtist) {  
       
    // display all artists  
       
    $artists "<span class='gallinfo'>".$list['aname'].",</span>"
         
    $saveArtist $list['aname'];
       }
       if (
    $list['cname'] != $saveCurator) {  
       
    // display curators  
       
    $curators "<span class='gallinfo'>Curator: ".$list['cname'].",</span>"
         
    $saveCurator $list['cname'];
       }
       if (! empty(
    $list['cname'])) {  
       
    $result_array[] = "$exhibit <b>$artists</b> $curators"
    }
    }
    $result_final "<div id='gallList'>";
    foreach(
    $result_array as $galleries) {
      if(
    $counter == $row) {    
        
    $counter 1;
        
    $result_final .= "";
      }
      else 
    $counter++;
      
    $result_final .= "$galleries";
    }
    if(
    $counter) {
      if(
    $rows$result_final .= "$row-$counter";
      
    $result_final .= "</div>";
    }
    echo 
    $result_final

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    Well lets examine what you're actually pulling.

    What does a Row of your result represent? exhibit x artist x curator.

    Either the Artist or Curator or Both fields may be blank (LEFT JOIN).
    If both fields are NOT blank, make the Artist bold. (This requisite nullifies the ability to GROUP BY)
    Output: <Headers><Artist List><Curator List>

    PseudoCode:
    While Row = Fetch_Array
    If Row[exhibit] != curexhibit
    if curexhibit is not empty
    output curator_array
    empty curator_array
    endif
    output Headers
    endif
    if artist is not empty
    if curator is not empty
    echo bold artist name
    add curator name to curator_array
    else
    echo artist name
    endif
    endif
    endwhile

    This is of course assuming you intend for exhibit x artist x curator, and not exhibit x artist, exhibit x curator.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2007
    Location
    Oslo, Norway
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you,

    Is it a typo when you're saying "output_curator_array" in the first loop - you mean "output_artist_array", right?
    This loop is supposed to end when the curator array is empty and output exhibit details plus the artist list (not in bold to show first loop).

    The second loop is where the problem lays, I think. If neither artist nor curator are empty it's supposed to output both a list of artists and a list of curators. First the artists, then the curators.
    If I understand correct you're saying it only outputs one artist name (in bold), then the curator_array - and the loop continues?
    How can I continue the loop after the first artist to get a list of all artists, and then add the curator_array?

    All exhibits will have at least one artist but might not have any curators OR as many as wanted.

    Thank you for helping!

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    Nope.

    Programming Trick - just because something is at the top of a loop, doesnt mean it's executed first.
    There's only one loop. Because there's only one set of data.
    the first IF triggers whenever a new exhibit is detected. (you should be sorting your query on exhibit)
    The second IF triggers if this ISNT the first entry into the loop.
    I meant it exactly the way it's written, with one addition. It should have had an additional entry though.

    "output additional seperator" should come after empty curator_array.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  5. #5
    SitePoint Enthusiast
    Join Date
    May 2007
    Location
    Oslo, Norway
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you. I think I understand what you mean by "sort query by exhibit" but I don't know how to set up the first IF, Row[exhibit].
    And what's the difference between output and echo?

    The $result_final loop from my first code should be removed now since this loop will do the same, right?

    Here is my new code that show artist names and exhibit details (Header?) no curators result (still the same link as above):
    PHP Code:
    $result_array = array();
    $counter 0;

    // List of exhibits per gallery
    $row 1;
    $result mysql_query
        
    "SELECT p.id
            , p.title
            , p.info
            , p.galleryid
            , p.start_date
            , p.end_date
            , p.date
            , a.id
            , a.aname
            , a.exhibitid 
            , k.id
            , k.cname
            , k.exhibitid 
            FROM exhibit AS p
        LEFT JOIN artist AS a ON p.id=a.exhibitid
        LEFT JOIN curator AS k ON p.id=k.exhibitid
        WHERE p.galleryid='
    $id'
        ORDER BY p.start_date, a.id asc" 
    );
    $saveTitle '';   
    $saveArtist '';   
    $saveCurator '';   

    // initialize the variables used for checking any changes
    while ($list mysql_fetch_array($result)) { 
       
    $exhibit '';
       
    $artists '';
       
    $curators '';
      
    $title $list['title'];
      
    $info $list['info'];
      
    $date $list['date'];
      
    $art $list['aname'];
      
    $cur $list['cname'];
      
    $start $list['start_date'];
      
    $end $list['end_date'];
      
    $timestamp strtotime($start);
      
    $start date("d.m.y",$timestamp);
      
    $timestamp strtotime($end);
      
    setlocale(LC_TIME"no_NO.ISO_8859-1");
      
    $end date("d.m.y",$timestamp);
        
       
    // check if the exhibit name has changed. If it did, display the exhibit info and save the new exhibit name 
       
    if ($title != $saveTitle) {  
       
    // display exhibit info
       
    $exhibit "<span class='gallinfo'></p>$start-$end </span><span class='galltitle'>$title </span><span class='gallinfo'><em>$info </em></span><br /><span class='gallinfo'>Artists: </span>";  
       
    // save new exhibit name  
       
    $saveTitle $title;
       if (! empty(
    $exhibit)) {
       
    // display curators  
       
    echo $curators
       unset(
    $curators);
       }
       echo 
    $exhibit
    }
       if (! empty(
    $art)) {  
       if (! empty(
    $cur)) {  
         echo 
    "<b>$art, </b>";
       
    // display curators  
       
    $curators "<span class='gallinfo'>Curator: $cur,</span>"
       
    $saveCurator $cur;
       }
       else { 
       echo 
    "$art, ";
    }
    }



Bookmarks

Posting Permissions

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