It is currently Tue Feb 07, 2012 8:05 am

All times are UTC + 2 hours [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: multiple selections scroll down menu
PostPosted: Sun Nov 15, 2009 1:57 pm 
Offline

Joined: Sun Nov 15, 2009 1:50 pm
Posts: 1
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'] . '" rel="nofollow" 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.";
                }
            }
        }
}

?>
<hr />
<form method="post" action="result.php">
    <select name="brand">
        <option selected="selected">Brand</option>
        <option><?php echo implode("</option><option>",$brands); ?></option>
    </select>
    <select name="color">
        <option selected="selected">Color</option>
        <option><?php echo implode("</option><option>",$colors); ?></option>
    </select>
    <select name="year">
        <option selected="selected">Year</option>
        <option>1990</option>
        <option>1991</option>
        <option>1992</option>
    </select>
    <input type="submit" value="search" />
</form>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 16, 2009 6:15 am 
Offline

Joined: Sun May 02, 2004 11:34 pm
Posts: 6498
Location: toronto, canada
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"])."')";
        }

_________________
Lostboy

Cat, the other other white meat

Please read Posting Etiquette before posting

You can always try Google


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 03, 2009 4:35 am 
Offline

Joined: Wed Dec 02, 2009 11:48 am
Posts: 2
Oh` very good...Thank you for sharing
---------------------------------------------
tuyen dung | tim viec | viec lam


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 14, 2009 10:33 am 
Offline

Joined: Mon Dec 14, 2009 9:44 am
Posts: 5
Location: Bangalore, IN
I observed one more thing, i.e you need to use multiple attribute for the select box, if you want to select multiple.
http://w3schools.com/tags/att_select_multiple.asp


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC + 2 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 0 guests


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

Search for:
Jump to:  
cron