<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Smarty {fetch} plugin
 *
 * Type:     function<br>
 * Name:     fetch<br>
 * Purpose:  fetch file, web or ftp data and display results
 * @link http://smarty.php.net/manual/en/language.function.fetch.php {fetch}
 *       (Smarty online manual)
 * @author Monte Ohrt <monte at ohrt dot com>
 * @param array
 * @param Smarty
 * @return string|null if the assign parameter is passed, Smarty assigns the
 *                     result to a template variable
 */

function smarty_function_getpage($params, &$smarty){     
 
	 if (is_null($params)) {
        return;
    }
 extract($params); 
 $order = isset($params['inorder']) ? $params['inorder'] : 'lm_id';
 $PageSize = isset($params['insize']) ? $params['insize'] : 10;
 $toid = isset($params['inid']) ? intval($params['inid']) : 0;
 $lm_nmid = isset($_GET['lm_nmid']) ? $_GET['lm_nmid'] :''; 
 $pid = isset($_GET['lm_rjnmid']) ? $_GET['lm_rjnmid'] : $toid; //获取栏目ID号
 $pstring = "&lm_nmid=".$lm_nmid."&lm_rjnmid=".$pid;
if(!is_numeric($pid)){
return '传入的参数错误,请检查传入的变量';
exit();
	}else{
$pid = intval($pid);
}
$nmsql = "select * from limao_nm where lm_id=$pid limit 1";
$nmrs = mysql_query($nmsql) or die('传入参数错误'.mysql_error());
$nmdata = mysql_fetch_object($nmrs);
if(!$nmdata){
return '传入参数错误';
exit();
	}
$nmtype = $nmdata->lm_error;
switch($nmtype){
	case 1:
	$nmtable = 'limao_article';
  $psql = "select * from $nmtable where lm_type=$pid order by $order desc";
	break;
  case 2:
  $nmtable = 'limao_shop';
  $psql = "select * from $nmtable where lm_type=$pid order by $order desc";
break;
	}
/*获取当前页，总页数，总记录数*/	
$result = mysql_query($psql);
if(!$result){
return '没有找到符合条件的数据';
}
$amount = mysql_num_rows($result);
$pagecount = ceil($amount / $PageSize);	
$page = (isset($_GET['page']) ? $_GET['page'] : 1);
if(is_numeric($page)){                              	
$page = intval($page);
if($page>=$pagecount){
$page = $pagecount;
        }
 if($page<=0){
$page = 1;
 }
}else{
 $page = 1;
    }
	
	//获得分页数据
	if($page==1){   
 $tmp1 = '首页&nbsp;上一页&nbsp;';
       
         }
         else{   	
   $tmp1 = '<a href="?page=1'.$pstring.'">首页</a>&nbsp;<a href="?page='.($page-1).$pstring.'">上一页</a>&nbsp;';
        
        }
  if($page>=5){
  $tmp5 = '<a href="?page='.($page-2).$pstring.'">".($page-2)."</a>&nbsp;<a href="?page='.($page-1).$pstring.'">".($page-1)."</a>';
  	}          
  if($pagecount-$page>=3){
  $tmp4 = '<a href="?page='.($page+1).$pstring.'">".($page+1)."</a>&nbsp;<a href="?page='.($page+2).$pstring.'">".($page+2)."</a>';
  	}      
                  
 if(($page==$pagecount)||($pagecount==0)){
 $tmp2 = '下一页&nbsp;尾页';
          }
          else{
 $tmp2 = '<a href="?page='.($page+1).$pstring.'">下一页</a>&nbsp;<a href="?page='.$pagecount.$pstring.'">尾页</a>';
           
           }
        
 $tmp3 =  "&nbsp;共有".$pagecount."页,当前第".$page."页,每页显示".$PageSize."条记录";	

 $tmpall = $tmp1.$tmp5.$tmp4.$tmp2.$tmp3;
return $tmpall;	
	    
    }  

/* vim: set expandtab: */

?>
