douhao2026 2009-12-18 16:37
浏览 27
已采纳

Smarty的简单新闻档案?

There is a simple table of news articles: [ id | title | body | created ].

What is the best way to create a simple news archive using smarty from a full collection of articles ordered by created date?

In the format:

  • 2009
    • 12.11. Title
    • 03.03. Title
    • 01.01. Title
  • 2008
    • 11.12. Title
    • 04.03. Title
    • 02.03. Title
    • 16.02. Title
  • 2007
    • ...
    • ...

<ul>
    {foreach item=p from=$news}
    <li>{$p->created} {$p->title}</li>
    {/foreach}
</ul>
  • 写回答

1条回答 默认 最新

  • doushenxu7294 2010-02-02 13:22
    关注

    There are 2 ways

    1. You create the structure in Smarty
    2. You group results from db by year

    The first would go

    $rows = $db->query('select year, title from news');
    $smarty->assign('news', $rows);
    
    // smarty code
    {if count($rows) > 0}
      {assign var=lastYear value=0}
      <ul><li>
      {foreach name=foo from=$rows item=item}
        {if $smarty.foreach.foo.first}
          {$row.year}<ul>
        {elseif $lastYear != $row.year}
          </ul></li><li>{$row.year}<ul>
        {/if}
        {assign var=lastYear value=$row.year}
        <li>{$row.title}</li>
      {/foreach}
      </ul></li></ul>
    {/if}
    

    The other one is to group the values in php:

    $rows = $db->query('select year, title from news');
    $news = array();
    foreach($rows as $row) {
      $news[$row['year']][] = $row['title'];
    }
    
    $smarty->assign('news', $news);
    
    // smarty code
    <ul>
    {foreach from=news key=year item=rows}
      <li>{$year}<ul>
      {foreach from=$rows item=row}
        <li>{$row}</li>
      {/foreach}
      </ul></li>
    {/foreach}
    </ul>
    

    The choice is up to you

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示