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
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
Link to comment
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]
Link to comment
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]
Link to comment
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";
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.