NYCPHP Meetup

NYPHP.org

[nycphp-talk] Newbie question - multi-select list box

Jeff jsiegel1 at optonline.net
Fri May 2 11:20:44 EDT 2003


Thank you so much for this code. Now I have a direction to go in.

Jeff

-----Original Message-----
From: gadhra [mailto:bfeqx44002 at sneakemail.com] 
Sent: Friday, May 02, 2003 9:38 AM
To: NYPHP Talk
Subject: Re: [nycphp-talk] Newbie question - multi-select list box


I think you need to generate your select box via a function, and not 
straight in HTML. A little bit less efficient, I know, but maybe one of 
the only ways to get the result you want.

Here's a quick function that I think will work (I didn't test it, but 
there you go):

function select_box($name,$option,$post='') {
  if ( empty ($post) ) {
    $post = array();
  }
 
  $str = "\
<select name=\\"$name[]\\" size=\\"10\\" multiple>";
  foreach ($option as $key=>$val) {
    if ( in_array($val,$post) ) {
      $str .="<option value=\\"$val\\" selected>$key</option>\
";
    } else {
      $str .= "<option value=\\"$val\\">$key</option>\
";
    }
  }
  $str .= "</select>\
";
  return $str;
}

So, let's say your option string, that you get from your database or 
whatever, looks like this:
$option = array (
        'Select text 1'=>1,
        'Select text 2'=>2,
        'Select text 3'=>3
      )
     

Put the result of this function in a variable, and echo it out on your 
webpage:
$displaySelect = select_box('MultipleSelect',$option);
echo $displaySelect;

If you error out, and want to redisplay the multiple selects, you only 
need to run something like:

if (isset ($error) ) {
  $displaySelect = 
select_box('MultipleSelect',$option,$_POST['MultipleSelect']);
}


echo out the $displaySelect, and you should be set.

I can't speak the veracity of this particular code (I didn't test it), 
but I know the principle behind it works quite well.

+gadhra+(Stefan)

Jeff jsiegel1-at-optonline.net |NY PHP| wrote:

>Small clarification.
>
>Here is the post variable from the multi-select box (I used
>print_r($_post) to get this). DL_mCategories is the name of the select 
>box.
>
> [DL_mCategories] => Array ( [0] => 2 [1] => 11 [2] => 3 )
>
>The question is, how do I manipulate that array so that the 
>multi-select box is "preselected" with the user's selections. That is, 
>when the form posts to itself I don't want the user's selections to 
>disappear.
>
>Jeff
>
>-----Original Message-----
>From: Neeraj Rattu [mailto:neerajrattu at consultant.com]
>Sent: Friday, May 02, 2003 9:10 AM
>To: NYPHP Talk
>Subject: Re: [nycphp-talk] Newbie question - multi-select list box
>
>  
>



--- Unsubscribe at http://nyphp.org/list/ ---






More information about the talk mailing list