Page 1 of 1

Newbie Dropdown

Posted: Fri Jun 14, 2002 10:37 pm
by virgil
Hi

How do I get multiple selected option values from a dropdown list, in a form, to insert into a table? I only seem to be able to get the first selection sent to the database. Or if I make it an array...... i.e.:

<select size="3" name="sport_type[]" multiple>
<option>baseball </option>
<option>football</option>
<option>basketball</option>
<option>soccer </option>

.......I get the just the word array in the table column.
How does it work?

Thanks

Posted: Fri Jun 14, 2002 11:34 pm
by Peter
Use arrays. You get the word array because it is an array... but try this to extract each value....

<?
foreach($sport_type as $sport){
echo "One of the options you chose was " . $sport;
}
?>

Posted: Sat Jun 15, 2002 1:01 pm
by Matt Phelps
On a similar subject (and I hope you don't mind me hijacking the thread a sec) I am looking for a way to set the value of a select box by getting it from the database.

I want someone to be able to submit their form and then later re-edit the information they sent which means that the select dropdown menu should show what it was they originally selected. Make sense?

Posted: Sat Jun 15, 2002 6:09 pm
by Peter
Matt... a quick HTML lesson...

<SELECT NAME='anything'>
<OPTION VALUE='1'>One
<OPTION VALUE='2'>Two
<OPTION VALUE='3'>Three
<OPTION VALUE='4' SELECTED>Four
<OPTION VALUE='5'>Five
</SELECT>

From that, the page will load with Four selected.

Posted: Sat Jun 15, 2002 6:15 pm
by twigletmac
Or in XHTML:

Code: Select all

<select name="anything"> 
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4" selected="selected">Four</option>
<option value="5">Five</option>
</select>
Mac

Posted: Sat Jun 15, 2002 8:33 pm
by Matt Phelps
Peter wrote:Matt... a quick HTML lesson...

<SELECT NAME='anything'>
<OPTION VALUE='1'>One
<OPTION VALUE='2'>Two
<OPTION VALUE='3'>Three
<OPTION VALUE='4' SELECTED>Four
<OPTION VALUE='5'>Five
</SELECT>

From that, the page will load with Four selected.
Right but the problem is that the item that I want to be selected is defined by a variable in the database. I have a form that my users fill in and submit including a dropdown box. Later on they want to edit the information and so I pull the info back out of the database and into the form text fields. I need the dropdown box to 'remember' what it had selected.

If you see what I mean.... :)

Posted: Sat Jun 15, 2002 11:12 pm
by Peter
Yes, I understand but I thought with the example I gave you that you could write something like this...

Say the drop down box contained the users' favourite type of beverage...

<?

// you've connected to the database

$result = mysql_query("select drink from table where user='mr_squiggle'");

list($drink) = mysql_fetch_row($result);

// you've got an array of the type of drink
$beverages = array('coke','coffee','tea','energy drink');

echo "<select name='drink'>";
foreach($beverages as $typeofdrink){
if($typeofdrink == $drink){
echo "<option value='$typeofdrink' selected>$typeofdrink";
}else{
echo "<option value='$typeofdrink'>$typeofdrink";
}
echo "</select>";

?>

..and walla!

Posted: Wed Aug 07, 2002 1:47 pm
by shootahdl
i'm having the same issue except i'm using a abbreviation to represent the value in the database.

for example, i have a state dropdown box which contains options like:

<OPTION value="NY">New York</OPTION>

when i go back to edit the record, i want "New York" to appear in the dropdown because i had selected it earlier when i first submitted the record.

any thoughts?

Posted: Wed Aug 07, 2002 2:25 pm
by Matt Phelps
Right, here is how I finally solved the puzzle with help from Peter.

What you want is some code like this:

Code: Select all

<option value="NY" <?php if ($myvariable== 'NY') &#123;echo "SELECTED";&#125; ?>>NY</option>
<option value="MI" <?php if ($myvariable== 'MI') &#123;echo "SELECTED";&#125; ?>>MI</option>
<option value="CA" <?php if ($myvariable== 'CA') &#123;echo "SELECTED";&#125; ?>>CA</option>

etc
What happens is that for each if statement it checks to see if the variable you got out of your database (or wherever) matches that option. If it does then it echo's SELECTED in the HTML code and your dropdown menu will automatically pre-select that option.

Hope that helps!

Posted: Wed Aug 07, 2002 2:50 pm
by shootahdl
thanks.

any thoughts how i would be able to map the database abbreviation to the real value.

so, if i displayed a table containing many different state abbreviations, i could map those abbrerviations to the actually state names and display.

i.e.

DB value
-----------
NY
CA
AL

Displayed value
-------------------
New York
California
Alabama

if i were to list out all of the possible values, this could make the sourcefile too big.

does anyone know of a method of creating a map file or reference file for these type of scenarios?

Posted: Wed Aug 07, 2002 4:18 pm
by RandomEngy
Try creating a PHP file that looks like this:

Code: Select all

<?php

$state_array = array(
  "NY" => "New York",
  // rest of the states
  "CA" => "California"
);

?>
Then when you need the full state name, just include that file, and use

Code: Select all

echo "State: ".$state_array&#1111;$dbvalue];
Another way would be creating another table in the database with all of the abbreviations and full names, but that would be less convenient than the associative array approach.

Posted: Thu Aug 08, 2002 9:17 am
by shootahdl
That should work.

I also have a DB abbreviation that's already in an array when i display it.

i.e.

echo "<td>" . $row["state"] . "</td>\n";

would i be able to map that value with the associative array.

i.e.

echo "<td>" . $state_array[$row["state"]]. "</td>\n";

or do i have to assign the value in the array to another variable?

Posted: Thu Aug 08, 2002 11:11 am
by RandomEngy
That last line should work. Test it out.