:::

7-2 完成首頁輪播圖及新聞列表

  1. 編輯 index.php,修改一下 switch 流程,在沒有 $id 編號時,就是列出所有新聞
    switch ($op) {
        case 'embed':
            # code...
            break;
    
        default:
            if (!empty($id)) {
                $smarty->assign('news', show($id));
                $op = "show";
            } else {
                $smarty->assign('all_news', all($start));
                $op = "main";
            }
            break;
    }
    

     

  2. 因為有用到新變數 $start,所以,也記得要加入過濾,這次給個預設值5,也就是5篇換一頁
    <?php
    require 'header.php';
    
    // 過濾外部傳來變數
    $op = filter_input_var('op');
    $id = filter_input_var('id', 'int');
    $start = filter_input_var('start', 'int', 0);
    

     

  3. 開啟 templates/main.tpl,將原本的載入新聞語法改用Smarty迴圈來進行,並且設定第一筆資料要載入輪播圖,其餘的只載入縮圖
    {foreach $all_news as $key => $news}
        {if $key==0}
            {include file="article.tpl" slider=true title=$news.news_title date=$news.report_date school=$news.school_name count=$news.views author=$news.publisher content=$news.news_content link=$news.related_link thumb=""}
        {else}
            {include file="article.tpl" slider=false title=$news.news_title date=$news.report_date school=$news.school_name count=$news.views author=$news.publisher content=$news.news_content link=$news.related_link thumb=$news.thumb_image}
        {/if}
    {/foreach}
    

     

  4. 這樣就大功告成了。

:::

書籍目錄

展開 | 闔起

http%3A%2F%2Fcampus-xoops.tn.edu.tw%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbdsn%3D2026%26tbsn%3D55

計數器

今天: 3086308630863086
昨天: 2259225922592259
總計: 7951884795188479518847951884795188479518847951884