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

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>";

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.