关联数组和布尔值检查 PHP
Associative Array and Boolean Check in PHP
我不太确定如何调用关联数组来验证数字是真还是假,因为我正在做一个简单的注册检查器 class。最大 class 容量为 40,文件由 HTML 和 PHP.
组合而成
我是这样做的:-
<?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);
?>
代码:-
<table width="300" style="border: 1px solid black">
<tr>
<?php
// class and enroll Lists
echo "<td width=20>";
echo "Class"."        "." Enroll"."<br><hr>";
for($i=0; $i < $totalEnroll; ++$i) {
echo $class[$i] . "     " .
$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>
我想要的输出:-
Class
Enroll
Full States
J1
20
Not Full
J5
40
Full
检查部分说`//注册以检查class房间是否已满。这是检查注册代码 class 的地方。
然而,顺便说一下,输出是否可以在类似 table 的列中更清晰地对齐。
您在两个单独的循环中显示数据,这使得对齐数据变得困难,您应该将其变成一个循环并在每组项目周围使用 <tr>
和 <td>
标签...
<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>
(这不包括 header,但我相信您可以添加)。
我不太确定如何调用关联数组来验证数字是真还是假,因为我正在做一个简单的注册检查器 class。最大 class 容量为 40,文件由 HTML 和 PHP.
组合而成我是这样做的:-
<?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);
?>
代码:-
<table width="300" style="border: 1px solid black">
<tr>
<?php
// class and enroll Lists
echo "<td width=20>";
echo "Class"."        "." Enroll"."<br><hr>";
for($i=0; $i < $totalEnroll; ++$i) {
echo $class[$i] . "     " .
$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>
我想要的输出:-
Class Enroll Full States J1 20 Not Full J5 40 Full
检查部分说`//注册以检查class房间是否已满。这是检查注册代码 class 的地方。
然而,顺便说一下,输出是否可以在类似 table 的列中更清晰地对齐。
您在两个单独的循环中显示数据,这使得对齐数据变得困难,您应该将其变成一个循环并在每组项目周围使用 <tr>
和 <td>
标签...
<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>
(这不包括 header,但我相信您可以添加)。