Re: Code help on a multi select list

From: Date: Fri, 29 Oct 2004 13:33:22 +0000
Subject: Re: Code help on a multi select list
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-200628@lists.php.net to get a copy of this message
Stuart Felenstein wrote:
I want to do a server side trap if a user selects more from a mult select list then allowed. Just unsure and didn't find any examples. Seems people rely more on javascript these days. So here is how I grab the array: if ( is_array( $_REQUEST['LurkerIndustry'] ) ) { $_SESSION['l_industry'] = array_unique( array_merge( $_SESSION['l_industry'], $_REQUEST['LurkerIndustry'] ) ); } here is my somewhat hazy notion: if ( is_array( $_REQUEST['LurkerIndustry'] ) ) { $_SESSION['l_industry'] = array_unique( if $l_industry > 5 ( Here I'm guess I need to redirect back to page
         with and error message)
else array_merge( $_SESSION['l_industry'], $_REQUEST['LurkerIndustry'] ) ); }
First of all, you didn't show your HTML code for the form, so I'm going to assume that your SELECT form field is set up as an array with the square brackets in the name: <select name="foo[]" multiple="multiple"> Next, you'll want to check out the array functions at <http://www.php.net/array> and study them. In particular, you'll want to check out the count() function. I'm not exactly sure of the logic behind your code, so I can't really comment on what you're trying to do, but, assuming you're trying to only count the unique values picked on the form, you'll want to do something like this: $l_industry = array_unique($_REQUEST['LurkerIndustry']); if (count($l_industry) > 5) {
    // ... do your stuff like redirecting back
    // to the form or redisplaying your form
} -- Regards, Ben Ramsey http://benramsey.com --------------------------------------------------- Atlanta PHP - http://www.atlphp.org/ The Southeast's premier PHP community. ---------------------------------------------------

« previous php.general (#200628) next »