Here's a side thought for you. If you ever code your html to have a <select> that allows for multiple values, your $value variable will start out as just the last value selected.
Code: Select all
<select name="available" multiple="multiple" size="7">
<option value="0">Sunday</option>
<option value="1">Monday</option>
<option value="2">Tuesday</option>
<option value="3">Wednesday</option>
<option value="4">Thursday</option>
<option value="5">Friday</option>
<option value="6">Saturday</option>
</select>
If your user picks Thursday, Friday, and Saturday, the reply will look something like
Code: Select all
available=6&available=4&available=5
and your cleaner code will only recognize whichever value came last. The first trick around this is to cheat with your html code.
Code: Select all
<select name="available[]" multiple="multiple" size="7">
<option value="0">Sunday</option>
<option value="1">Monday</option>
<option value="2">Tuesday</option>
<option value="3">Wednesday</option>
<option value="4">Thursday</option>
<option value="5">Friday</option>
<option value="6">Saturday</option>
</select>
Note the square brackets inside the "name" attribute. Now, if your user picks Thursday, Friday, and Saturday, the reply will look something like
Code: Select all
available[]=6&available[]=4&available[]=5
which becomes
Code: Select all
$_GET['available'][0]=6;
$_GET['available'][1]=4;
$_GET['available'][2]=5;
and the $value variable will, itself, become an array. This will generate an error. </deadpan>
When this problem beat me up, I wound up doing this:
Code: Select all
if ($_GET) {
foreach ($_GET as $index1 => $value1) {
if (is_array($value1)) {
foreach ($value1 as $index2 => $value2) {
$get[$index1][$index2] = $elements->sanitize($value2);
}
}
else {
$get[$index1] = $elements->sanitize($value1);
}
}
unset ($_GET);
}
if ($_POST) {
foreach ($_POST as $index1 => $value1) {
if (is_array($value1)) {
foreach ($value1 as $index2 => $value2) {
if (is_string($value2)) {
$post[$index1][$index2] = $elements->sanitize($value2);
}
}
}
elseif (is_string($value1)) {
$post[$index1] = $elements->sanitize($value1);
}
} # foreach ($_POST as $index1 => $value1)
unset ($_POST);
}
which runs everything through a sanitizer function, even if a given key turns out to contain an array. (The trick of using square brackets inside a name attribute isn't mine. But, I don't remember where I saw it.)
Cheers!
dafydd
--
The word "instantiate" bugs me. "Create an instance of" is too hard to type?