首页 > php教程 > PHP开发 > 正文

利用PHP与Smarty模板技术生成分页静态页面的代码

黄舟
发布: 2016-12-14 11:32:22
原创
1286 人浏览过

lear_all_cache();//清除所有缓存

//smarty.php
//create html file use smarty template
//Rossy.cn@gmial.com
//2007-07-27 11:15

require_once("classes/Smarty.class.php"); //smarty文件
require_once("config.php"); //配置文件
require_once("mysql_class.php"); //数据库连接文件

$smarty = new Smarty();
$smarty->template_dir = "templates";
$smarty->compile_dir  ="templates_c";
$smarty->left_delimiter = "<{";
$smarty->right_delimiter ="}>";

$page_count = "3"; //每一页的记录数
$Db = new Mysql;
$query = $Db->Query("Select * FROM Tablea orDER BYId DESC");
$tatol = $Db->FetchCount($query);
$pages = ceil($tatol / $page_count);
for ($j=1;$j<=$pages;$j++)
$showpages.="".$j."";

for ($i = 0;$i<$pages; $i++)
{
$start_page = $i *$page_count;
if($pages>1)
    {
       $indexpath  ="user_".($i+1).".htm";  //filename
       $showpagestr= "第一页".$showpages." 最尾页";
    }
    else
    {
       $indexpath  = "user.htm"; //file name
       $showpagestr= "";
    }
   
    $sql = "Select * FROM TableaorDER BY ID DESC LIMIT $start_page,$page_count";
    $query =$Db->Query($sql) or die("Invalid Query");
   if(($Db->FetchCount($query))<1){
$Admin ="數據庫沒有此數據";
}else{
while ($rs =$Db->FetchArray($query)) {
          $Admin[] =array('Id'=>$rs['Id'],'Name'=>$rs['Name'],'Nick'=>$rs['Username'],'Type'=>$rs['Type'],'Email'=>$rs['Email']);
       }
    }

   $smarty->assign(array('Site_Name'=>"系统",'AdminName'=>'ybfqlyq','AdminType'=>'Administrator','Type'=>'1'));
   $smarty->assign('Admin',$Admin);
   $smarty->assign('Page_Str',$showpagestr);
    $temp =$smarty->fetch('test.tpl', null, null, false);//模板文件
    unset($Admin);
    $showpagestr = "";
   $fp=fopen($indexpath,'w');
    fwrite($fp,$temp) ordie('create failed');
}
$Db->Close();
echo "create htm file successfully";
?>

test.tpl文件代码:


<{$SiteName}>
href="templates/css.css" type=text/cssrel=stylesheet>



  


       
       
        
       
       
Example of create html file usesmarty template with php

      cellSpacing=0 cellPadding=2 width="100%"align=center border=1>
       
       
        

        
        
        
        
       
       <{sectionname=adminlist loop=$Admin}>1">
          <{$Admin[adminlist].Id}>
          <{$Admin[adminlist].Name}>
          <{$Admin[adminlist].Nick}>
          <{$Admin[adminlist].Type}>
        

       
       <{/section}>
       
        
編號用戶名姓名類別郵件地址
href="mailto:<{$Admin[adminlist].Email}>"><{$Admin[adminlist].Email}>
<{$Page_Str}>

     
       


       Powered by www.ybfq.com     Rossy'sblog:http://www.ybfq.com/blog


     

 

 

 

 

$smartyArr = $smarty->get_template_vars();
      
      //记录总数,每页显示记录条数,总页数
       $pager_Total= count($smartyArr['productID']);
       $pager_Size= 10;
      $pager_Number = ceil($pager_Total/$pager_Size);
       $pager_URL ="index.php?action=View";
      
      //当前页的页数,从REQUEST获得
      if(isset($_GET['pager_PageID'])&&!empty($_GET['pager_PageID'])){
         $pager_PageID = intval($_GET['pager_PageID']);
       }else{
         //第一次访问
         $pager_PageID = 1;
      }       

      //每页的起,始记录数
       if($pager_PageID == 1 ) {
         $pager_StartNum = 0;
       } else{
          $pager_StartNum = ($pager_PageID -1) *$pager_Size;
      }   
      
      $pager_EndNum = $pager_StartNum + $pager_Size;

       if($pager_PageID == 1 &&$pager_Number>1) {
          //第一页
          $pager_Links= "上一页 | 下一页";
       }elseif($pager_PageID == $pager_Number&& $pager_Number>1){
          //最后一页
          $pager_Links = "上一页| 下一页";
       } elseif($pager_PageID > 1&& $pager_PageID <=$pager_Number) {
          //中间
          $pager_Links = "上一页| 下一页";
       } else{
          $pager_Links = "上一页 | 下一页";
       }

       $smarty->assign('pager_Total',$pager_Total);      
      $smarty->assign('pager_StartNum',$pager_StartNum);
      $smarty->assign('pager_EndNum',$pager_EndNum);
      $smarty->assign('pager_Links',$pager_Links);

       return$smarty->fetch("list.tpl");      
