Forum Moderators: coopster

Message Too Old, No Replies

Running a query from drop down boxes

Anybody wanna give me a kickstart?

         

deejay

9:24 am on Aug 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, so I'm moving up from garden variety static sites and getting into this MySQL/PHP thing.

I've build my first sample database. I've even got a couple of pages calling information from the database.

YAY!

But of course, this new endeavour is being driven by a purpose... and that purpose is a boss leaning over my shoulder asking 'can you, can you, can you?'

The goal is a page with a few drop down boxes with parameters in each... eg, region, size, colour, etc. User selects any paramaters they want, click a button and results are returned. (In this case a list of relevant items with links to their info pages)

It's the drop down boxes that have me a bit flummoxed right now. I've not used anything like them before and, as usual, my terminology challenged brain isn't helping me with searching for info. Can you build these boxes in straight html or are they something 'flash' like javascript or something? Ideally I'd like them to pull the criteria lists from the database so that as info is added there any new parameter options are also added.

I hate asking these sort of questions - they feel too 'do my homework-ish'.. but the homework is a lot easier if you at least know which textbook to look at. :)

Any and all pointers would be appreciated.

templar

2:44 pm on Aug 22, 2004 (gmt 0)

10+ Year Member



Hey,

Here is some sample code which should help:

dropdown.php


<?php

// get variable after selecting something from the dropdown with name 'chooser'
$select = $_POST['select'];

// if something has been chosen
if (!empty($select)) {

// get the chosen value
$chooser = $_POST['chooser'];

// do whatever u want with that value
// for example output it
echo($chooser.'<br><br>');

}

?>

<!-- This defines a new form -->
<form action="dropdown.php" method="post">

<!-- This creates the dropdown in html -->
<select name="chooser">

<option value="sweets">Sweets</option>
<option value="vegies">Vegetables</option>
<option value="fruit">Fruit</option>

</select>

<!-- Create a button -->
<input type="submit" value="Select" name="select">

</form>

If you open that website it will show u a drop down box with three items; Sweets, Vegetables and Fruit. You can select one and click on Select. This will then show you what you chose just above the dropdown.

Here is what it all means:

Everything between the <?php?> brackets is php, the rest is HTML.

<!-- this is an html comment -->
// this is a php comment

When you first open the website, the php will get what is inside $_POST['select'] and store it into the $select variable. Since you have not selected anything yet the $select variable will be empty and hence if (!empty($select)) will fail and the rest of the php will be skipped and the html will be displayed. This is why all you see is the dropdown and the button.

Ill explain the tags next:

'<form action="dropdown.php" method="post">'

This defines a new form element. Form elements are used for user input. The action property is the page all the data gets sent to. The method can either be post or get. In the case of post the data is sent transparently to the webserver, in the case of get the data is sent through the URL. (if you use get it will work just the same, but in your php you need to change $_POST to $_GET. When using get you will see the url look like this: dropdown.php?select=Select&chooser=fruit for example).

'<select name="chooser">'

This defines the start of a new dropdown box. The name is what php will use to grab the info from it. So inside the php you will see $_POST['chooser'] which retrieves the value selected in the dropdown.

'<option name="fruit">Fruit</option>'

This is an element inside the dropdown box. You can add as many of these between the <select name="chooser"> </select> tags. Depending on how many choices u want. The name is the value that will be stored inside the $_POST['chooser'] variable.

'<input type="submit" value="Select" name="select">'

This creates an input element of type 'submit'. Every form needs some sort of button to be able to send the data. These buttons have the type 'submit' (the name is pretty obvious). You can have other elements, such as type="text" or type="hidden". The 'text' element is a text box and the 'hidden' element is an invisible element which can be used to pass data transparently. The value is what will be written on the button. The name is the name of the button. The value can be retrieve like everything else using $_POST['select'].

Now.. assuming that you select something and click on select. The data will be posted to the webserver and it will execute the php. This time $_POST['select'] will not be empty as it will contain the string 'Select' (ie the value of the button). So the if (!empty($select)) will not fail this time.

