0

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?

2
  • I'm confused ... can you post some code somewhere (pastebin or here)? Commented Apr 12, 2010 at 15:22
  • i don't have a code. basically, i have 3 tables that i need to select to display one person information to edit. eg. if i want to edit person id=111 i click on that id and i populate that person info in my form fields. i have a multiple select in my form fields. which again, i query from the db to display all the available options. the 3rd table select comes when i am trying to match whata person has already selected with my options values. again, its easier when a person has only one options selected in the db, the problem comes when i am trying to display all the person's multiple options. Commented Apr 12, 2010 at 15:30

3 Answers 3

2

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) {
        echo "value='$place'";
        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.

Sign up to request clarification or add additional context in comments.

4 Comments

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!
Ops, too slow, deleting my answer, I would add the select w3c syntax to your answer as a reference and point out the size attribute too.
I added some PHP. It's hideous, but it will work. You just need to find some way to fill the arrays.
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.
1

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>

Comments

0
<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

1 Comment

selected="selected" to make it XHTML compliant

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.