2 回答
TA贡献1824条经验 获得超5个赞
创建另一个数组来保存您已经使用过的值,您还可以使用循环来大大简化代码。
$grades = array("2", "3", "4", "5", "6");
//create an empty array to hold grades once they are displayed
$used = [];
while ($d = mysqli_fetch_array($query)) {
//loop through each grade value
foreach($grades as $grade) {
//check if `$grade` is in array `$d`
//AND check if `$used[$grade]` has not been set (if it hasn't been set, it hasn't been displayed)
if(in_array($grade, $d) && !isset($used[$grade])) {
//set $used[$grade] so the previous check will fail if this grade is a duplicate
$used[$grade] = true;
//include your php file
include "table/grade{$grade}_tab.php";
}
}
}
此解决方案意味着您不再需要为每个等级添加 IF 块。
TA贡献2039条经验 获得超7个赞
尝试将可用数据存储在另一个数组上,并检查数据是否使用过
$useArray = array();
$d['grade'] = array("2", "3", "4", "5", "6");
while ($d = mysqli_fetch_array($query))
{
if(in_array("2", $d) && !in_array("2",$useArray))
{
$useArray[] = 2;
include "table/grade2_tab.php";
}
if(in_array("3", $d) && !in_array("3",$useArray))
{
$useArray[] = 3;
include "table/grade3_tab.php";
}
if(in_array("4", $d) && !in_array("4",$useArray))
{
$useArray[] = 4;
include "table/grade4_tab.php";
}
if(in_array("5", $d) && !in_array("5",$useArray))
{
$useArray[] = 5;
include "table/grade5_tab.php";
}
if(in_array("6", $d) && !in_array("6",$useArray))
{
$useArray[] = 6;
include "table/grade6_tab.php";
}
}
?>
- 2 回答
- 0 关注
- 89 浏览
添加回答
举报