Newbie Dropdown

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
virgil
Forum Commoner
Posts: 59
Joined: Thu Jun 13, 2002 11:43 pm
Location: New York, U.S.

Newbie Dropdown

Post 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
Peter
Forum Commoner
Posts: 28
Joined: Mon Jun 10, 2002 12:40 am
Location: Brisbane, Australia

Post 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;
}
?>
Matt Phelps
Forum Commoner
Posts: 82
Joined: Fri Jun 14, 2002 2:05 pm

Post 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?
Peter
Forum Commoner
Posts: 28
Joined: Mon Jun 10, 2002 12:40 am
Location: Brisbane, Australia

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Matt Phelps
Forum Commoner
Posts: 82
Joined: Fri Jun 14, 2002 2:05 pm

Post 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.... :)
Peter
Forum Commoner
Posts: 28
Joined: Mon Jun 10, 2002 12:40 am
Location: Brisbane, Australia

Post 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!
shootahdl
Forum Newbie
Posts: 3
Joined: Wed Aug 07, 2002 1:47 pm

Post 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?
Matt Phelps
Forum Commoner
Posts: 82
Joined: Fri Jun 14, 2002 2:05 pm

Post 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!
shootahdl
Forum Newbie
Posts: 3
Joined: Wed Aug 07, 2002 1:47 pm

Post 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?
User avatar
RandomEngy
Forum Contributor
Posts: 173
Joined: Wed Jun 26, 2002 3:24 pm
Contact:

Post 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.
shootahdl
Forum Newbie
Posts: 3
Joined: Wed Aug 07, 2002 1:47 pm

Post 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?
User avatar
RandomEngy
Forum Contributor
Posts: 173
Joined: Wed Jun 26, 2002 3:24 pm
Contact:

Post by RandomEngy »

That last line should work. Test it out.
Post Reply