php foreach将2个数组合并为1行(工作日和数据)
我当前的“考勤项目”有问题,所以我有2个数组php foreach将2个数组合并为1行(工作日和数据),php,mysql,arrays,codeigniter,Php,Mysql,Arrays,Codeigniter,我当前的“考勤项目”有问题,所以我有2个数组 第一个数组显示“工作日” 第一个数组只显示当月的工作日,例如:四月,因此我的第一个数组中的结果是(3,4,5,6,7,10,11,12,13,14,17,18,19,20,21,24,25,26,27,28) 第二个数组显示当月员工出勤情况,例如:4月,因此我的第二个数组中的结果是(17,19) 这是我目前的代码: <table class="table table-striped table-bordered zero-configura
- 第一个数组显示“工作日” 第一个数组只显示当月的工作日,例如:四月,因此我的第一个数组中的结果是(3,4,5,6,7,10,11,12,13,14,17,18,19,20,21,24,25,26,27,28)
- 第二个数组显示当月员工出勤情况,例如:4月,因此我的第二个数组中的结果是(17,19)
<table class="table table-striped table-bordered zero-configuration">
<thead>
<tr>
<th style="width: 200px">Siswa</th>
<!-- <?php for($i = 1; $i < 31; ++$i){?>
<th><?= $i ?></th>
<?php } ?> -->
<?php foreach($workdays as $w){ ?>
<th><?=$w;?></th>
<?php } ?>
</tr>
</thead>
<tbody>
<?php
// for($x = 1; $x < 27; ++$x){
foreach($records as $r){
?>
<tr>
<td style="width: 200px"><?=$r->StudentName;?></td>
<?php
?>
<?php
foreach($workdays as $w){
foreach($tanggale as $t){
if($w == $t){
?>
<td style="background: #FFF000">M</td>
<?php }else{ ?>
<td style="background: #48C9A9">O</td>
<?php } } } ?>
</tr>
<?php } ?>
</tbody>
</table>
西斯瓦
M
O
它将产生:
“我希望”值(17和19)将用黄色背景标记数据,并且该表未超出范围。
任何帮助都将不胜感激。您所能做的只是将两个数组合并成一个数组,然后根据需要迭代合并数组。 检查下面的代码以组合数组
<?php
$working_days = array(3,4,5,6,7,10,11,12,13,14,17,18,19,20,21,24,25,26,27,28);
$present_days = array(17.19);
$combine_attendence_array = array();
foreach($working_days as $day) {
$combine_attendence_array[$day] = 'Absent';
if(in_array($day, $present_days)) {
$combine_attendence_array[$day] = 'Present';
}
}
?>
这段代码将创建一个组合数组,其中键为day,值为present或缺席
现在您可以根据您的需求进行迭代,下面是迭代代码
foreach($combine_attendence_array as $day => $value){
if($value == 'Present'){ ?>
<td style="background: #FFF000">M</td>
<?php }else{ ?>
<td style="background: #48C9A9">O</td>
<?php } ?>
<?php } ?>
foreach($combine\u attendence\u array as$day=>$value){
如果($value=='Present'){?>
M
O
我希望这能解决你的问题。这样做吧
M
O
您的代码看起来很凌乱,我不会尝试根据您现有的代码进行修复,但我会建议解决方案:
第一次-运行foreach(工作日为$w)并制作页眉
第二步-运行foreach(工作日为$w),并使桌体类似:
foreach ($workdays as $w) {
if (in_array($w, $tanggale)) //if tanggle is the one with 17 and 19
{
//code
}
else
{
//code
}
}
foreach(工作日为$w){
foreach($tanggale作为$t){
如果($w==$t){
$color=“#FFF000”;
$text=“M”;
}否则{
$color=“#48C9A9”;
$text=“O”;
}
}
?>
嵌套在foreach循环中的foreach循环必须使该部分“超出范围”,因为它运行循环*循环次数
foreach($workdays as $w){
foreach($tanggale as $t){
if($w == $t){
$color = "#FFF000";
$text = "M";
} else {
$color = "#48C9A9";
$text = "O";
}
}
?>
<td style="background: <?php echo $color; ?>"><?php echo $text; ?></td>
<?php }?>