【问题标题】:Create HTML Table using PHP stdClass Object使用 PHP stdClass 对象创建 HTML 表
【发布时间】:2016-03-15 08:20:41
【问题描述】:

我有一个像下面这样的数组,

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 表格输出,

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

举个例子,

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

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

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>

提前致谢。

【问题讨论】:

  • 测试一下,让我知道状态foreach($arr as $val){ $a = get_object_vars($val); ?&gt;&lt;tr&gt;&lt;?php foreach($a as $v ){ echo '&lt;td&gt;$v&lt;/td&gt;'; } ?&gt; &lt;/tr&gt; &lt;?php }
  • 嗨。它给出了类似prnt.sc/afgfx5 的内容,感谢您的帮助:))
  • 试试这是我的愚蠢foreach($arr as $val){ $a = get_object_vars($val); ?&gt;&lt;tr&gt;&lt;?php foreach($a as $v ){ echo '&lt;td&gt;' . $v . '&lt;/td&gt;'; } ?&gt; &lt;/tr&gt; &lt;?php }
  • 它给了我这个prnt.sc/afgfx5,它与预期的有点不同。
  • 现在这个foreach($arr as $val){ $a = get_object_vars($val); ?&gt;&lt;tr&gt;&lt;?php foreach($a as $v ){ echo "&lt;td&gt;"; var_dump($v); echo "&lt;/td&gt;" } ?&gt; &lt;/tr&gt; &lt;?php }

标签: php html arrays


【解决方案1】:

我认为如果项目名称、结构名称和任务名称的组合再次出现,它应该跳过该行。 试试这段代码

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

【讨论】:

  • 谢谢。是的,这结合了项目名称、结构名称和任务名称,我还想在跳过其他行的同时在同一行中显示工作时间。
  • 如果您可以参考我附上的图片。你也许能理解我在说什么:)) 我很感激你的手
  • @jitendra,你的回答只对这个有好处,如果他将来需要改变,它不会起作用。所以我建议你用更好的方式来做,现在,我可以投票它。
猜你喜欢
  • 2018-12-18
  • 1970-01-01
  • 2011-08-31
  • 2013-10-12
  • 2021-01-19
  • 1970-01-01
  • 2013-01-25
  • 2014-02-28
  • 2013-05-12
相关资源
最近更新 更多