duandgfh0506 2017-04-19 08:39
浏览 73
已采纳

php foreach将2个数组合并为1行(工作日和数据)

I have a problem with my current "Attendance Project", so I have 2 arrays.

  • 1st array is to show a "workdays" the 1st array show only workdays in current month ex:April, so the result in my 1st array is (3,4,5,6,7,10,11,12,13,14,17,18,19,20,21,24,25,26,27,28)
  • 2nd array is showing Employee Attendance in current month ex:April, so the result in my 2nd array is (17, 19)

here is my current code :

<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>

It will produce : enter image description here

I want value (17 and 19) will markup the data with yellow background, and the table is NOT out of range. Any help will appreciate..

  • 写回答

4条回答 默认 最新

  • doucong7963 2017-04-19 08:54
    关注

    Your code seems messy and I'm not gonna try to fix it on what you have, but I'll suggest solution:

    1st - run foreach ($workdays as $w) and make header 2nd - run foreach ($workdays as $w) and make table-body like:

    foreach ($workdays as $w) {
        if (in_array($w, $tanggale)) //if tanggle is the one with 17 and 19
        {
             //code
        }
        else
        {
             //code
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?