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!

Link to comment
Share on other sites

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;

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.