Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2003
    Posts
    225

    Unanswered: Filter by Drop down

    I have been working on my web page and thanks to a great deal of help from this site i really am getting somewhere...

    page is here:

    http://www.chimp.cc/Players/

    i was hoping to be able to filter via a drop down box, the players based on position,

    Example:

    All Positions
    Goalkeeper
    Defender
    Midfieder
    Striker

    as the user changes the info in the drop down box the page would update. just containing the info based on the result.

    if all positions was selected obviously i would want all players displayed..

    Can this be done?
    if so how please?

    current page

    PHP Code:
    <html>
    <title>RTFC Players 2006/07</title>
    <body bgcolor="#ffffff"><font color="#66CC66">
    <?php
    $username 
    "root";
    $password "";
    $database "Players";
    $db mysql_connect(localhost$username$password) or die(mysql_error());
    mysql_select_db($database$db) or die("Unable to select database");
    $query "SELECT Forename, Surname, DOB, Nickname, Photo, RegNo, Comments, Position, GamesPlayed, GoalsScored FROM players";
    $result mysql_query($query$db);

    echo 
    '<h1 style="text-align: center; font-weight: bold;">Season 2006/07 Squad</h1><br/><br/>';

    while (
    $row mysql_fetch_assoc($result)) {
      foreach (
    $row as $key => $value) {
        $
    $key htmlspecialchars($value);
      }

      echo 
    '<table cellspacing="3" cellpadding="5" align="center" style="border:3px solid #66CC66; width: 70%;">';
      echo 
    "<tr><td><b>Name:</b></td><td>$Forename $Surname</td></tr>";
      echo 
    "<tr><td><b>Date Of Birth</b></td><td>$DOB</td></tr>";
      echo 
    '<tr><td><b>Photo</b></td><td><img src="';
      echo (
    trim($Photo) != "") ? $Photo "http://uxellaleague.com/images/teams_200506/awaiting_teampic.gif";
      echo 
    '" border="0" alt="Select this for a more detailed image"></td></tr>';
      echo 
    "<tr><td><b>Reg No</b></td><td>$RegNo</td></tr>";
      echo 
    "<tr><td><b>Comments</b></td><td>$Comments</td></tr>";
      echo 
    "<tr><td><b>Position</b></td><td>$Position</td></tr>";
      echo 
    "<tr><td><b>Games Played</b></td><td>$GamesPlayed</td></tr>";
      echo 
    "<tr><td><b>Goals Scored</b></td><td>$GoalsScored</td></tr>";
      echo 
    "</table><br/><br/>";
    }
    mysql_close($db);
    ?>
    </html>
    Cheers

    Andy

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    yes it can be done, but you need to decide if your user base will allow that fucntionality on their website

    you can achieve this using a fragment of Javascript to trigger a page refresh, or you cna use a bit more Javascript and an AJAX call back. The easiest is going to be a simple javascript function trigered by a change
    I'd rather be riding on the Tiger 800 or the Norton

  3. #3
    Join Date
    Feb 2004
    Posts
    533
    Andy,

    The key to this is changing the SQL query based on the user selection of a list to return the desired records. How the list is activated and requeries the data is a secondary consideration.

    In this example I modified your code. I used a submit button to send the form data and refresh the URL. The $where scaler is either Null for 'All Positions' selection (where statement not required to return all records) or a where clause is defined to return the records for a selected position. I also created an array to contain the list values and a function to manage the list construction.

    The submit button should be used if you think you will add more lists for criteria. Then the user can set multi criteria then click the button to refresh the screen.

    I used the Get method of the form to show the form values in the URL. You likely will want to change this to the Post Method for the public version. Use $_POST('selposition') to catch the form Post Values after changing to the Post method.


    PHP Code:
    <html> 
    <title>RTFC Players 2006/07</title> 

    <body bgcolor="#ffffff"><font color="#66CC66"> 
    <form method="GET" target="_self">
    <?php 
    $array 
    = array(
    array(
    "id" => "All Positions""value" =>"'All Positions'""display_value" =>"All Positions"),
    array(
    "id" => "Goalkeeper""value"=> "Goalkeeper""display_value" => "Goalkeeper"),
    array(
    "id" => "Defender""value"=> "Defender""display_value" => "Defender"),
    array(
    "id" => "Midfielder""value"=> "Midfielder""display_value" => "Midfielder"),
    array(
    "id" => "Striker""value"=> "Striker""display_value" => "Striker")
    ); 

    $name "selposition";
    $selected = !isset($_GET['selposition'])? $array[0]['id']: $_GET['selposition']; 

    print 
    insertListBox($array,$name,$selected);
    ?>
    &nbsp;<input type="submit" name="submit" value="Submit" />
    </form>

    <?php 

    $username 
    ""
    $password ""
    $database "Players"
    $db mysql_connect(localhost$username$password) or die(mysql_error()); 
    mysql_select_
    db
    ($database$db) or die("Unable to select database"); 

    $where = !$selected == $array[0]['id']? null" WHERE Position = '$selected'";
    // $query = "SELECT Forename, Surname, DOB, Nickname, Photo, RegNo, Comments, Position, GamesPlayed, GoalsScored FROM players".$where; 
    $result mysql_query($query$db); 
    echo 
    '<h1 style="text-align: center; font-weight: bold;">Season 2006/07 Squad</h1><br/><br/>'
    echo 
    "<p>".$query."</p>";
    while (
    $row mysql_fetch_assoc($result)) { 
      foreach (
    $row as $key => $value) { 
        $
    $key htmlspecialchars($value); 
      } 

      echo 
    '<table cellspacing="3" cellpadding="5" align="center" style="border:3px solid #66CC66; width: 70%;">'
      echo 
    "<tr><td><b>Name:</b></td><td>$Forename $Surname</td></tr>"
      echo 
    "<tr><td><b>Date Of Birth</b></td><td>$DOB</td></tr>"
      echo 
    '<tr><td><b>Photo</b></td><td><img src="'
      echo (
    trim($Photo) != "") ? $Photo "http://uxellaleague.com/images/teams_200506/awaiting_teampic.gif"
      echo 
    '" border="0" alt="Select this for a more detailed image"></td></tr>'
      echo 
    "<tr><td><b>Reg No</b></td><td>$RegNo</td></tr>"
      echo 
    "<tr><td><b>Comments</b></td><td>$Comments</td></tr>"
      echo 
    "<tr><td><b>Position</b></td><td>$Position</td></tr>"
      echo 
    "<tr><td><b>Games Played</b></td><td>$GamesPlayed</td></tr>"
      echo 
    "<tr><td><b>Goals Scored</b></td><td>$GoalsScored</td></tr>"
      echo 
    "</table><br/><br/>"

    mysql_close($db); 
    ?> 
    </html>



    <?php 
    function insertListBox($array,$name,$selected,$attribs=null,$events=null)
    {
            
    printf("<select name=$name $attribs $events>");
           
    // printf("<option value=>select something");
            
    for($i=0;$i<sizeof($array);$i++)
            {
                    
    $row $array[$i];
                    if(
    $row['id'] == $selected)
                    {
                            
    printf("<option value=$row[value] selected>$row[display_value]");
                    }
                    else
                    {
                            
    printf("<option value=$row[value]>$row[display_value]");
                    }
            }
            
    printf("</select>");
    }
    ?>
    ~

    Bill

Posting Permissions

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