Jump to content

Multi select list remember value


karenn1

Recommended Posts

Hey everyone,

 

I have a multiple select list pulling it's info from my database:

 

<select name="skill[]"size="5" multiple="multiple" class="formfields" id="skill[]">
<?php		
while ($rs_skill = mysql_fetch_array($result_skill))	{
print "<option value=\"".$rs_skill["id"]."\">".ucwords($rs_skill["skill"])."</option>";
}
?>      
</select>

 

When the page gets submitted and a user left out any other field, it returns to the page with an error. All of this works fine, however, the values selected in the multiple list doesn't stay selected. How can I adapt the coding to remember the selections?

 

 

 

 

Thanks!

 

Karen

 

 

 

 

 

Link to comment
Share on other sites

Ignace, you are a star! It works perfect!!

 

My next question. If I have an array of values stored into my database for a user, ie. 4, 10, 43 (the numbers being the skill ID), how can I adapt this code to pull in the value from the database and show the multiple selected skills in the list?

 

I hope this makes sense

 

 

Thanks!

 

Karen

Link to comment
Share on other sites

Try:

 

print "<option value=\"".$rs_skill["id"]."\"".(isset($_POST['skill']) && in_array($rs_skill['id'], $_POST['skill']) ? ' selected="selected"' : '').">".ucwords($rs_skill["skill"])."</option>";

 

Your select has multiple="multiple" and the PHP adds a selected="selected" to any skill that is in the _POST['skill'] array. So aren't all skills selected? Or do you mean upon you retrieve it from the database? You can pre-fill $_POST['skill']:

 

if (empty($_POST)) {//nothing was yet submitted
    while ($row = mysql_fetch_assoc($result)) {
        $_POST['skill'][] = $row['skill_id'];
    }
} else {
    //form was submitted
}

//form is underneath this code

Link to comment
Share on other sites

Hi ignace,

 

Yes, I want to retrieve the array from the database and have those items selected in the multi list. I've used your coding and it looks like this now:

 

$sql_skillarray = "SELECT * FROM skill_sellers WHERE id = '".$_REQUEST['id']."'";
$result_skillarray = mysql_query($sql_skillarray);

if (empty($_POST)) {//nothing was yet submitted

	while ($row = mysql_fetch_assoc($result_skillarray)) {
        		     $_POST['skill'][] = $row['skills'];
    		}
} else {
    	//form was submitted
}


while ($rs_skill = mysql_fetch_array($result_skill))	{
  print "<option value=\"".$rs_skill["id"]."\"".(isset($_POST['skill']) && in_array($rs_skill['id'], 
  $_POST['skill']) ? ' selected="selected"' : '').">".ucwords($rs_skill["skill"])."</option>";
}

 

Nothing is selected. What am I doing wrong?

 

 

 

Thanks!

 

Karen

Link to comment
Share on other sites

while ($rs_skill = mysql_fetch_array($result_skill))   {
    echo $rs_skill['id'], ' in ', print_r($_POST['skill'], true), '<br>';
    print "<option value=\"".$rs_skill["id"]."\"".(isset($_POST['skill']) && in_array($rs_skill['id'],
    $_POST['skill']) ? ' selected="selected"' : '').">".ucwords($rs_skill["skill"])."</option>";
}

 

What does this give you?

Link to comment
Share on other sites

Hi Ignace,

 

Thanks again for all your help. The code above displays the list but nothing is selected. Herewith my complete code for the list:

 

<select name="skill[]" size="5" multiple="multiple" class="formfields" id="skill[]">
           <?php
   						
$sql_skill = "SELECT * FROM skills ORDER BY skill ASC";
$result_skill = mysql_query($sql_skill);


while ($rs_skill = mysql_fetch_array($result_skill))   {
    	echo $rs_skill['id'], ' in ', print_r($_POST['skill'], true), '<br>';
   	print "<option value=\"".$rs_skill["id"]."\"".(isset($_POST['skill']) && in_array($rs_skill['id'],
    	$_POST['skill']) ? ' selected="selected"' : '').">".ucwords($rs_skill["skill"])."</option>";
}

?>
</select>

 

Any ideas?

 

 

 

Karen

Link to comment
Share on other sites

I hope this is what you are looking for. It's a very long list so I just copied the first few:

 

<select name="skill[]" size="5" multiple="multiple" class="formfields" id="skill[]">
                      1 in <br><option value="1">Account Handling: Account Director</option>2 in <br>
