0

I'm not too sure how to call the Associative array to verify if the number is true or false because I am doing a simple checker for enrollment class. The max class capacity is 40 and the file is combined with HTML and PHP.

I did it like this:-

<?php   
     //Create the association array.
     $classInfo = array("J1" => 20 ,"J2" => 30,"J3" => 10,"J4" => 43,
                          "J5" => 40,"J6" => 45,"J7" => 15,"J8" => 34,"J9" => 10,"J10" => 45);

     $class = array_keys($classInfo);
     $totalEnroll = count($classInfo);
?>

The Code:-

    <table width="300" style="border: 1px solid black">
        <tr>
            <?php
                // class and enroll Lists
                echo "<td width=20>";  
                echo "Class"."&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp"." Enroll"."<br><hr>";
                    for($i=0; $i < $totalEnroll; ++$i) { 
                        echo $class[$i] . "&nbsp&nbsp&nbsp&nbsp&nbsp" . 
                             $classInfo[$class[$i]] . "<br>"; 
                    } 
                echo "</td>";
                
                // Enroll to check whether the classroom is full.
                echo "<td width=20>";
                    echo "Class States" . "<br><hr>";
                    for($check = 0; $check < 10; $check++){
                        if($classInfo[$totalEnroll[$check]] >= 0 && 
                           $classInfo[$totalEnroll[$check]] <= 40){
                            echo "Full";
                            echo " <br>";
                        } else {
                            echo "Not Full";
                            echo " <br>";
                        }
                    }
                echo "</td>";
            ?>
        </tr>
    </table>

The Output That I want:-

Class Enroll Full States
J1 20 Not Full
J5 40 Full

Do check on the part say `//Enroll to check whether the classroom is full. This is where the code of the enrollment class is checked.

However, by the way, the output can it be aligned more cleanly in a table-like column.

NISHA NAJIHAH
  • 177
  • 2
  • 12

1 Answers1

2

You are displaying the data in two separate loops which makes it difficult to align the data, you should make it into 1 loop and use the <tr> and <td> tags around each group of items...

<table width="300" style="border: 1px solid black">
    <?php
        foreach ( $classInfo as $className => $enrolled )   {
            echo "<tr>";
            // class and enroll Lists
            echo "<td>{$className}</td>";
            echo "<td>{$enrolled}</td>";

            // Enroll to check whether the classroom is full.
            echo "<td>";
            if($enrolled <= 40){
                echo "Full";
            } else {
                echo "Not Full";
            }
            echo "</td>";
            echo "</tr>";
        }
    ?>
</table>

(This doesn't include a header, but I'm sure you can add that).

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • Thank you I see... May I ask how do you know when to loop separately or in 1 loop – NISHA NAJIHAH May 11 '21 at 15:40
  • And why did you put this { } in here `echo "{$className}";` – NISHA NAJIHAH May 11 '21 at 15:42
  • If you need to data to be related as in this case, one loop helps link the two together. If you want them in lists one after the other, then 2 loops is better. – Nigel Ren May 11 '21 at 15:42
  • The `{}` just makes it clear what the variable is - https://stackoverflow.com/questions/2596837/curly-braces-in-string-in-php – Nigel Ren May 11 '21 at 15:42
  • Oh, I never know about this before `{ }`. Really thank you for explaining to me. I got to learn something new hehe. This helps a lot :) @Nigel Ren – NISHA NAJIHAH May 11 '21 at 16:16