-3

I am doing a LAB exercise about using the foreach and for loop to print a table in PHP. But I have met the problem now.

The question let me to print a table like this.

Here is my code:

<?php

        $subjects = array(
            "sem1" => array("Prog", "DP", "NF", "ENG", "SDD"),
            "sem2" => array("IP", "DMS", "OOP", "SA"),
            "sem3" => array("INSP", "SAP", "ITP"),
        );

        //maximum number of subjects
        $maxSubNum = 10;

        //creating table
        echo "<table border='1'>";
            //loop the array
            foreach ($subjects as $sem => $subjectArray) {
                //print <tr>
                echo "<tr>";
                //print semeester number in <td>, bold the text
                echo "<td><b>$sem</b></td>\n";
                //loop 10 times
                for ($i=0; $i < $maxSubNum; $i++) {
                    //check if subject exists
                    if (isset($subjectArray)) {
                        //print subject in <td>
                        echo "<td>$subjectArray[$i]</td>\n";
                    } else {
                        //print empty in <td>
                        echo "<td></td>\n";
                    }
                }
                //closing <tr>
                echo "</tr>\n";
            }
        echo "</table>\n";

        ?>

Finally, it warns me those notices although I can print out he table.

Anyone can help? Please?

2 Answers2

0

you are using wrong limit for loop not maxSubNum but count( $subjectArray)

for ($i=0; $i < count( $subjectArray)-1; $i++) {
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
0

Your sem1,2 and 3 arrays have 5,4,3 elements in them respectively. However, when you define $maxSubnum = 10 php is looking for the rest of the other elements.

Try the below.

<?php

        $subjects = array(
            "sem1" => array("Prog", "DP", "NF", "ENG", "SDD"),
            "sem2" => array("IP", "DMS", "OOP", "SA"),
            "sem3" => array("INSP", "SAP", "ITP"),
        );



        //creating table
        echo "<table border='1'>";
            //loop the array
            foreach ($subjects as $sem => $subjectArray) {
                //print <tr>
                echo "<tr>";
                //print semeester number in <td>, bold the text
                echo "<td><b>$sem</b></td>\n";
                //don't have to loop 10 times
                 //maximum number of subjects
                 $maxSubNum = count($subjectArray);
                for ($i=0; $i < $maxSubNum; $i++) {
                    //check if subject exists
                    if (isset($subjectArray)) {
                        //print subject in <td>
                        echo "<td>$subjectArray[$i]</td>\n";
                    } else {
                        //print empty in <td>
                        echo "<td></td>\n";
                    }
                }
                //closing <tr>
                echo "</tr>\n";
            }
        echo "</table>\n";

        ?>
Ela Buwa
  • 1,652
  • 3
  • 22
  • 43