RE: [PHP] selecting from an array based on form content
| From: | John W. Holmes | Date: | Sun, 02 Feb 2003 18:27:30 +0000 |
| Subject: | RE: [PHP] selecting from an array based on form content | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-134019@lists.php.net to get a copy of this message | ||
> The following code loops through a multidimensional array, printing
the
> subject heading and then the strand:
>
> foreach( $strandx as $subjectx=>$strandy )
> {
> print " <li><b>".$subjectx."</b>.<br />\n";
>
>
> print " <ul>\n";
> asort( $strandy ); // sorts the strands
> foreach( $strandy as $str )
>
> { print "<li>";
> checkbox("fstrand[]",$subjectx,$str,0,0);
> print "</li>\n";
> }
> print " </ul>\n";
> }
> print "</ol>\n";
>
> This is the array so far:
> $strandx = array('English' => array('Reading',
> 'Writing','Oral and
Visual
> Communication'),
> 'Mathematics' => array('Number Sense and Numeration',
> 'Measurement',
> 'Geometry and Spatial Sense','Patterning and Algebra','Data Management
and
> Probability'));
>
> I would like to limit the subject and strand being printed based on
the
> subject selected in a form. So, for example, if "English" is selected
in
> the
> form, only the subject name and the strands for English would be
> published.
> Could someone please help me with the code for this.
Since you already know $subjectx (English or Math), then just use that
value in your foreach() loop...
Foreach($strandx[$subjectx] as $strandy)
Where $subjectx would come from your form. Provide some validation
first.
So something like this:
//Many ways to do this, just validate
//whatever is coming from your form
//matches a key in $strandx
switch($_POST['subject'])
{
case "English"
$subject = "English"
break;
default:
$subject = "Mathematics";
break;
}
print " <ol><li><b>".$subject."</b>.<br />\n";
foreach( $strandx[$subject] as $strandy )
{
print " <ul>\n";
asort($strandy ); // sorts the strands
foreach( $strandy as $str )
{
print "<li>";
checkbox("fstrand[]",$subjectx,$str,0,0);
print "</li>\n";
}
print " </ul>\n";
}
print "</li></ol>\n";
---John W. Holmes...
PHP Architect - A monthly magazine for PHP Professionals. Get your copy
today. http://www.phparch.com/