17-1
新增 - 新聞管理
一、增加資料表
- admin_update.php 增加下面這段,請複製 prod來用
記得執行「後台/更新管理」
#檢查資料表(ugm_p_news)
if (!chk_isTable("{$WEB['moduleName']}_news")) {
$sql = "
CREATE TABLE `{$WEB['moduleName']}_news` (
`sn` int(10) unsigned NOT NULL auto_increment comment 'prod_sn',
`kind` smallint(5) unsigned NOT NULL default 0 comment '分類',
`title` varchar(255) NOT NULL default '' comment '名稱',
`summary` text NULL comment '摘要',
`content` text NULL comment '內容',
`price` int(10) unsigned NOT NULL comment '價格',
`amount` int(10) unsigned NOT NULL comment '數量',
`enable` enum('1','0') NOT NULL default '1' comment '狀態',
`choice` enum('1','0') NOT NULL default '0' comment '精選',
`date` int(10) unsigned NOT NULL default 0 comment '建立日期',
`sort` smallint(5) unsigned NOT NULL default 0 comment '排序',
`counter` int(10) unsigned NOT NULL default 0 comment '人氣',
`icon` varchar(255) NOT NULL default '' comment '圖示',
PRIMARY KEY (`sn`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
";
createTable($sql);
}
二、複製商品管理程式、樣板
- admin_prod.php -> admin_news.php
- templates/admin/tpl/admin_prod.tpl -> templates/admin/tpl/admin_news.tpl
- 將關鍵字 prod -> news、商品 -> 新聞
三、修改後台主樣板
- templates/admin/theme.tpl
<{* 新聞管理 *}>
<{elseif $WEB.file_name == "admin_news.php"}>
<{include file="tpl/admin_news.tpl"}>
- templates/admin/tpl/topBar.tpl
<li><a href="admin_news.php">新聞管理</a></li>
四、分析前台樣板
- 製作一個「最新消息區塊」

- index.php 我們借 get_prod()來用
###########################################################
# 撈商品資料
###########################################################
function get_news() {
global $db, $smarty;
#撈商品資料
$sql = "select a.`sn`,a.`title`,a.`summary`,a.`icon`
from `ugm_show_news` as a
where a.`enable`='1'
order by a.`sort` desc
";//die($sql);
$result = $db->query($sql) or redirect_header("", 3000, $db->error."\n".$sql,true);
$rows=[];
while($row = $result->fetch_assoc()){
#過濾資料
$row['sn'] = intval($row['sn']);
$row['title'] = htmlspecialchars($row['title'], ENT_QUOTES);
$row['summary'] = htmlspecialchars($row['summary'], ENT_QUOTES);
$row['icon'] = htmlspecialchars($row['icon'], ENT_QUOTES);
$rows[] = $row;
}
//print_r($rows);die();
$smarty->assign("newss", $rows);//送至樣板
}
- 樣板 templates/creative/theme.tpl
<{foreach from=$newss item=row}>
<div class="col-lg-3 col-md-6 text-center">
<div class="service-box mt-5 mx-auto">
<i class="fa fa-4x <{$row.icon}> text-primary mb-3 sr-icons"></i>
<h3 class="mb-3"><{$row.title}></h3>
<p class="text-muted mb-0"><{$row.summary}></p>
</div>
</div>
<{/foreach}>
-