limiting results returned

Discussion in 'PHP / Perl / Ruby on Rails' started by teej, Oct 17, 2008.

  1. teej CSNM Customer

    Hi - I have a problem the solution to which i think is in the php, but maybe in the sql.

    Here goes: I have 2 tables, one for users (names & id), one for photos (id, caption, filename), and I would like to display a list of users with a thumbnail for each. But I can't figure out how to limit the data returned to one row for each distinct user. The code below returns as many rows as there are filenames in the photos table.

    I'm thinking that LIMIT and DISTINCT are not going to do it for me and that I need to set some condition in the loop, but I can't see how.

    Code:
    $query =     "SELECT        first_name, last_name, user_id, filename
                FROM          Users AS u
                LEFT JOIN    Photos AS p
                ON              p.user = u.user_id
                WHERE        photosUploaded = 'Y'
                ORDER BY    last_name ASC
                ";                        
                                                
    $result = @mysql_query ($query); // run the query
    
    if ($result)
            {
            while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {               
    echo "<a href='galleryPage.php?pageID=" . $row['user_id'] . "'>
      " . $row['first_name'] . "&nbsp;" . $row['last_name'] . "<br />
      <img src='photos/" . $row['filename'] . "' /><br /></a>\n";
                            }
    mysql_free_result ($result);
            } else {
            echo '<p class="error">Current userList could not be retrieved</p>';
            }
    
    mysql_close();
    
    Can anyone help please?
  2. Andrew Taylor CS New Media Staff

    Can you stick it in your loop, query if the ID or whatever has been used before, if it has echo nothing, otherwise carry on?
  3. teej CSNM Customer

    OK - that's what I was thinking, but for some reason just couldn't see it. It's taken me ages, and I was feeling really stupid, but I think I've got it at last, certainly seems to work :thumbup1::

    Code:
    $query =     "SELECT     first_name, last_name, user_id, 
                            filename
                FROM        rjUsers AS u
                LEFT JOIN    rjPhotos AS p
                ON               p.user = u.user_id
                WHERE        photosUploaded = 'Y'
                ORDER BY    last_name ASC
                ";                        
                                                
    $result = @mysql_query ($query); // run the query
    
    if ($result)
        {
            while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) 
            {
            if ( $alreadySeen != $row['user_id'] )
                {
                echo"<a href='galleryPage.php?pageID=" . $row['user_id'] . "'>
                      " . $row['first_name'] . "&nbsp;" . $row['last_name'] . "<br />
                      <img src='photos/" . $row['filename'] . "' /><br /></a>\n";
                  ( $alreadySeen = $row['user_id'] );
                  } // end if
              else
                  {
                  echo '';
                  } // end else
              } //end while  
                            
     mysql_free_result ($result);
          } //end if
    
    Thanks for the direction.

Share This Page