简体   繁体   English

如何在用户友好的树形视图html列表中显示具有datetime属性的记录(php + mysql)?

[英]How to show records(php+mysql) with datetime attributes in a user friendly tree view html list?

I got some records with column 'id','time','catagory','content' saved in MySql database. 我在MySql数据库中保存了一些带有“ id”,“ time”,“ catagory”,“ content”列的记录。 Demo PHP (Actually I am using Yii, the data were provided by dataprovider.) 演示PHP (实际上,我使用的是Yii,数据由dataprovider提供。)

And I need to present the data in a user-friendly way. 我需要以一种用户友好的方式呈现数据。

The list were group by "Week"=>"Date"=>"record". 该列表按“周” =>“日期” =>“记录”进行分组。 Demo Output html 演示输出html

Could some give me an smart way to do the php loop? 可以给我一个聪明的方法来做php循环吗?

Should I need to do an Week loop to check the exist week and saved an key-value array, 如果我需要进行Week循环来检查存在的一周并保存了键值数组,

then to check the any record were on the same day and saved in another array, 然后检查同一天是否有任何记录并保存在另一个数组中,

then finally do the output loop to show the html?Many thanks. 然后最后做输出循环来显示html?

<div class="tree">
<ul>
    <li>
        <span>2014, Week 2 , 2014-01-05~2014-01-11</span>
            <ul>
                <li>
                    <span>Monday, January 6</span>
                        <ul>
                            <li>
                                <div class="record">
                                    <span>08:00:30</span><br>
                                    <span>Play</span><br>
                                    <span>Do something good...</span><br>
                                </div>
                            </li>
                        </ul>
                </li>
function getRecords($result){
$arr = new array();
while ($row = myqsli_fetch_array($result)) {
        $arr[] = $row;
    }
return $arr;
}
$week;
$date;
$records = getReceords($queryresultfromyourdb);
foreach($records as $rec)
{
if ($week != $rec["Week"])
{
echo $rec["Week"] . '<br>';
$week = $rec["Week"];
}
if ($date != $rec["Date"])
{
echo $rec["Date"] . '<br>';
$date = $rec["Date"];
}
echo $rec["record"] . '<br>';
}

This is the php code: 这是PHP代码:

<?php
//fake data setting
$recordlist=Array();
$record=Array();
$record['time']='2014-01-06 08:00:30';
$record['catagory']='Play';
$record['content']='Do something good...';
$recordlist[]=$record;

$record=Array();
$record['time']='2014-01-07 10:00:30';
$record['catagory']='Study';
$record['content']='Do something fun...';
$recordlist[]=$record;

$record=Array();
$record['time']='2014-01-07 12:00:30';
$record['catagory']='Sport';
$record['content']='Bike';
$recordlist[]=$record;

$record=Array();
$record['time']='2014-12-15 12:00:30';
$record['catagory']='Sport';
$record['content']='Bike';
$recordlist[]=$record;

$record=Array();
$record['time']='2014-12-12 12:01:30';
$record['catagory']='Sport';
$record['content']='Bike';
$recordlist[]=$record;
?>
<?php
//output
//how to output just as http://jsfiddle.net/euj89/


// Refactor Array
foreach($recordlist as $record){
    $date = explode(" ",$record['time']);
    $hour = $date[1];
    $date = $date[0];
    $week = date("W",strtotime($date));
    $month = date("F",strtotime($date));
    $year = date("Y",strtotime($date));
    $newRecordList[$year][$month][$week][$date][$hour]['time']       = $record['time'];
    $newRecordList[$year][$month][$week][$date][$hour]['category']   = $record['catagory'];
    $newRecordList[$year][$month][$week][$date][$hour]['content']    = $record['content'];
}

//OUTPUT
?><div class="tree"><ul><?
        foreach ($newRecordList as $year => $recordByYear) {
            foreach ($recordByYear as $month => $recordByMonth) {
                foreach ($recordByMonth as $week => $recordByWeek) {
                    ?><li><span><?=$year?>, Week <?=$week?> , date</span><ul><?
                    foreach ($recordByWeek as $day => $recordByDay) {
                        ?><li><span><?=date("l",strtotime($day))?>, <?=$month?> <?=$day?> </span><ul><?

                            foreach ($recordByDay as $hour => $recordByHour) {
                                ?><li><div class="record">
                                    <span><?=$hour?></span><br>
                                    <span><?=$recordByHour['category']?></span><br>
                                    <span><?=$recordByHour['content']?></span><br>
                                </div></li><?
                            }
                            ?></ul></li><?
                    }
                    ?></ul></li><?
                }
            }
        }
?></ul></div><?

Output should be like this: 输出应如下所示: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM