在php中打印递归列表

时间:2011-11-14 22:02:41

标签: php sql

我正在尝试打印一个递归列表,其中每个日期都有按日期排序的事件子列表。

例如,我在数据库中有:

+------+----------+--------+
| date | event_id | post_id|
+------+----------+--------+
|date1 | event1   | post1  |
|date1 | event2   | post2  |
|date1 | event3   | post3  |
|date2 | event4   | post4  |
|date2 | event5   | post5  |
+------+----------+--------+

我需要打印

<ul>
    <li>date1</li>
    <ul>
        <li>event1, post1</li>
        <li>event2, post2</li>
        <li>event3, post3</li>
    </ul>
    <li>Date 2</li>
    <ul>
        <li>event4, post4</li>
        <li>event5, post5</li>
    </ul>
</ul>

如何在php中打印

select date, event_id, post_id from tablename

在php中查询有这个吗?

6 个答案:

答案 0 :(得分:2)

用php和mysql过了一会儿,我在mysql中做的最少,即使它通常更优雅。 mysql的性能很糟糕。即使您查询的内容很快,它仍然会减慢另一个用户在其他地方执行的另一个查询。所以这是对DB负载最小的响应。一个查询,没有分组。只需选择3个字段,即可应用此字段。

$lastDate = null;  // lastDate will be updated at each row, so we can check if it has changed the next one.

echo '<ul>';
foreach ($queryResult as $row)
{
    if ( $lastDate != $row['date'] )
    {
        if ( $lastDate) { echo '</ul>'; }  // if lastdate "exists", it's not the first, so let't close the "date" list...

        echo '<li>', $row['date'], '</li><ul>'; // ... and start a new one
    }
    echo '<li>', $row['event_id'], ', ',$row['post_id'], '</li>';
    $lastDate = $row['date'];  

}
echo '</ul>'; // close the very last date list
echo '</ul>'; // close the the full list

答案 1 :(得分:1)

看起来你需要MySQL GROUP_CONCAT()

SELECT   date,
         GROUP_CONCAT(event_id) AS 'event_ids',
         GROUP_CONCAT(post_id) AS 'post_ids'
FROM     tablename
GROUP BY date

这将返回三个字段的结果。 date将成为日期字段,event_idspost_ids将是逗号分隔的事件和帖子列表。

答案 2 :(得分:1)

这应该有效。你想要添加一些错误检查。

<ul>
    <?php
    $result = mysql_query(SELECT date FROM tableName);
    if (mysql_num_rows($result) > 0)
    {
        while ($dateRow = mysql_fetch_array($result))
        {
            echo "<li>" . $dateRow['date'] . "</li>";
            $result2 = mysql_query("SELECT event_id,post_id FROM tableName where date = '" . $dateRow['date'] . "'");
            if (mysql_num_row($result2) > 0)
            {   
                echo "<ul>";
                while ($row = mysql_fetch_array($result2))
                {
                    echo "<li>" . $row['event_id'] . " , " . $row['post_id'] . "</li>";
                }
                echo "</ul>";
            }
        }
    }
    ?>
</ul>

答案 3 :(得分:1)

以下是一个粗略的答案:

$query = "select date, event_id, post_id from tablename order by date";
$result = mysql_query($query);
$currentDate = null;

echo "<ul>";
while($row = mysql_fetch_assoc($result)){
     if($currentDate != $row['date']){
          echo "<li>" . $currentDate  . "</li>";
          echo "<ul>";
     }

     echo "<li>" . $row['event'] . $row['post'] . "</li>";

     if($currentDate != $row['date']){
          $currentDate = $row['date'];
          echo "</ul>";
     }
}
echo "</ul>";

答案 4 :(得分:0)

这是我对这个问题的看法。首先,您应该将无序列表项嵌套到父元素中。结果输出应为

<ul>
    <li>date1
        <ul>
            <li>event1, post1</li>
            <li>event2, post2</li>
            <li>event3, post3</li>
        </ul>
    </li>
    <li>Date 2
        <ul>
            <li>event4, post4</li>
            <li>event5, post5</li>
        </ul>
    </li>
</ul>
此嵌套架构的

优点

  • 有效的HTML(仅此一点就足够了)
  • 通过Javascript更轻松地进行DOM操作

这是一个理论上应输出前html的函数:

function printList()
{
    $sql = 'SELECT t.event_id, t.post_id, t.date FROM table t ORDER BY date';
    $result = mysql_query($sql);
    $date = null;
    $out = '';

    while($row = mysql_fetch_assoc($result))
    {
        if($date !== $row['date'])
        {
            if(!is_null($date))
            {
                $out .= '</ul></li>';
            }

            $date = $row['date'];

            $out .= sprintf('<li>%s<ul>', $row['date']);
        }

        $out.= sprintf('<li>%s, %s</li>', $row['event_id'], $row['post_id']);            
    }

    return sprintf('<ul>%s</ul></li></ul>', $out);
}

答案 5 :(得分:-1)

$sql = "SELECT date, event_id, post_id
FROM tablename
GROUP BY date ORDER BY date";