发新话题
打印

[smarty实例教学]用smarty打造你的留言板(二)

[smarty实例教学]用smarty打造你的留言板(二)

接下来我们先开始写显示留言的程序
-------------------------------------------------------------------------------
为了简要,我们在这里建一个简单的表guestbook,字段三个gb_id,gb_name,gb_content
表建好后,填几条记录方便测试 (附:我这里用的数据库mysql)
接着写个数据库连接程序gb_conn.inc.php放在inc目录下
代码如下:
<?php
mysql_connect("localhost","root","") or die("Sorry,can't connect data server!");
mysql_select_db("test") or die("Sorry,can't open database!");
extract($_GET);
extract($_POST);
?>
再写一个分页处理的函数,写进gb_function.php,文件放在function里
<?php
/*==============================================================================
*
* 文 件 名: gb_function.php
* 程序功能: 自定义函数集
* 更新时间: 2004-09-02
*
* 程序设计: Jzealot
*  E-mail : web-xy@163.com
*
*===============================================================================*/
/*******************************************
函 数 名: pagedeal()
函数功能: 分页处理  
*******************************************/
function  pagedeal($pagesql, $pagesize)
{
    global $recordcount, $pagecount, $page, $startloc;
  
$rs          = mysql_query($pagesql);
$recordcount = mysql_num_rows($rs); //取得记录总数

//******计算总页数*******/
if ( $recordcount % $pagesize == 0 )
     $pagecount = $recordcount / $pagesize;
else
    $pagecount  = intval($recordcount / $pagesize) + 1;
   
if ( empty($page)||$page<1 )//$page是当前页
     $page = 1;
  
if ( $page>$pagecount )
    $page = $pagecount;
   
$startloc = ($page - 1) * $pagesize;//确定当前页的起始记录

    //-----------------当前页的前部分数字分页----------------------//
    if ($page<=5);
    $i = 0;
    if ( $page>5 && $pagecount - $page < 5 && $pagecount>10 )
        $i = $pagecount - 10;
    if ($page>5 && $pagecount - $page >=5)
        $i = $page - 5;
    for (; $i < $page; $i++)
        $numpage[$i] = $i  + 1;

    //--------------当前页的后半部分(包括当前页)数字分页------------//
    $j = $pagecount;
    if ($page<=5 && $pagecount>10)
       $j = 10;
    if ($page>5 && $pagecount - $page >=5)
       $j = $page + 5;
    for (; $i<$j; $i++)
       $numpage[$i] = $i+1;

    return $numpage;//返回数字分页
}
?>
最后我们再写留言显示程序gb_list.php
<?php
/*==============================================================================
*
* 文 件 名: gb_list.php
* 程序功能: 显示留言
* 更新时间: 2004-09-02
*
* 程序设计: Jzealot
*  E-mail : web-xy@163.com
*
*===============================================================================*/
include_once("inc/gb_conn.inc.php");//载入数据库连接&请求设置
include_once("inc/smarty.inc.php");//载入smarty设置
include_once("function/gb_function.php");

//--------------------------------------------------------------------------------
//分页处理查询数据
//--------------------------------------------------------------------------------
$sql      = "select gb_id from guestbook";
$pagesize = 5;
$fpage    = "gb_list.php";
$numpage  = pagedeal($sql,$pagesize);//调用分页处理函数
//----------------------分页相关的模板变量替换-------------------------//  
$smarty -> assign("recordcount", $recordcount);//总记录数
$smarty -> assign("page",        $page);       //当前页
$smarty -> assign("pagecount",   $pagecount);  //总页数
$smarty -> assign("prepage",     $page-1);     //上一页
$smarty -> assign("nexpage",     $page+1);     //下一页
$smarty -> assign("numpage",     $numpage);    //数字分页
$smarty -> assign("fpage",       $fpage);      //要分页的网页

//-------------------------------------------------------------------------------
//查询数据并传递到模板
//-------------------------------------------------------------------------------
$rs  = mysql_query("select * from guestbook limit $startloc,$pagesize");
$num = 0;
while ( $row = mysql_fetch_array($rs) )
{
$rss[$num] = array("gb_name" => $row["gb_name"], "gb_content" => $row["gb_content"]);
$num++;
}
$smarty -> assign("rss",$rss);
$smarty -> display('gb_list.tpl');//显示模板
?>
下次说说模板的写法

相关资源:
[smarty实例教学]用smarty打造你的留言板(一)
[smarty实例教学]用smarty打造你的留言板(二)
[smarty实例教学]用smarty打造你的留言板(三)
[smarty实例教学]用smarty打造你的留言板(四)

[ 本帖最后由 Ajax_chou 于 2007-7-10 21:36 编辑 ]
努力为phpres做贡献
时刻准备着,当机会来临时你就成功了
打好基础,增加社会经验
资深技术工程师是我的梦想
承接各种团体网站外包服务和各种it技术培训
准备申请AJAX版大,希望大家支持~~

TOP

extract($_GET);
extract($_POST);
这两个是干什么用的??查手册也不明白,可否相告?

TOP

gb_connect.php
用一个函数不错
function db_connect() {
        $result = new mysqli('localhost','root','******','test');
        if (!$result) {
                throw new Exception('Could not connect to database server');
        }else{
                return $result;
        }
}

TOP

extract():
extract — 从数组中将变量导入到当前的符号表
int extract ( array $var_array [, int $extract_type [, string $prefix]] )

本函数用来将变量从数组中导入到当前的符号表中。接受结合数组 var_array 作为参数并将键名当作变量名,值作为变量的值。对每个键/值对都会在当前的符号表中建立变量,并受到 extract_type 和 prefix 参数的影响。

TOP

发新话题