Home > php教程 > PHP开发 > Use PHP and Smarty template technology to generate code for paging static pages

Use PHP and Smarty template technology to generate code for paging static pages

黄舟
Release: 2016-12-14 11:32:22
Original
1303 people have browsed it

lear_all_cache();//Clear all caches

//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();
​​ //Total number of records, display records on each page Number of items, total number of pages
                $pager_Total= count($smartyArr['productID']);                                                                                                          =View";
                                                                                                                                                                                                                                                           . $_GET['pager_PageID']);
                                                                                                                                                                                               if($pager_PageID == 1 ) {
           $pager_StartNum = 0;
                                                                                                               $pager_Start        = $pager_StartNum + $pager_Size;

if($pager_PageID == 1 &&$pager_Number> 1) {
// Page
$ Pager_LINKS = "Previous page | & lt; ahref =". $ Page_url. "& Pager_pageid =". ($ Pages_pageid+1). "& Gt; ";
          }elseif($pager_PageID == $pager_Number&& $pager_Number>1){
                                                                                                                                        . ">Previous page| Next page";
           } elseif($pager_PageID                                                                                                                                                  . $pager_URL."&pager_PageID=".($pager_PageID-1).">Previous page| assign('pager_StartNum',$pager_StartNum);
            $smarty->assign('pager_EndNum',$pager_EndNum); 
                                           return$ smarty->fetch("list.tpl");
$tpl->config_dir = __SITE_ROOT . "/configs/";
$tpl->cache_dir = __SITE_ROOT . "/cache/";
$tpl->left_delimiter = '<{';
$tpl- >right_delimiter = '}>';
?>



$page_count = "3"; //Number of records per page
$ tatol=$aa->selectpage("news","","");
$pages = ceil($tatol / $page_count);

if($tatol<2)
{
echo "Home/Previous page/Next page/Last page";
}
if($tatol>2)
{
$p=2;
$e=0 ;
$name="Home Page /Previous Page / Next Page/ End Page & lt;/a & gt; ";

for ($ pi = 0; $ pi & lt; = $ tatol; $ pi ++) {
$ start_page = $ pi *$ page_count;

(($ pi+1)% $page_count==0)
{ $page_count";
// $query =$Db->Query($sql) or die("Invalid Query");
$query=$aa->query($sql) ordie("sql error") ;
//if(($Db->FetchCount($query))<1){
// $Admin = "The database does not have this data";

// }else{

while ($rs = mysql_fetch_array( $query)) {
     $Admin[] =array('Id'=>$rs['Id'],'title'=>$rs['title'],'content'=>$rs[ 'content']);
}
//}

$tpl->assign(array('Site_Name'=>"System",'AdminName'=>'ybfqlyq','AdminType'=>' Administrator','Type'=>'1'));
$tpl->assign('Admin',$Admin);
$tpl->assign('Page_Str',$name);
$temp =$tpl->fetch('test.tpl', null, null, false);//Template file
unset($Admin);

///unset($name);

// $showpagestr = "" ;
$fp=fopen("news".$p.".html",'w');
fwrite($fp,$temp) or die('create failed');


$name="" ;
$name.="Homepage / ";
$name.="Previous page / ";
$p++;

$p++;

if($p<>$pages+1)
{
$name.="Next page / ";
                                                                                                                                                                                                    Next page else
                                                                                                                                                                                            $name.=" html>Homepage / Previous page / Next page / Last page";

}

}


//这是生成另一个的代码
//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)

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template