Jump to content

Archived

This topic is now archived and is closed to further replies.

Andrew R

Creating a dropdown menu filter?

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

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

Cheers

Share this post


Link to post
Share on other sites
OK I got ya

you can do this and let them sort asc and desc also
[code]<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";[/code]

Share this post


Link to post
Share on other sites
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.

[code]$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);[/code]

Share this post


Link to post
Share on other sites
[quote author=Andrew R link=topic=104784.msg418299#msg418299 date=1155940251]
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.

[code]$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);[/code]
[/quote]

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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.