Search code examples
phparraysbooleanassociative-array

Associative Array and Boolean Check in PHP


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.


Solution

  • 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).