Jump to content

[SOLVED] Clever way to make a dropdown-menu filter?


katie77

Recommended Posts

Hi there,

 

I have a dropdown menu which is supposed to filter a table to show results for professionals, students or all.

But I don't really know what I'm doing.  This is what I have so far. 

 


if (isset($_GET['occupation'])) {
if ($_GET['occupation'] == "all")
{
       $filter = $_GET['occupation'];
} 
        else 
{
	$filter = "'Student' or occupation = 'professional'";
}
}
else 
{
      $filter = "'Student' or occupation = 'professional'";
}

//query table

$result = mysql_query
              ("select * from software 
		WHERE occupation = '$filter'
		ORDER by id DESC ");								

//drop-down menu		
echo "
<form method='get' enctype='text/plain'>
<select size='1' name='occupation'>
                <option value='All'>All</a></option>
	<option value='Student'>Students</a></option>
	<option value='Professional'>Professional</option>
 </select>
        <input type='submit' name='submit' value='Filter'> 
</form>";

 

 

Initially, I was able to filter for students or professionals, but since trying to have an all option (students and professionals) it has gone to pot, and I keep getting errors.  I know that my quotation marks are not right, but I can't think of a solution. I would be more than happy to see any ideas, even if they are completely different to mine!

 

Can anyone help me out?

 

Many thanks,

 

Katie

Link to comment
Share on other sites

if it's "All", omit the WHERE

<?php
if (isset($_GET['occupation'])) {
    if ($_GET['occupation'] == "All")
    {
        $filter = "";
    } 
    else 
    {
        $filter = "WHERE occupation = {$_GET['occupation']}";
    }
}
else 
{
$filter = "";
}

//query table

$result = mysql_query
              ("select * from software 
		$filter
		ORDER by id DESC ");								

//drop-down menu		
echo "
<form method='get' enctype='text/plain'>
<select size='1' name='occupation'>
                <option value='All'>All</a></option>
	<option value='Student'>Students</a></option>
	<option value='Professional'>Professional</option>
 </select>
        <input type='submit' name='submit' value='Filter'> 
</form>";

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.