drnycqxwz63508434 2018-07-06 09:56
浏览 32
已采纳

too long

I try to create a table with a date column within a month, I want to try to enter the date that I have into the table but I do not understand its logic ..

<?php
    $Month = date('m');
    $Year = date('Y');
    $day = cal_days_in_month(CAL_GREGORIAN, $Month ,$Year);

    $q = $this->db->select('nm_py, dt_start,dt_finish')->from('tb_payment')->get();
    $data = $q->result_array();

    //example data nm_py : Mobile, dt_start: 2018-05-02, dt_finish: 2018-05-05
    //example table output as expected
    // | NO| Payment | Month
    // |___|_________| 1 | 2 | 3 | 4 | 5 | 6 | 7 | xxxxx
    // | 1 |  Mobile | - | Y | Y | Y | Y | - | - | xxxxxx
?>

<table>
    <thead>
    <tr>
        <th class="text-center">No</th>
        <th class="text-center">Payment</th>
        <th class="text-center" colspan="<?php echo $day; ?>">Month</th>
    </tr>
    <tr>
        <th class="text-left"></th>
        <th class="text-left"></th>
        <?php
            for ($x = 1; $x <= $day; $x++) {
                echo "<th class='text-center'>".$x."</th>";
            }       
        ?>
    </tr>
    <thead>  
    <tbody>
        <?php $no = 1; foreach($data as $row) { ?>

        <?php $no++; }?>
    </tbody>
</table>
  • 写回答

1条回答 默认 最新

  • drgmszo076956 2018-07-06 16:07
    关注

    You can use date_create to get day see working code bellow :

    <?php
    
       $q = $this->db->select('nm_py, dt_start,dt_finish')->from('tb_payment')->get();
        $data = $q->result_array();
    
        //example data nm_py : Mobile, dt_start: 2018-05-02, dt_finish: 2018-05-05
        //example table output as expected
        // | NO| Payment | Month
        // |___|_________| 1 | 2 | 3 | 4 | 5 | 6 | 7 | xxxxx
        // | 1 |  Mobile | - | Y | Y | Y | Y | - | - | xxxxxx
    ?>
    
    <table>
        <thead>
        <tr>
            <th class="text-center">No</th>
            <th class="text-center">Payment</th>
            <th class="text-center" colspan="<?php echo $day; ?>">Month</th>
        </tr>
        <tr>
            <th class="text-left"></th>
            <th class="text-left"></th>
            <?php
                for ($x = 1; $x <= $day; $x++) {
                    echo "<th class='text-center'>".$x."</th>";
                }       
            ?>
        </tr>
        <thead>  
        <tbody>
            <?php $no=1; foreach($data as $row) { 
    
                     echo "<tr>";
                     echo "<td class='text-center'>".$no."</td>";
                     echo "<td class='text-center'>".$row['nm_py']."</td>";
    
                    // days values
                    $day_start=date_create($row['dt_start']);
                    $day_end=date_create($row['dt_finish']);
    
            for ($x = 1; $x <= $day; $x++) {
    
                if($x >=$day_start->format('d') and $x <=$day_end->format('d'))
                     echo "<td class='text-center'>Y</td>";
                 else
                      echo "<td class='text-center'>-</td>";
    
            }echo "</tr>";
            $no++;
              }?>
        </tbody>
    </table>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制