模型类class.article.php
<?php
class Article{
var $_articleInfo;
var $_articleId;
var $_db;
function Article(&$db){
$this->_db = $db;
$this->_articleInfo = array();
$this->_articleId = null;
}
function getArticleById($id){
$sql = "SELECT * FROM Articles WHERE ArticleId='".$id."'";
$this->_db->query($sql);
$this->_db->fetch_array();
$this->_articleInfo = $row;
return $this->_messageInfo;
}
function getArticleList(){
$sql = "SELECT * FROM Articles ORDER BY ArticlePostTime DESC";
//if( $amount > 0 )
//{
//$start = ( $page - 1 ) * $amount;
//$sql .= " LIMIT {$start}, {$amount}";
//}
if ( !$this->_db->query($sql) ){
return false;
}
$rets = array();
while( $row = $this->_db->fetch_array() ){
if($row['ArticleContent'] = substr($row['ArticleContent'],0,20).".......")
array_push( $rets, $row );
}
return $rets;
}
function getTopArticleList(){
$sql = "SELECT TOP 10 * FROM Articles ORDER BY ArticlePostTime DESC";
//if( $amount > 0 )
//{
//$start = ( $page - 1 ) * $amount;
//$sql .= " LIMIT {$start}, {$amount}";
//}
if ( !$this->_db->query($sql) ){
return false;
}
$rets = array();
while( $row = $this->_db->fetch_array() ){
array_push( $rets, $row );
}
return $rets;
}
function delArticle($id){
$sql = "DELETE FROM Articles WHERE ArticleId=".$id;
if( !$this->_db->query($sql) ){
return false;
}
return true;
}
}
?> ;
列表显示类class.articlelist.php
<?php
include_once('class.mysql.php');
include_once('class.article.php');
class ArticleList{
var $_articleInfo;
var $_tpl;
var $_mysql;
function ArticleList(){
$this->_articleInfo = array();
$this->_tpl = &$GLOBALS['tpl'];
$this->_mysql = &new Mysql();
}
function ListArticle($article = array()){
$article = &new Article($this->_mysql);
$articleList = $article->getArticleList();
$this->_tpl->assign('articleList',$articleList);
$this->_tpl->display('index.tpl');
}
}
?> ;
最近10篇显示类class.articletop.php
<?php
include_once('class.mysql.php');
include_once('class.article.php');
class ArticleTop{
var $_articleInfo;
var $_tpl;
var $_mysql;
function ArticleTop(){
$this->_articleInfo = array();
$this->_tpl = &$GLOBALS['tpl'];
$this->_mysql = &new Mysql();
}
function TopArticle($article = array()){
$article = &new Article($this->_mysql);
$articleList = $article->getTopArticleList();
$this->_tpl->assign('articleTop',$articleTop);
$this->_tpl->display('index.tpl');
}
}
?> ;
列表模板articlelist.tpl
<{section name=loop loop=$articleList}>
<div class="article">
<div class="article-title">
<h5><{$articleList[loop].ArticleTitle}></h5><h6>我发表于2007-1-1</h6>
</div>
<div class="article-content">
<{$articleList[loop].ArticleContent}>
</div>
</div>
<{/section}> ;
最近10篇模板left.tpl
<div class="panel">
<h5>Recent Topics</h5>
<div class="panel-content">
<ul>
<{section name=loop loop=$articleTop}>
<li><{$articleTop[loop].ArticleTitle}></li>
<{/section}>
</ul>
</div>
</div> ;
首页模板
<html>
<head>
<title>这是一个foreach使用的例子</title>
<link href="css/css.css" type="text/css" rel="stylesheet"/>
</head>
<body>
<{include file="head.tpl"}>
<div id="contain">
<div id="mainbg">
<div id="right">
<div id="innerright">
<{include file="articlelist.tpl"}>
</div>
</div>
<div id="left">
<div id="innerleft">
<{include file="left.tpl"}>
</div>
</div>
</div>
</div>
<{include file="foot.tpl"}>
</body>
</html> ;
在global.php里已经
global $tpl;
$tpl = &new Template();
为什么在index.php
<?php
define('CMSADMIN_ROOT',dirname(__FILE__).'/');
include_once('global.php');
include_once('Class/class.articletop.php');
include_once('Class/class.articlelist.php');
$listArticle = new ArticleList();
$listArticle->ListArticle();
$topArticle = new ArticleTop();
$topArticle->TopArticle();
?> ;
显示两个模板呢?
该如何设计首页的流程呢?
请指点
也就是说需要把头10条列表显示在left.tpl模板上需要一个页面left.php
所有内容显示在首页上index.tpl也有一个页面index.php
这样可以吗 还是需要怎么设计模板还是程序
不知道我表达清楚了吗
或者哪位有类似smarty应用的复杂点的例子 麻烦给我一份 只用于学习。
[email protected]