multiple select v2.1 [solution] and possible 'settype()' bug in 3.0.16?

From: Date: Wed, 19 Jul 2000 23:28:59 +0000
Subject: multiple select v2.1 [solution] and possible 'settype()' bug in 3.0.16?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-7359@lists.php.net to get a copy of this message
Here is an updated version of my 'multiple select box' code that also keeps the options selected for you (as opposed to clearing them like other code fragments illustrate). This version fixes a little issue where upon first load of the page, since the form hasn't been used, the $sel variable would be an empty "string" and cause the 'each()' to STB. This one forces it to be an 'array' and therefore doesn't choke. I did notice one thing that is perhaps a bug in PHP 3.0.16, I had to put the "settype($matchArray, "array");" in the selectBox function itself rather than pass it like this: "<?php selectBox(6,sel,settype($sel, "array")); ?>", as it would come into the function as an 'integer' for some reason if I did it that way?!?! Perhaps something for the PHP guys to look into. ----------------- snip ----------------- <HTML><HEAD><title>Using multiple select v2.1</title></HEAD> <BODY> <H2>Using multiple select v2.1</H2> by <A HREF="http://daevid.com" TARGET="daevid">Daevid Vincent</A> <?php function inList($needle, $haystack) { while (list($k, $v) = each($haystack)) if ($needle == $v) return true; return false; } function selectBox($size, $name, $matchArray) { // we have to force this because on the first pass, // this is set to string and will STB. settype($matchArray, "array"); echo "<select multiple name='".$name."[]' size='".$size."'>\n"; for ($x = 0; $x < 5; $x++) { echo "\t<option value='key".$x."'"; if (inList("key".$x, $matchArray)) echo " SELECTED"; echo ">Value ".$x."\n"; } echo "</select>"; } //selectBox() //////////////////////////////////////////////////////////////////////// $num_selected = count($sel); print "<P>Number of Values selected: $num_selected<p>\n"; if ($num_selected) { echo "<P>while list version<BR>\n"; while (list($k, $v) = each($sel)) { echo "sel[".$k."] == ".$v."<BR>\n"; } } ?> <form method="post"> <?php selectBox(6,sel,$sel); ?> <br> <input type=submit value="Submit"> </form> </BODY></HTML>

« previous php.general (#7359) next »