Thanks for the suggestion, John. When I implement your code, however, I get
an error saying
Warning: Invalid argument supplied for foreach() in
home/student/peter/public_html/week4.php on line 153

where line 153 is

foreach($strandx[$subject] as $strandy )

This is the code as I have it now:

switch($_POST['fsubject'])
{
        case "English":
                $subject = "English";
        break;
        case "Mathematics":
                $subject = "Mathematics";
        break;
}
Print "<h4>Now select the strands for Grade $grades:
$subject:</h4></td></tr><tr><td>";

print "<td>";
print "$subject"; //this tells me that I have the correct value
foreach($strandx[$subject] as $strandy )
{
//print " <li><b>".$subject."</b>.<br />\n";


print " <ul>\n";

foreach( $strandy as $str )

{ print "<li>";
checkbox("fstrand[]",$strandx,$str,0,0);
print "</li>\n";
}

Any help would again be appreciated.

Peter
-----Original Message-----
From: John W. Holmes [mailto:[EMAIL PROTECTED]]
Sent: Sunday, February 02, 2003 1:28 PM
To: 'Peter Gumbrell'; [EMAIL PROTECTED]
Subject: RE: [PHP] selecting from an array based on form content


> 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/





-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to