Forum Moderators: coopster

Message Too Old, No Replies

Multiple Select Box and Mysql

I want to mark options as selected in a multiple select box using mysql

         

macman1

11:25 pm on Feb 18, 2006 (gmt 0)

10+ Year Member



Hello all -

I am designing an admin page for one of my sites and have a multiple select box that is used to select several areas of the site a user has access to. This is part of a CMS. I have a table in my database that has two fields: "username", "contentpage" what I want to do is compare the rows pulled from the database with the ones in my select box and if they match, mark them as selected. I have the following code but it isn't working correctly. It will only select one. Any ideas? Thanks!

$resultID = mysql_query("SELECT contentpage FROM contentpages WHERE username = '$username'", $linkID);
$row = mysql_fetch_assoc($resultID);
echo '<select name="assigned_locations[]" size="8" multiple>';
echo '<option'.($row['contentpage']=="Engineering"? ' selected' : '').'>Engineering</option>';
echo '<option'.($row['contentpage']=="Home"? ' selected' : '').'>Home</option>';
echo '<option'.($row['contentpage']=="News"? ' selected' : '').'>News</option>';
echo '<option'.($row['contentpage']=="Employee of the Month"? ' selected' : '').'>Employee of the Month</option>';
echo '<option'.($row['contentpage']=="Business Office"? ' selected' : '').'>Business Office</option>';
echo '<option'.($row['contentpage']=="Sales"? ' selected' : '').'>Sales</option>';
echo '<option'.($row['contentpage']=="Programming"? ' selected' : '').'>Programming</option>';
echo '<option'.($row['contentpage']=="Promotions"? ' selected' : '').'>Promotions</option>';
echo '</select>';

coopster

11:59 pm on Feb 18, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, macman1.

You need to loop through the result set, not just read the first row. We typically use while loops for this. Think of the result set as a spreadsheet of data coming back to you, rows and columns. You can't just read the first one, you need to loop through and read them all. Let me show you one way to do this:

$resultID = mysql_query("SELECT contentpage FROM contentpages WHERE username = '$username'", $linkID); 
$user_options = array(); // initialize
while [php.net] ($row = mysql_fetch_assoc($resultID)) {
// build option list
$user_options[] = $row['contentpage'];
}

Now you have all the options for this user in an array. Much easier to check against using in_array() [php.net]. Also, in this case, it seems you have a pre-defined list of options. I'll often load them into an array and use them to build the list as well. Sure makes it easier to expand upon later!

$valid_options = array( 
'Engineering',
'Home',
'News',
'Employee of the Month',
'Business Office',
'Sales',
'Programming',
'Promotions'
);
echo '<select name="assigned_locations[]" size="8" multiple="multiple">';
foreach [php.net] ($valid_options as $v) {
echo '<option'.(in_array($v, $user_options) ? ' selected="selected"' : '').'>'.htmlentities [php.net]($v).'</option>';
}
echo '</select>';

I didn't test any of this code, ripped it off the top of my head. Hopefully this gives you some ideas on how to setup and process a result set to build an option list.

macman1

1:32 pm on Feb 19, 2006 (gmt 0)

10+ Year Member



Thanks Coopster!

I appreciate the help. It worked right away! I am glad to see you knew what I was trying to do right away.

Thanks again!