Jump to content


Photo

Creating a dropdown menu filter?


  • Please log in to reply
6 replies to this topic

#1 Andrew R

Andrew R
  • Members
  • PipPipPip
  • Advanced Member
  • 158 posts
  • LocationIreland

Posted 18 August 2006 - 06:04 PM

Hi

I need some help on creating a drop down menu filter that will filter the page, (roster.php) by location and age.  I know how to create a filter using a form that will show the results on a secondary page such as viewroster.php but what I really wanted is for the page to be filtered without having to create a secondary page such as viewroster.php. 

Thanks

#2 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 18 August 2006 - 06:13 PM

Not sure I get what you are saying. Can you tell me what the form will look like and what you want it to do.

Ray

#3 Andrew R

Andrew R
  • Members
  • PipPipPip
  • Advanced Member
  • 158 posts
  • LocationIreland

Posted 18 August 2006 - 06:43 PM

Yes.  Ok I have a page (roster.php) that displays a list of users.  An SQL query pulls information such as name, id, location, age etc from the database and displays it on (roster.php).  What I want to do is create a drop down menu filter that will filter the results on the page by location and age. For example when you select "Age" from the drop down menu it will filter the page so it displays the users in the database by age ascending on roster.php

<select name="select">
  <option value="Age" selected>Age</option>
  <option value="Location">Location</option>
</select>

Cheers

#4 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 18 August 2006 - 07:06 PM

OK I got ya

you can do this and let them sort asc and desc also
<form name=form1 method=GET action="<?=$_SERVER['PHP_SELF']?>">
  <select name="select">
  <option value="Age" selected>Age</option>
  <option value="Location">Location</option>
</select>
  <select name="order">
    <option value="ASC">ASC</option>
    <option value="DESC">DESC</option>
  </select><br />
  <input type=submit value=Sort name=submit />
</form>
<?
if(isset($_GET['select']) && isset($_GET['order'])){
$filter = $_GET['select'];
$order = $_GET['order'];
} else {
$filter = "id";
$order = "ASC";
}
// SQL will look like this
  $sql = "SELECT * FROM table_name ORDER BY $filter $order";


#5 Andrew R

Andrew R
  • Members
  • PipPipPip
  • Advanced Member
  • 158 posts
  • LocationIreland

Posted 18 August 2006 - 10:30 PM

Thank you very much craygo. I have one more question.  How would I add the sql query into an existing sql query (below).  The reason I want to do this is because I have a pagination set-up so if I want the filter to work it needs to go through the pagination sql.

$query_users = "SELECT * FROM users ORDER by `id`LIMIT $start, $limit";
$users = mysql_query($query_users, $conn_abrv) or die(mysql_error());
$row_users = mysql_fetch_assoc($users);
$totalRows_users = mysql_num_rows($users);


#6 Woolf

Woolf
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 19 August 2006 - 12:07 AM

Thank you very much craygo. I have one more question.  How would I add the sql query into an existing sql query (below).  The reason I want to do this is because I have a pagination set-up so if I want the filter to work it needs to go through the pagination sql.

$query_users = "SELECT * FROM users ORDER by `id`LIMIT $start, $limit";
$users = mysql_query($query_users, $conn_abrv) or die(mysql_error());
$row_users = mysql_fetch_assoc($users);
$totalRows_users = mysql_num_rows($users);


$query_users = "SELECT * FROM users ORDER BY $filter $order LIMIT $start, $limit";

#7 Andrew R

Andrew R
  • Members
  • PipPipPip
  • Advanced Member
  • 158 posts
  • LocationIreland

Posted 19 August 2006 - 10:04 PM

Cheers




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users