![]() |
|
首页 │ Apache │ Linux│ Java│ MySQL│ 注册│帮助 | |||
我(新手)写的最简单的smarty分页显示,见笑分享出来!
conn.php
[php]
include "mysql.php";
/*************** 链接数据库 ************************************/
$db_name="sq"; //数据库名称
$db_user="root"; //数据库用户名
$db_pass=""; //数据库密码
$db_host="localhost"; //数据库地址
$sql = new MySQL_class;
$sql->Setup ($db_user,$db_pass,$db_host);
$sql->Create("$db_name");
/***************************************************************/
[/php]
main.php
[php]
include "class/Smarty.class.php";
include "conn.php";
define('__SITE_ROOT', 'E:/sq/smarty'); // 最後沒有斜線
$tpl = new Smarty();
$tpl->template_dir = __SITE_ROOT . "/templates/";
$tpl->compile_dir = __SITE_ROOT . "/templates_c/";
$tpl->config_dir = __SITE_ROOT . "/configs/";
$tpl->cache_dir = __SITE_ROOT . "/cache/";
$tpl->left_delimiter = '<{';//自己设边界
$tpl->right_delimiter = '}>';
[/php]
test7.php
[php]
require "main.php";
if(isset($_GET['p']))
$curr_page=$_GET['p'];
if(empty($_GET['p']))
$curr_page=1;
$sql->Query('select count(*) from place');
$sql->Fetch(0);
$total=$sql->data[0];//总记录
$row=10;//每页记录数
$totpages=ceil($total/$row);//总页数
$offset=($curr_page-1)*$row;
$sql->Query("select * from place limit $offset,$row");
$array=array();
$info=array();
for($i=0;$i<$sql->rows;$i++){
$sql->Fetch($i);
$data=$sql->data;
$id=$data['id'];
$b_place=$data['b_place'];
$s_place=$data['s_place'];
$youbian=$data['youbian'];
$array=array('id'=>$id,'b_place'=>$b_place,'s_place'=>$s_place,'youbian'=>$youbian);
array_push($info,$array);
}
//var_dump($info);
//fenye
//显示分页
$AtFirstPage=1;
$AtPrePage=$curr_page-1;
$AtNextPage=$curr_page+1;
$AtLastPage=$totpages;
$str='总共'.$totpages.'页,当前为第'.$curr_page.'页,';
if($AtPrePage)
//echo '上一页';
$str.='上一页 | ';
if($AtNextPage)
//echo '下一页';
$str.='下一页';
$tpl->assign('page',$str);
$tpl->assign('arrays',$info);
$tpl->display('test7.tpl');
[/php]
test7.tpl
CODE: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>测试分页</title> </head> <body style="text-align:center"> <table border="1" width="500"> <{section name=sec loop=$arrays}> <tr> <td><{$arrays[sec].id}></td> <td><{$arrays[sec].b_place}></td> <td><{$arrays[sec].s_place}></td> <td><{$arrays[sec].youbian}></td> </tr> <{/section}> <tr> <td colspan="3"><{$page}><td> </tr> </table> </body> </html> |

