使用PHP stdClass Object创建HTML表

时间:2016-03-15 08:20:41

标签: php html arrays

我有一个像bellow一样的数组,

Array ( [0] => stdClass Object ( [projectname] => test p1 [structurename] => test structure [taskname] => task [taskstartdate] => 2016-02-02 00:00:00 [estimatedhours] => 10.00 [hours] => 5.00 [createdon] => 2016-02-03 07:38:08 )
        [1] => stdClass Object ( [projectname] => test p1 [structurename] => test structure [taskname] => task [taskstartdate] => 2016-02-02 00:00:00 [estimatedhours] => 10.00 [hours] => 2.00 [createdon] => 2016-02-04 14:21:34 )
        [2] => stdClass Object ( [projectname] => p2 [structurename] => struc [taskname] => p2t1 [taskstartdate] => 2016-02-03 00:00:00 [estimatedhours] => 8.00 [hours] => 2.00 [createdon] => 2016-02-04 11:05:31 )
        [3] => stdClass Object ( [projectname] => p2 [structurename] => struc [taskname] => p2t1 [taskstartdate] => 2016-02-03 00:00:00 [estimatedhours] => 8.00 [hours] => 6.00 [createdon] => 2016-02-05 08:00:22 )
        [4] => stdClass Object ( [projectname] => web dev [structurename] => dev test [taskname] => dev task [taskstartdate] => 2016-02-04 00:00:00 [estimatedhours] => 30.00 [hours] => 8.00 [createdon] => 2016-02-04 08:21:14 )) 

我希望您的帮助可以使用上面的数组创建以下HTML表格输出,

enter image description here

这个想法是显示特定任务中某个特定任务的工作时间(在本例中为2016年2月的第一周)。 根据上面的数组,[小时]是工作小时数,[createdon]是工作小时数。

例如,

测试p1 |测试结构| task - 在上面的stdClass对象中有两个数组([0] => stdClass对象和[1] => stdClass对象),因为它们代表给定Project的相同任务,所以它们应该在HTML表的一行中

02-01 | 02-02 | 02-03 | 02-04 | 02-05 是日期(2月第一周) 而我正在使用另一个数组作为日期

Array ( [0] => 2016-02-01
        [1] => 2016-02-02
        [2] => 2016-02-03
        [3] => 2016-02-04
        [4] => 2016-02-05 ) 

此外我已尝试过如下,但它没有给我预期的输出。

<table id="filter_table4" class="mytable">
        <thead>
            <tr>
                <th>Project | Structure | <b>Task</b></th>
                <th>Start Date, Time</th>
                <th>Estimated<br />Hours</th>
                <?php
                  foreach( $dates as $date )
                  {
                    $date = DateTime::createFromFormat("Y-m-d", $date);
                    echo '<th>'. $date->format("m-d") .'</th>';
                  }
                ?>
                <th>Progress</th>
            </tr>
        </thead>
        <tbody>
            <?php
              foreach( $week_tasks as $task )
              {
                echo '<tr>
                        <td>'. $task->projectname .' | '. $task->structurename .' | <b>'. $task->taskname .'</b></td>
                        <td>'. $task->taskstartdate .'</td>
                        <td>'. $task->estimatedhours .'</td>
                        <td> '. $task->hours .' </td>
                        <td> '. $task->hours .' </td>
                        <td> '. $task->hours .' </td>
                        <td> '. $task->hours .' </td>
                        <td> '. $task->hours .' </td>
                        <td>
                            '. $progress .'
                        </td>
                    </tr>';
              }
            ?>
        </tbody>
    </table>

提前谢谢。

2 个答案:

答案 0 :(得分:1)

我认为你想要的是,如果projectname,structurename和taskname的组合再次出现,它应该跳过该行。 试试这段代码

    <?php
$array_new  = array();
$i = 0;
  foreach( $week_tasks as $task )
  {
    $search_string = $task->projectname .' | '. $task->structurename .' | '. $task->taskname;

    $date = DateTime::createFromFormat("Y-m-d", $date);
    $md = $date->format("m-d");
    if(!isset($array_new[$search_string]))
    {
        $array_new[$search_string]['name'] = $task->projectname .' | '. $task->structurename .' | <b>'. $task->taskname .'</b>'; 
        $array_new[$search_string]['taskstartdate'] = $task->taskstartdate;
        $array_new[$search_string]['estimatedhours'] = $task->estimatedhours;
    }

    $array_new[$search_string]['hours'][$md] = $task->hours; 

  }
  $html ='';
  foreach( $array_new as $row )
  {
        $html .= '<tr>
            <td>'. $row['name'] .'</td>
            <td>'. $row['taskstartdate'] .'</td>
            <td>'. $row['estimatedhours'] .'</td>';

        foreach( $dates as $date )
        {
            $date = DateTime::createFromFormat("Y-m-d", $date);
           $md = $date->format("m-d");
           if(isset($row[$md])){
            $html.='<td> '. $row[$md] .' </td>';
           }else{
                $html.='<td> </td>';
           }
        }

       $html .='<td>
                '. $progress .'
            </td>
        </tr>';
    }
    echo $html;
?>

答案 1 :(得分:0)

尝试一下,你会得到一张桌子而不是你可以修改它以获得所需的结果,或者你可以修改你的桌子标题。

  foreach($arr as $val){
        $a = get_object_vars($val); 
        echo "<tr>";
        foreach($a as $v ){
           echo "<td>$v</td>" }
           echo "</tr>";
        }
    }