Filter retrieved data with more than one select box
Posted: Thu Aug 08, 2013 7:34 pm
Hi!
I am creating a database and have made an example table (persons) for this post with the colums age, city, name and gender. On my webpage I have four select boxes for age, city, name and gender to filter what will displays in content of the page.
My function below has three parts. The first and the second show all the rows if there is no selection in the neither of the four select boxes. The last part show every row that meet the criterias in all four rows.
I also want to be able to show every row with values that meet the criterias when I select only one, two or three select boxes. I could write a part for every possible combination, but is there a better and more effective solution?
Here is the function:
Here is the webpage:
Leftbar:
Content:
Suzanne
I am creating a database and have made an example table (persons) for this post with the colums age, city, name and gender. On my webpage I have four select boxes for age, city, name and gender to filter what will displays in content of the page.
My function below has three parts. The first and the second show all the rows if there is no selection in the neither of the four select boxes. The last part show every row that meet the criterias in all four rows.
I also want to be able to show every row with values that meet the criterias when I select only one, two or three select boxes. I could write a part for every possible combination, but is there a better and more effective solution?
Here is the function:
Code: Select all
function show_data(){
if(!isset($_POST['retrieved_gender']) && !isset($_POST['retrieved_age']) && !isset($_POST['retrieved_city']) && !isset($_POST['retrieved_name'])){
$result=mysql_query("SELECT * from persons") or die(mysql_error());
while($row=mysql_fetch_array($result)){
echo $row['name']."<br />".$row['age']."<br />".$row['city']."<br />".$row['gender'];
}
}
else
if($_POST['retrieved_gender'] == "" && $_POST['retrieved_age'] == "" && $_POST['retrieved_city'] == "" && $_POST['retrieved_name'] == ""){
$result=mysql_query("SELECT * from persons") or die(mysql_error());
while($row=mysql_fetch_array($result)){
echo $row['name']."<br />".$row['age']."<br />".$row['city']."<br />".$row['gender'];
}
}
else
if($_POST['retrieved_gender'] && $_POST['retrieved_age'] && $_POST['retrieved_city'] && $_POST['retrieved_name']){
$gender = $_POST['retrieved_gender'];
$age = $_POST['retrieved_age'];
$city = $_POST['retrieved_city'];
$name = $_POST['retrieved_name'];
$result=mysql_query("SELECT * from persons WHERE gender = '$gender' && age = '$age' && city = '$city' && name = '$name'") or die(mysql_error());
while($row=mysql_fetch_array($result)){
echo $row['name']."<br />".$row['age']."<br />".$row['city']."<br />".$row['gender'];
}
}
}Leftbar:
Code: Select all
<?php select_gender( (isset($_POST['retrieved_gender']) ? $_POST['retrieved_gender'] : null) ); ?>
<?php select_age( (isset($_POST['retrieved_age']) ? $_POST['retrieved_age'] : null) ); ?>
<?php select_city( (isset($_POST['retrieved_city']) ? $_POST['retrieved_city'] : null) ); ?>
<?php select_name( (isset($_POST['retrieved_name']) ? $_POST['retrieved_name'] : null) ); ?>Content:
Code: Select all
show_data();