Jump to content

PHP Dropdown | Filter results based on selection


webmaster1

Recommended Posts

Hi All,

 

I'm outputting the contents of a mySQL table into a web page using a simple SELECT * statement.

 

I want to be able to filter results by date and/or name so I presume I need a dropdown for each and one submit button.

 

From what I understand I need to do the following:

 

[*]Populate the distinct dates and names in their respective dropdowns

[*]Filter whats to be displayed depending on whats selected in the two dropdown menus

 

Can anyone point me in the right direction? When I search online I find help forums with partial snippets of code rather than any decent tutorials.

 

Heres the code I'm using that SELECTS *

 

<?php
session_start();

if (!$_SESSION['logged'])
{
    header("location: loginfl.php");
}
else
{
?>

<html>
<body>
<a href='admin.php'>Return to Administration Home</a>

<table border="1" cellspacing="2" cellpadding="2">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Date</font></th>
<th><font face="Arial, Helvetica, sans-serif">URN</font></th>
<th><font face="Arial, Helvetica, sans-serif">Recipient Email</font></th>
<th><font face="Arial, Helvetica, sans-serif">Recipient Name</font></th>
<th><font face="Arial, Helvetica, sans-serif">Sales Agent Name</font></th>

</tr>

<?
include("dbinfo.php");

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM zevasce ORDER BY date";
$result=mysql_query($query);
$num=mysql_num_rows($result);
mysql_close();

$i=0;
while ($i < $num) {

$date=mysql_result($result,$i,"date");
$urn=mysql_result($result,$i,"urn");
$recipientemail=mysql_result($result,$i,"recipientemail");
$recipientname=mysql_result($result,$i,"recipientname");
$salesagentname=mysql_result($result,$i,"salesagentname");

?>

<tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo $date; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $urn; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $recipientemail; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $recipientemail; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $salesagentname; ?></font></td>

</tr>

<?
$i++;
}

echo "</table>";

}

?>

Link to comment
Share on other sites

The provided code displays the contents of the table in its entirety using SELECT *

 

The columns of the table are as follows:

 

Date | URN | Recipient Email | Recipient Name | Sales Agent Name

 

I want all the distinct values of Date to list in the first dropdown and and the distinct values of Sales Agent Name in the second dropdown.

 

Finally depending on which options the user selects from each drop down the results will be listed accordingly on the same page.

Link to comment
Share on other sites

Oh so you want to populate the table based on a specific date or agent. Okay then, you're on the right track.

 

First create your drop down lists. Then use a IF and ELSEIF statements to search the table for specific information. The query will look something like:

$stuff = mysql_query("SELECT * FROM `zevasce` WHERE salesagentname=$salesagentname") or die(mysql_error());
while ($in = mysql_fetch_assoc($stuff)) {
TABLE HTML
}

 

And it should throw out all the information (should be no need for the $i statements).

Do you need help building the dropdown lists or the IF, ELSEIF statements?

Link to comment
Share on other sites

I've tried working with this script I found through google but it seems incomplete.

 

<?php
... 
mysql cnx code 
... 

$sql="SELECT id, thing FROM table"; 
$result=mysql_query($sql); 

$options=""; 

while ($row=mysql_fetch_array($result)) { 

    $id=$row["id"]; 
    $thing=$row["thing"]; 
    $options.="<OPTION VALUE=\"$id\">".$thing; 
} 
?> 
... 
html code 
... 

<SELECT NAME=thing> 
<OPTION VALUE=0>Choose 
<?=$options?> 
</SELECT> 
... 

 

I know this seems like a basic example though I can't actually work out whats going on.

 

Why is there a need for it to call on the id when its only populating one variable?

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.