SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot gregs's Avatar
    Join Date
    Aug 2002
    Posts
    161
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Reusing an Array

    PHP Code:
      $page .= "<select name=\"year\">\n<option selected value=\"".$year."\">".$year."</option>\n";
       
    $query "SELECT DISTINCT " .
               
    "year " .
               
    "FROM {{table}} " .
               
    "ORDER BY year ASC";
      
    $result doquery($query"teams");
      while (
    $row mysql_fetch_array($result)) { $page .= "<option value=\"".$row['year']."\">".$row['year']."</option>\n"; }
    $page .= <<<END
            </select>
          </td>
          <td>State: 
    END;
      
    $page .= "<select name=\"state\">\n<option selected value=\"".$state."\">".$state."</option>\n";
      
    $query "SELECT DISTINCT " .
               
    "state " .
               
    "FROM {{table}} " .
               
    "ORDER BY state ASC";
      
    $result doquery($query"teams");
      while (
    $row mysql_fetch_array($result)) { $page .= "<option value=\"".$row['state']."\">".$row['state']."</option>\n"; } 
    Should there be two queries like so in this? Could it just be made like:

    PHP Code:
      $query "SELECT DISTINCT " .
               
    "year, state " .
               
    "FROM {{table}} " .
               
    "ORDER BY year, state ASC"
    If so, how would you reset $result to use it twice?

  2. #2
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sounds like you need mysql_data_seek()

  3. #3
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    I'm confused. You've got year, and state, in the same table, but they're not related to each other? Sounds like you need a new table, to me.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  4. #4
    SitePoint Zealot gregs's Avatar
    Join Date
    Aug 2002
    Posts
    161
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    This is used in a FORM with dropdown boxes.
    The first time it runs $query, it finds all DISTINCT years within the table TEAMS.
    The second time $query runs, it is finding all the DISTINCT states within the table TEAMS.
    Each record has: id, teamname, year, state, sport, the last four making it unique to all other records.
    It seems that I should be able to just gather all the DISTINCT years and states with only one Query and then use $result twice,once to load and show years within its dropdown box and then again to show states in its own dropdown box.

    Sorry if I have trouble explaining things.

  5. #5
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Did you read the link I posted?

    All you need to do is run a single query that collects all the info you need from the db and then use mysql_data_seek() to reset the result set's pointer each time you want to reuse the result set.

  6. #6
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,032
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    Are both queries accessing the same table?
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator


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
  •