///////view_item类库///////////////
<?php/* this class will bind data to html and output 
**
*/
class view_item
{
var $item; //instance of item class
var $output; //output rendered HTML for display

//constructor
function view_item(&$item)
{
$this->item = &$item; 
}

//display a single item
function item_details($id=1)
{
$this->item->list_item($id);
while ($this->item->get_item())
{
//bind to HTML
$this->output  = "<table width='100%' cellspacing='1' cellpadding='5' bgcolor='669ACC'>";
$this->output .= "<tr align='center' class='td'><td><b>信息类型</td><td>".$this->item->mode."</td></tr>";
$this->output .= "<tr align='center' class='td'><td><b>设备编号</b></td><td>".$this->item->id."</td></tr>";
$this->output .= "<tr align='center' class='td'><td><b>设备品牌</b></td><td>".$this->item->brand."</td></tr>";
$this->output .= "<tr align='center' class='td'><td><b>设备型号</b></td><td>".$this->item->type."</td></tr>";
$this->output .= "<tr align='center' class='td'><td><b>发&nbsp;布&nbsp;人</b></td><td>".$this->item->poster."</td></tr>";
$this->output .= "<tr align='center' class='td'><td><b>发布时间</b></td><td>".$this->item->postime."</td></tr>";
$this->output .= "<tr align='center' class='td'><td><b>备&nbsp;&nbsp;&nbsp;&nbsp;注</b></td><td>".$this->item->others."</td></tr>";
$this->output .= "</table>";
}
return $this->output;
}
//build a item list table 
function item_list($start=0, $mode="")
{
$num_ppage    = 5;
$total_page   = ceil($this->item->total_item($mode) / $num_ppage);
//allways set $start to a multiple of $num_ppage;
if ($start < $num_ppage)
$start = 0;
else
$start = $start - ($start % $num_ppage);
$current_page = $start / $num_ppage + 1;
$first_page   = 0;
$last_page    = ($total_page - 1) * $num_ppage;
//echo $start;
$this->item->list_items($start, $num_ppage, $mode);
$this->output = "<table width='100%' cellpadding='10'>";
while ($this->item->get_item())
{
//bind to HTML
$this->output .= "<tr><td><table width='100%' cellspacing='1' bgcolor='669ACC'>";
$this->output .= "<tr><td class='td' align='center'><b>设备编号</b></td><td class='td' align='center'>".$this->item->id."</td><td class='td' align='center'><b>信息类型</b></td><td class='td' align='center'>".$this->item->mode."</td></tr>";
$this->output .= "<tr><td class='td' align='center'><b>设备品牌</b></td><td class='td' align='center'>".$this->item->brand."</td><td class='td' align='center'><b>设备型号</b></td><td class='td' align='center'>".$this->item->type."</td></tr>";
$this->output .= "<tr><td class='td' align='center'><b>发&nbsp;布&nbsp;人</b></td><td class='td' align='center'>".$this->item->poster."</td><td class='td' align='center'><b>发布时间</b><td  class='td' align='center'>".$this->item->postime."</td></tr>";
$this->output .= "<tr><td class='td' align='center'><b>备&nbsp;&nbsp;&nbsp;&nbsp;注</b></td><td class='td' align='center' colspan='3'>".$this->item->others."</td></tr>";
$this->output .= "<tr><td class='td'  colspan='4'>&nbsp;<img src='images/bullet_".$mode.".gif'>&nbsp;<a href='item_details.php?id=".$this->item->id."'><font color='red'>点击此处查看详细信息</font></a></td></tr>";
$this->output .= "</table></td></tr>";
}

$this->output .= "<tr><td><table width='100%' cellspacing='1' bgcolor='669ACC'><td class='td_index' align='center'>";
$this->output .= "当前总共&nbsp;<font color='red'>".$this->item->total_item($mode)."</font>&nbsp;条信息";
$this->output .= "&nbsp;&nbsp;共&nbsp;<font color='red'>".$total_page."</font>&nbsp;页";
$this->output .= "&nbsp;&nbsp;当前为第&nbsp;<font color='red'>".$current_page."</font>&nbsp;页";
if (0 == $start)//first page
{
$this->output .= "&nbsp;&nbsp;首页";
$this->output .= "&nbsp;&nbsp;上一页";
}
else
{
$this->output .= "&nbsp;&nbsp;<a href='".$_SERVER['PHP_SELF']."?start=".$first_page."'>首页</a>";
$this->output .= "&nbsp;&nbsp;<a href='".$_SERVER['PHP_SELF']."?start=".($start-$num_ppage)."'>上一页</a>";
}
if ($this->item->total_item($mode) <= ($start + $num_ppage))//last page
{
$this->output .= "&nbsp;&nbsp;下一页";
$this->output .= "&nbsp;&nbsp;尾页";
}
else
{
$this->output .= "&nbsp;&nbsp;<a href='".$_SERVER['PHP_SELF']."?start=".($start+$num_ppage)."'>下一页</a>";
$this->output .= "&nbsp;&nbsp;<a href='".$_SERVER['PHP_SELF']."?start=".$last_page."'>尾页</a>";
}
$this->output .= "</td></tr></table></td></tr></table>";
return $this->output;
}
/*this function is only used by index.php
**list 4 item  return 0 for error
*/
function list_index($mode)
{
$start = 0;
$rows  = 4;
$this->output = "<table class = 'table'>";
$this->item->list_items($start, $rows, $mode);
while ($this->item->get_item())
{
$this->output .= "<tr><td><img src='images/bullet_".$mode.".gif'>&nbsp;<a href='item_details.php?id=".$this->item->id."'>";
if ($this->item->mode)
$this->output .= $this->item->mode."-";
if ($this->item->brand)
$this->output .= $this->item->brand."-";
if ($this->item->type)
$this->output .= $this->item->type."&nbsp;&nbsp;&nbsp;&nbsp;";
$this->output .= $this->item->postime;
$this->output .="</a></td><tr>";
}
$this->output .= "</table>";
return $this->output;
}
//output rendered HTML
function show()
{
return $this->output;
}

}
?>////////控制器部分/////////////////只显示信息类型为出售的信息
<?php
//index page use /template/index.htm as its HTML template
require_once("config.inc.php");
require_once("class/template.inc.php");
require_once("class/mysql.inc.php");
require_once("class/item.inc.php");
require_once("class/view_item.inc.php");//initialize the class object
$db        = & new mysql();
$item      = & new item($db);
$view_item = & new view_item($item);
if (!isset($HTTP_GET_VARS['start']))
$start = 0;
else
$start = $HTTP_GET_VARS['start'];
$mode = 'sale';
$data = $view_item->item_list($start, $mode);
//output use template
$tpl  = new template($TEMPLATE_DIR);
$tpl->set_file('itemsale', 'itemsale.htm');
$tpl->set_var(array('bulletin' => nl2br($BULLETIN_DATA),
'help'     => nl2br($HELP_DATA),
'sale'     => $data));
$tpl->pparse('output', 'itemsale');
?>

解决方案 »

  1.   

    请问一下页面的执行时间是多少?
    我在本地调试时的结果
    MVC模式    10ms左右
    模块方式    2ms左右
    直接嵌入   0.1ms以下你的作法我想是受到了CSDN上的那篇经典的文章的启发,当初我也试过用其方法但其效果却不尽如人意。
    对于MVC模式个人看法是以牺牲执行时间为代价来换取程序的良好结构。实现容易但效率不高。
    我建议对php下的MVC模式进行一些修改,以提高效率。
      

  2.   

    这个我倒没试
    但是直接嵌入后对修改程序带来了很大麻烦
    可能使用SMARTY做控制器部分会好些。毕竟不用做二次编译了
    我对SMARTY不熟,有谁熟的可以帮助改进一下程序
      

  3.   

    你尝试一下 把显示层(view_item类库)改为用phplib 中的 template 类来实现,可能快些。你这样直接把html嵌到里面也太难看了,
    还有 你加上 cash 就会快些。