Jump to content

Make Select Options Of Multiple Select Sticky


son
 Share

Recommended Posts

I use the following to make 'normal' select options sticky:

if((isset($_POST['category'])) && ($_POST['category'] == $category_id)){ echo ' selected="selected" '; }

How do you do this with multiple select options? This one does not work...Son

Link to comment
Share on other sites

Personally, I simply wouldn't use "echo" to begin with.Other than that, it's hard to say why yours isn't working... you'll have to show a larger part of your code.

Link to comment
Share on other sites

Personally, I simply wouldn't use "echo" to begin with.Other than that, it's hard to say why yours isn't working... you'll have to show a larger part of your code.
The relevant code is:
<p><label for="category"><?php echo check_error('category', 'Category*'); ?></label><br /><select name="category[]" id="category[]" multiple="multiple" size="5"><option value="0">Please select a category...</option><?phpwhile (list($category_id, $category) = mysqli_fetch_array($r, MYSQLI_NUM)) {echo "<option value=\"$category_id\"";if((isset($_POST['category'])) && ($_POST['category'] == $category_id)){ echo ' selected="selected" '; }echo ">" . $category . "</option>\n";}echo "</select></p>\n";?>

with the addition at top of page:

if (isset($_POST['category']) && ($_POST['category']) != 0)	{		$category = $_POST['category'];	}	else	{		$category = FALSE;		$errors['category'] = 'Category';		}

Any ideas?Son

Link to comment
Share on other sites

$_POST['category'] is an array. You either need to use in_array or loop through it to check if the item exists, you can't just compare the entire array to a single value, they aren't equal.

Edited by justsomeguy
Link to comment
Share on other sites

$_POST['category'] is an array. You either need to use in_array or loop through it to check if the item exists, you can't just compare the entire array to a single value, they aren't equal.
Have it now as:if($category != 0 && in_array("$category_id", $_POST['category'])){ echo ' selected="selected" '; }and it works. Initially had a bit of a problem when page initially loads, so I inserted $category = 0;at top of script.Working well now... many thanks,Son
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...