Next you need to find out what was chosen in the dropdown box. This value can be retrieved using $_POST['chooser'] and store it into the variable $chooser. (Note: variable names can have whatever name you choose, i just chose names that make it more obvious).

Next you can do whatever you want, now that u know what was chosen in the dropdown. The above code just outputs it to the website using echo($chooser) which as you should see just prints it on the website.

Assuming you have a database and inside it a table with all sorts of foods, say u select 'fruit' from the dropdown you could select all the fruit out of your database table. Say you have columns in your table called 'name and 'type' and your table is called 'foods'.

You could do something like this:

dropdown2.php


<?php

// get variable after selecting something from the dropdown with name 'chooser'
$select = $_POST['select'];

// if something has been chosen
if (!empty($select)) {

// get the chosen value
$chooser = $_POST['chooser'];

// select the type from the database
// database connection details (change to whatever you need)
$HOST = 'myhost';
$DATABASE = 'mydatabase';
$USER = 'username';
$PASSWORD = 'password';

// connect to database
if(!$conn=mysql_connect('mysqlhost','username','password')) {
echo("<li>Can't connect to $HOST as $USER");
echo("<li>MySQL Error: ".mysql_error());
die;
}

// select database
if (!mysql_select_db($DATABASE,$conn)) {
echo("<li>We were unable to select database $DATABASE");
die;
}

// if everything successful create query
// this selects all rows where the type is the one you chose in the dropdown
// * means that it will select all columns, ie name and type as i said above
$sql_query = "SELECT * FROM foods WHERE type='$chooser'";

// get the data from the database
$result = mysql_query($sql_query,$conn);

// output data
while (($details = mysql_fetch_assoc($result))) {
// print out the name
echo('Name: '.$details['name'].' - ');
// print out the type
echo('Type: '.$details['type'].'<br>);
}

// close mysql connection
mysql_close($conn);

}

?>

<!-- This defines a new form -->
<form action="dropdown.php" method="post">

<!-- This creates the dropdown in html -->
<select name="chooser">

<option value="sweets">Sweets</option>
<option value="vegies">Vegetables</option>
<option value="fruit">Fruit</option>

</select>

<!-- Create a button -->
<input type="submit" value="Select" name="select">

</form>

After selecting something from the dropdown and then clicking the button it will display all the foods of the type you selected.

In mysql run the below sql code to set up a table assuming you already have a database to store this stuff.

CREATE TABLE foods (
id auto_increment NOT NULL,
name VARCHAR(32) NOT NULL,
type VARCHAR(32) NOT NULL,
PRIMARY KEY(id));

INSERT INTO foods (name,type) VALUES ('apple','fruit');
INSERT INTO foods (name,type) VALUES ('orange','fruit');

etcetcetc you get the point

make sure that the type is the same as the dropdown because it is case sensitive. ie types should be

fruit
vegies
sweets

The commands just above need to be run inside mysql, not from a website. Im sure you can figure that out. Check out www.mysql.com and www.php.net.

Appologies for any mistakes. I haven't actually tested the code. Its approximately correct or totally correct i don't know. So you can just try it out and if u come across errors its good practice to try to fix them. If you can't just post here again and ill help.

Byes

Oliver

coho75

2:51 pm on Aug 22, 2004 (gmt 0)

10+ Year Member



Wow Templar, great post!

There is a lot of good information and examples here.

coho75

lazydog

3:28 pm on Aug 22, 2004 (gmt 0)

10+ Year Member



Hi!

Great post by templar. I'll keep mine short and just point you to the textbooks :)

You can create drop down boxes in html. Look at FORM, SELECT, OPTION. See -
[w3schools.com...]

To process the parameters, you will find a lot of info in the php manual.

[php.net...]
[php.net...]

Hope that helps,
Saurabh.

deejay

9:21 am on Aug 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Templar that's fabulous! Thank you. It'll take me a few days to get down to this again thanks to a wee network worm I'm dealing with, but you have me very fired up on this now. :)

And Lazydog, thank you too. I'm definitely goin to be working my way through those links as well. :)

coopster

1:29 pm on Aug 23, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, templar and lazydog!

deejay, To add to lazydog's list, there are also more resources available in the PHP Library [webmasterworld.com].