PHP forums, MySQL forums, Web Development resources

Home PageHome    PHP ResourcesTopic List    FAQFAQ    SearchSearch    MemberlistMemberlist    UsergroupsUsergroups 
 RegisterRegister
    ProfileProfile    Log in to check your private messagesLog in to check your private messages    Download the RSS Reader RSS Feed Download the RSS Reader RSS for this forum Log inLog in 

PHP Forum :: MySQL Forum :: Java Script Forum



multiple selections scroll down menu

 
Post new topic   Reply to topic    WeberForums.com Forum Index -> PHP General
View previous topic :: View next topic  
Author Message
kareltje



Joined: 15 Nov 2009
Posts: 1

PostPosted: Sun Nov 15, 2009 12:57 pm    Post subject: multiple selections scroll down menu Reply with quote

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.";
                }
            }
        }
}
 
?>
<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>
Back to top
View user's profile Send private message
lostboy



Joined: 02 May 2004
Posts: 5914
Location: toronto, canada

PostPosted: Mon Nov 16, 2009 5:15 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message Send e-mail AIM Address MSN Messenger
nguyentruong



Joined: 02 Dec 2009
Posts: 2

PostPosted: Thu Dec 03, 2009 3:35 am    Post subject: Reply with quote

Oh` very good...Thank you for sharing
---------------------------------------------
tuyen dung | tim viec | viec lam
Back to top
View user's profile Send private message
manmada



Joined: 14 Dec 2009
Posts: 5
Location: Bangalore, IN

PostPosted: Mon Dec 14, 2009 9:33 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
Display posts from previous:   
WeberTrivia Questions WeberTrivia Questions
 Think you are smart? Prove it!. Try your skills with these questions :
 WeberTrivia QuestionsRecursive arrays and multi-dimensional arrays are one and the same. (PHP and MySQL)
 WeberTrivia QuestionsThe \"cache_dir\" tag of the squid configuration has a default of /var/spool/squid. (Linux)

WeberTrivia Questions



PHP Code Examples
 Stream diffrent sizes of images from a single image to save disk space.
 JavaScript dropdown list menu to switch any page.
 Dump the contents of a PHP variable in html format with a recursive list of subfolders and files from a given root directory.
 PHP Dump in html format the contents of one array variable with a recursive list of the nested array variables inside.
 Link Extractor - This function is used to extract links from a given URL. This will convert relative path into absolute path and also remove PHPSESSID stuff.
 Building a Dynamic Form using Javascript and innerHTML. Add form elements in realtime without refreshing the page.
 A PHP Calendar function with CSS : add a cool calendar to any php page by just adding a calendar class based function.
Post new topic   Reply to topic    WeberForums.com Forum Index -> PHP General All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
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






Powered by phpBB © 2001, 2005 phpBB Group
PHP Forum :: MySQL Forum :: Java Script Forum