<option value="2">Account Handling: Account Executive</option>3 in <br>
<option value="3">Account Handling: Account Manager</option>4 in <br>
<option value="4">Account Handling: Administration</option>5 in <br>
<option value="5">Account Handling: Group Account / Client Services</option>6 in <br>
<option value="6">Account Handling: Team Assistant</option>7 in <br>
<option value="7">Consulting: Advertising</option>8 in <br>
<option value="8">Consulting: Branding</option>  </select>

Link to comment
Share on other sites

I'm a bit confused by your code, check if this is correct:

 

$sql_skillarray = "SELECT * FROM skill_sellers WHERE id = '".$_REQUEST['id']."'";
$result_skillarray = mysql_query($sql_skillarray);
while ($rs_skill = mysql_fetch_array($result_skillarray))   {
    print "<option value=\"".$rs_skill["id"]."\"".(isset($_POST['skill']) && in_array($rs_skill['id'], $_POST['skill']) ? ' selected="selected"' : '').">".ucwords($rs_skill["skill"])."</option>";
}

Link to comment
Share on other sites

$sql_skillarray = "SELECT * FROM skill_sellers WHERE id = '".$_REQUEST['id']."'";
$result_skillarray = mysql_query($sql_skillarray);
while ($rs_skill = mysql_fetch_array($result_skillarray))   {
    print "<option value=\"".$rs_skill["id"]."\"".(isset($_POST['skill']) && in_array($rs_skill['id'], $_POST['skill']) ? ' selected="selected"' : '').">".ucwords($rs_skill["skill"])."</option>";
}

 

The list is now blank again. Just a white block

 

 

Link to comment
Share on other sites

Change it back to:

 

$sql_skill = "SELECT * FROM skills ORDER BY skill ASC";
$result_skill = mysql_query($sql_skill);                                    
while ($rs_skill = mysql_fetch_array($result_skill))   {
    print "<option value=\"".$rs_skill["id"]."\"".(isset($_POST['skill']) && in_array($rs_skill['id'], $_POST['skill']) ? ' selected="selected"' : '').">".ucwords($rs_skill["skill"])."</option>";
}

 

What does the table skill_sellers then contain?

Link to comment
Share on other sites

skill_sellers contains a listing of all clients, ie:

 

1 John Smith john@smith.com 1,3,5

 

In this example, 1,3,5 is the id numbers of all his skills. This is what I want to match up with the multi select list. When updating John's profile, I want to see what skills he has, thus those must be selected on the multi select list. All skills are in the 'skills' table.

 

Does that makes sense?

Link to comment
Share on other sites

Ah k I get it. But that 1,3,5 shouldn't be in your table as now you can't work with it to gather the skills the user has use it like:

 

CREATE TABLE users_skills (
    skills_sellers_id INTEGER NOT NULL,
    skills_id INTEGER NOT NULL,
    PRIMARY KEY (skills_sellers_id, skills_id)
);

INSERT INTO users_skills VALUES (1, 1), (1, 3), (1, 5);

 

I created 3 rows and they all have 1 for skills_sellers_id (your user's id) and that matches up with 1, 3 and 5 (his skills) If you now would select all skills (ids) for that user:

 

SELECT skills_id FROM users_skills WHERE skills_sellers_id = 1

 

Returns 1, 3 and 5 Now to get the name for that skill id:

 

SELECT * FROM skills LEFT JOIN users_skills ON skills.id = users_skills.skills_id WHERE skills_sellers_id = 1

 

Now to PHP:

 

$id = /*retrieve user id from session or wherever it is stored*/;
$query = "SELECT skills.skill, skills_sellers_id, skills_id  FROM skills LEFT JOIN users_skills ON skills.id = users_skills.skills_id WHERE skills_sellers_id = $id";// This will give us all skills
$result = mysql_query($query);
print '<select name="skills">';

while ($row = mysql_fetch_assoc($result)) {
    echo '<option value="', $row['skills_id'], '"', ((null != $row['skills_sellers_id']) ? ' selected="selected"' : ''), '>', $row['skill'], '</option>';
}
print '</select>';

 

I have pulled a little trick here I used LEFT JOIN which will get all rows from skills even if their is no record for in users_skills (whereby skills_sellers_id = null) which will cause to list all skills (even those the player doesn't have if you don't want that, remove LEFT + 1) and will 'highlight' (selected="selected") all skills the player has.

 

Take a look in phpMyAdmin and provide this query:

 

SELECT skills.skill, skills_sellers_id, skills_id  FROM skills LEFT JOIN users_skills ON skills.id = users_skills.skills_id WHERE skills_sellers_id = 1

 

(1): echo '<option value="', $row['skills_id'], '" selected="selected">', $row['skill'], '</option>';

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.