?>

 

 

 

include "class/Smarty.class.php";
$tpl = new Smarty();
$tpl->config_dir = __SITE_ROOT . "/configs/";
$tpl->cache_dir = __SITE_ROOT . "/cache/";
$tpl->left_delimiter = '<{';
$tpl->right_delimiter = '}>';
?>



$page_count = "3"; //每一页的记录数
$tatol=$aa->selectpage("news","","");
$pages = ceil($tatol / $page_count);

if($tatol<2)
{
echo "首页 / 上一页/ 下一页 / 末页";
}
if($tatol>2)
  {
   $p=2;
   $e=0;
   $name="首页  /上一页  /  下一页/  末页";
   
   for($pi=0;$pi<=$tatol;$pi++)
{
$start_page = $pi *$page_count;

if(($pi+1)%$page_count==0)
{
$p--;
//生成HTML页面
echo "
".$name;

    $sql ="Select * FROM news orDER BY ID DESC LIMIT$start_page,$page_count";
  //  $query =$Db->Query($sql) or die("Invalid Query");
 $query=$aa->query($sql) ordie("sql error");
   //if(($Db->FetchCount($query))<1){
// $Admin = "數據庫沒有此數據";
// }else{
while ($rs = mysql_fetch_array($query)) {
$Admin[] =array('Id'=>$rs['Id'],'title'=>$rs['title'],'content'=>$rs['content']);
       }
    //}

   $tpl->assign(array('Site_Name'=>"系统",'AdminName'=>'ybfqlyq','AdminType'=>'Administrator','Type'=>'1'));
   $tpl->assign('Admin',$Admin);
   $tpl->assign('Page_Str',$name);
    $temp =$tpl->fetch('test.tpl', null, null, false);//模板文件
   unset($Admin);
 ///unset($name);
   // $showpagestr = "";
   $fp=fopen("news".$p.".html",'w');
   fwrite($fp,$temp) or die('create failed');


    $name="";
    $name.="首页 /  ";
    $name.="上一页 /  ";
    $p++;
    $p++;
     if($p<>$pages+1)
     {
      $name.="下一页 /  ";
      $name.="末页";
     }
     else
     {
      $name.="下一页 /  末页";
     }
    }

   }
   if($tatol%$page_count<>0)
   {
   $name="首页 /  上一页 / 下一页  / 末页";
   
   }  
  
  }

 

//这是生成另一个的代码
//for ($j=1;$j<=$pages;$j++)
// $showpages.= "".$j."";
//
//for ($i = 0;$i<$pages; $i++)
//{
// $start_page = $i *$page_count;
// if($pages>1)
//    {
//       $indexpath   ="user_".($i+1).".htm";  //file name
//       $showpagestr = "第一页".$showpages." 最尾页";
//    }
//    else
//    {
//       $indexpath   = "user.htm"; //filename
//       $showpagestr = "";
//    }
//    
//    $sql ="Select * FROM news orDER BY ID DESC LIMIT$start_page,$page_count";
//  //  $query =$Db->Query($sql) or die("Invalid Query");
// $query=$aa->query($sql) ordie("sql error");
//   //if(($Db->FetchCount($query))<1){
//// $Admin = "數據庫沒有此數據";
//// }else{
// while ($rs = mysql_fetch_array($query)) {
// $Admin[] =array('Id'=>$rs['Id'],'title'=>$rs['title'],'content'=>$rs['content']);
//       }
//    //}
//
//   $tpl->assign(array('Site_Name'=>"系统",'AdminName'=>'ybfqlyq','AdminType'=>'Administrator','Type'=>'1'));
//   $tpl->assign('Admin',$Admin);
//   $tpl->assign('Page_Str',$showpagestr);
//    $temp =$tpl->fetch('test.tpl', null, null, false);//模板文件
//   unset($Admin);
// unset($showpagestr);
//   $showpagestr = "";
//   $fp=fopen($indexpath,'w');
//   fwrite($fp,$temp) or die('create failed');
//}
echo "create htm file successfully";
?>

以上就是利用PHP与Smarty模板技术生成分页静态页面的代码,希望可以帮助大家,更多相关内容请关注PHP中文网(www.php.cn)

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板