Multi select boxes
Moderator: General Moderators
Multi select boxes
Can someone tell me if there is a way to count the number of items selected from a multi select box? I need to run a loop but need to know how many items have been selected by the user. Can this be done?
- RandomEngy
- Forum Contributor
- Posts: 173
- Joined: Wed Jun 26, 2002 3:24 pm
- Contact:
Actually, I just figured it out. You need to name your list box: name[] This will make it an array. Then in your PHP code you use a for loop to go through the array to access each value. My problem was that I only needed it to run through the array as many times as the number of items selected. The solution to this is to use the PHP function sizeof($arrayname) in your for loop. This returns the the size of the array which is exactly the number of selected items.

-
daemorhedron
- Forum Commoner
- Posts: 52
- Joined: Tue Jul 23, 2002 11:03 am
The problem you have is that you are treating the select as a var instead of an array in your html select (ie <select multiple name="foo">). Try <select multiple name="foo[]"> and var_dump that. See the example code.
HTH!
Code: Select all
<? if ($mselect1) var_dump($mselect1); ?>
<form method="post" action="<?=$PHP_SELF?>">
<select multiple name="mselect1ї]">
<option value="r">Red</option>
<option value="g">Green</option>
<option value="b">Blue</option>
</select>
<input type=submit>
</form>-
daemorhedron
- Forum Commoner
- Posts: 52
- Joined: Tue Jul 23, 2002 11:03 am