Jump to content

Creating a dropdown menu filter?


Andrew R

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
Link to comment
https://forums.phpfreaks.com/topic/17971-creating-a-dropdown-menu-filter/
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
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]
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 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";

Archived

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

×
×
  • Create New...

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.