views:

551

answers:

3

here is my mysql and php code layout:

I have 3 tables

  • tableA stores unique "person" information
  • tableB stores unique "places" information
  • tableC stores not unique information about a person and places they have "beenTo".

here is how i layed out my form: -one big form to insert into "person" tableA; "beenTo" tableC in the form, a person mulitple selects "places" which get inserted into "beenTo"

my question is, when i am editing a "person" how do i display what the user has already selected to appear on my multiple select options drop down menu?

my drop down menu at the moment query "places" table and displays it in a multiple select drop down menu. its easier when a person have beenTo one place, the problem arrises when there is more than one "beenTo" places?

any ideas. thanks

+2  A: 

Foreach option, check if they have beenTo it. Then add the selected="selected" attribute to the tag if true.

Example:

<select multiple="multiple">
    <option selected="selected">Rome</option>
    <option>France</option>
    <option selected="selected">Underpants</option>
</select>

And in PHP this might look like:

$beenTo = array("Rome","Underpants");
$places = array("Rome","France","Underpants");
?> <select multiple="multiple"> <?php
foreach($places as $place) {
    echo "<option";
    $found = false;
    foreach($beenTo as $placeBeenTo) {
        if ($placeBeenTo == $place) {
            $found == true;
            echo " selected=\"selected\">";
            break;
        }
    }
    if (!$found) echo ">";
    echo $place . "</option>";
}
?> </select> <?php

There's probably a much more efficient way to do this.

Fletcher Moore
i think i will need to loop it, but if i am selecting one person to display their information, not sure how the looping will happen. i have been thinking about it 4ever!
Menew
Ops, too slow, deleting my answer, I would add the [select w3c syntax](http://www.w3schools.com/TAGS/tag_Select.asp) to your answer as a reference and point out the size attribute too.
Eineki
I added some PHP. It's hideous, but it will work. You just need to find some way to fill the arrays.
Fletcher Moore
This answer has the right approach. I don't think why this would be too much of a problem. @Chocho:'but if i am selecting one person to display their information, not sure how the looping will happen.' You have 2 tables, one for 'person' information(you can't loop that) second for the 'placesbeento' that will return multiple entries for each person you have to loop that, as in the example above.
DMin
A: 
<option id = 'example' <? if ($_POST['example']) echo 'selected' ?>>

But you'll be using a $_SERVER['cookies'] or other cache to store the past visits, not the $_POST array.. edit with extreme predjudice

Stiggz
I like it. Succinct while making a strong point.
webbiedave
selected="selected" to make it XHTML compliant
bigstylee
A: 

For clarification, you will want to have a name attribute for your select tag which allows for multiple selected options to function correctly.

<form method="post" action="">
<select name="places[]" multiple="multiple">
<?php
$_POST += array('places' => array());
$places = array('fr' => 'France', 'cn' => 'China', 'jp' => 'Japan');
$beenTo = array_flip($_POST['places']);
foreach ($places as $place => $place_label) {
  $selected = isset($beenTo[$place]) ? ' selected="selected"' : '';
  echo '<option value="' . $place . '"' . $selected . '>' . $place_label . '</option>';
}
?>
</select>
<input type="submit" value="Save Changes" />
</form>
Jonathan Champ