Jump to content

Creating a drop-down box to filter query results in a table.


brendanm

Recommended Posts

I'm brand new to PHP and have successfully created an SQL table, connected to it and then retrieved the data. Now I've got all my data back from the table using a select query, I want to be able to alter what is displayed.

 

So, say I have a table with three fields: Name, Age and Nationality.

When I query the table, I can retrieve all the data using SELECT * FROM tablename

 

Now I want to filter those results to display only British people (from the nationality) field.

I understand that I must use the WHERE function. No problem. I can query the table and retrieve just the British people.

 

But how do I install a drop-down box on the page that allows me flick between various filter options. Is there an array or variable or something? I was wondering if there was a tutorial or some sample code which allowed a developer to filter in this way. Any advice for a noob greatly appreciated!

I hope this is write...

 

# create drop-down box

<form action="action_page.php" method="post">

$query = mysql_query("select id, Nationality  from tbl_name");
while($row = mysql_fetch_array($query)):
$dropdown = "<select name=\"dp_name\"><option value=".$row['id'].">".$row['Nationality']."</option></select>";
endwhile;
print $dropdown;

 

# action page
$query = mysql_query("select *  from tbl_name WHERE Nationality = '{$_POST['dp_name']}'");
while($row = mysql_fetch_array($query)):
# show record of selected query....
endwhile;

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.