首页 > 解决方案 > 从 Laravel 中的存储过程列中动态显示包含列的 HTML 表

问题描述

我有一个带有一些参数的存储过程。我想显示带有列名的 HTML 表应该是存储过程中的列名。我怎样才能做到这一点?

示例:我正在执行我的存储过程 CALL_LIST, 2020-06-01, 2020-06-03。这里 CALL_LIST 是存储过程名称,2020-06-01 和 2020-06-03 是参数。

存储过程的结果将是这样的

    +---------+-------------+-----------+-----------+
    | Emp_Code | 01-06-2020 | 02-06-2020| 03-06-2020|
    +----------+------------+-----------+-----------+
    |       1  | 10:00      |   09:00   |   10:00   |
    |       2  | 10:00      |   10:00   |   10:00   |
    |       3  | 10:00      |   10:00   |   10:00   |
    +----------+------------+-----------+-----------+

我的要求是根据传递给存储过程的日期创建动态列。就像这里我传递了 2020-06-01 和 2020-06-03,所以需要生成 3 个动态列与值。如果我通过 2020-06-01 和 2020-06-10,则必须生成 10 个动态列并显示值。

例子

<table>
<thead>
     <tr>
      <th>Emp Code</th>
      <?php $begin = new DateTime($datefrom);
        $end = new DateTime($dateto);
        $end = $end->modify('+1 day');
        $interval = new DateInterval('P1D');
        $daterange = new DatePeriod($begin, $interval, $end); 
       foreach ($daterange as $date_val) {
        <th scope="col">$date_val->format('d-m-Y')</th>
        } ?>
     </tr>
</thead>
<tbody><?php foreach ($data as $val) { ?>
   <tr>
      <td>{{$val->Emp_Code.}}</td>
   </tr><?php } ?>
</tbody>
</table>

如何显示存储过程中的列值?

标签: phpsql-serverlaravelstored-procedures

解决方案


我得到了结果

<table>
<thead>
     <tr>
      <th>Emp Code</th>
      <?php $begin = new DateTime($datefrom);
        $end = new DateTime($dateto);
        $end = $end->modify('+1 day');
        $interval = new DateInterval('P1D');
        $daterange = new DatePeriod($begin, $interval, $end); 
       foreach ($daterange as $date_val) {
        <th scope="col">$date_val->format('d-m-Y')</th>
        }
            $dates = array();
            foreach ($daterange as $key => $date) {
                $dates[$key] = $date->format('Y-m-d');
            }   ?>
     </tr>
</thead>
<tbody><?php foreach ($data as $val) { ?>
   <tr>
      <td>{{$val->Emp_Code}}</td>
      <?php foreach($dates as $val){
        <td>{{$row->{$val}}}</td>
           <?php } ?>
   </tr><?php } ?>
</tbody>
</table>

推荐阅读