Posted: Sun Nov 15, 2009 12:57 pm Post subject: multiple selections scroll down menu
Hi everybody, Im new here so a little introduction. Iam 30 years, very new to the website developing and php/mysql business and thats why I had to post a question on your forum.
I am developing this website for people to find a particular type of car.
It's something like this (the original website has ofcourse much more choices but to make it easier to read) --> the options:
1. brand of the car: Audi, Opel, BMW
2. color: red, green, black
3. year the car was built: 1990, 1991, 1992
I'm using php and html.
Bwlow is the code + form I'm using which works great (although some say there are a lot of errors and wrong coding, for some reason the code works fine even with error-detection on) . I can make a selection, and when I press search, the results are displayed on results.php using Lightbox (very nice)
My question is: I now have only 1 option I can select. Bij adding size=1 multiple> and [] to for example <select name="brand"> to make it look like <select name="brand[]" size=1 multiple>
I can select multiple options. But when I press the search button I get the message "no selection was made". But I did make a selection. When I remove [] and keep the addition size=1 multiple then I can select multiple options but when I press the search button, the result.php page only dusplays the results of the last selection. For example I select Audi and Opel then the result page only displays Opel pictures. I dont understand what I'm doing wrong. I hope you do.
Thank you very much already!!!!
Code:
<?php
$brands = array("audi","bmw","opel");
$colors = array("red","groen","black",);
if($_SERVER['REQUEST_METHOD'] == 'POST' ) {
$where = array();
if (isset($_POST["brand"]) && in_array($_POST["brand"],$brands)) {
$where[] = "BRAND='".$_POST["brand"]."'";
}
if (isset($_POST["color"]) && in_array($_POST["color"],$colors)) {
$where[] = "COLOR='".$_POST["kleur"]."'";
}
if (isset($_POST["year"]) && preg_match("/^(19|20)\d\d$/i", $_POST["year"])) {
$where[] = "YEAR='".$_POST["year"]."'";
}
if (count($where)==0) {
echo "no selection was made.";
} else {
$query = "select ID, BRAND, COLOR, YEAR from catalog_table WHERE ".implode(" AND ",$where);
if ($result = mysql_query($query)) {
if (mysql_num_rows($result)<>0) {
while ( $row = mysql_fetch_assoc ( $result ) ) {
echo $row["BRAND"]." - ".$row["COLOR"]." - ".$row["YEAR"]."<br />";
$informationquery = "select INFORMATION from information where Catalog_ID=".$row["ID"];
if ($informationresult = mysql_query($informationquery)) {
if (mysql_num_rows($informationresult)<>0) {
while ( $informationrow = mysql_fetch_assoc ( $informationresult ) ) {
echo $informationrow["INFORMATION"]."<br />";
}
} else {
echo "no information availible<br />";
}
}
$photoquery = "select PHOTO from foto where Catalog_ID=".$row["ID"];
if ($photoresult = mysql_query($photoquery)) {
if (mysql_num_rows($photoresult)<>0) {
while ( $photorow = mysql_fetch_assoc ( $photoresult ) ) {
echo '<a title="' . $row['PHOTO'] . '" href"images/big/' . $row['PHOTObig'] . "' rel="lightbox"><img src="images/thumbs/' . $photorow['PHOTOthumb']."' alt='".$row["BRAND"]." - ".$row["COLOR"]." - ".$row["YEAR"]."' /><br />";
}
} else {
echo "<img src='nophoto.jpg' alt='no photo' /><br />";
}
}
}
} else {
echo "no cars matched your selection.";
}
}
}
}
Joined: 02 May 2004 Posts: 5914 Location: toronto, canada
Posted: Mon Nov 16, 2009 5:15 am Post subject:
I noticed a few things when looking at your code.
1. you are checking for the POST var 'color', but are using 'kluer'
Quote:
if (isset($_POST["color"]) && in_array($_POST["color"],$colors)) {
$where[] = "COLOR='".$_POST["kleur"]."'";
}
2. The multiple items come back as an array within the POST array, yo can check this out by doing a VAR_DUMP($_POST);. So you need to handle that your code is taking in an array and then change the WHERE var to handle that. Usually an IN clause is good for that. Something like
Code:
if (isset($_POST["brand"]) && in_array($_POST["brand"],$brands)) {
$where[] = "BRAND in ('".implode("','"$_POST["brand"])."')";
}
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum