$p = intval($_GET['p'])
$p = empty($p) ? '1' : $p
$start = ($p-1)*$a_length
$sql = "SELECT * FROM news WHERE del = 0 ORDER BY updtime DESC LIMIT $start , $a_length"
$news = $mysql ->getAll($sql)
if(!empty($news)){
foreach($news as $k =>&$v){
$v['title']= myCutStr($v['title'] , 40)
$v['content'] = myCutStr($v['content'] , 60)
}
}
$sql = "SELECT COUNT(*) AS num FROM news WHERE del = 0"
$total = $mysql ->getRow($sql)
$page = page($total['num'] , $p ,$a_length)
$smarty ->assign('page' , $page)
$smarty ->assign('news' ,$news)
$smarty ->display('list.html')
page()方法
/**
* 生成分页的字符串,页码的url参数如下: &p=1,2,3
*
* @param int $total_rows 总记录数
* @param int $currentpage 当前第几页
* @param int $page_size 每页几条
* @param int $page_numbers 页码数 10个数字or 20个数字
* @param string $href 链接字符串
* @return a string of pages.
*/
function page($total_rows, $current_page, $page_size=10, $page_numbers=10, $class='', $href_string=''){
if($total_rows <= $page_size) return '' //总记录数 小于 每页显示,为不分页
$current_page = $current_page == 0 ? 1 : $current_page
$pages = intval(($total_rows / $page_size))
if($total_rows % $page_size >0) $pages ++
$start = $current_page - intval($page_numbers / 2)
$end = $current_page + intval($page_numbers / 2)
if($start <= 0) {
$start = 1
$end = $start + $page_numbers -1
}
if($end >$pages) {
$end = $pages
$start = $end - $page_numbers +1
}
if($start <=0) $start = 1
// current_url, remove p=1,2,3
if($href_string == '') {
$href_string = $_SERVER['PHP_SELF'] . '?'
$params = $_SERVER['QUERY_STRING']
$params = str_replace('&', '&', $params)
$params_array = explode('&', $params)
foreach($params_array as $param) {
$index = strpos($param, '=')
$key = substr($param, 0, $index)
if($key != 'p') {
$href_string .= $param . '&'
}
}
$href_string = rtrim($href_string,'&')
}
$str = ""
//build home
if($current_page >1) {
$str .= "<a href='{$href_string}&p=1#ptop' class='{$class}' title='首页'>首页</a> "
} else {
$str .= '<font color="#808080">首页</font> '
}
// build prev page
if($current_page >1) {
$prev_page = $current_page -1
$str .= "<a href='{$href_string}&p={$prev_page}#ptop' class='{$class}' title='上页'>上页</a> "
} else {
$str .= '<font color="#808080">上页</font> '
}
// build page
for($i=$start$i<=$end$i++) {
if($i == $current_page) {
$str .= "<span class = 'current'>{$i}</span>"
} else {
$str .= " <a href='{$href_string}&p={$i}#ptop' class='{$class}' title='第 $i 页'>[{$i}]</a>"
}
}
if($current_page <$pages) {
$next_page = $current_page + 1
if($next_page >$pages) {
$next_page = $pages
}
$str .= "<a href='{$href_string}&p={$next_page}#ptop' class='{$class}' title='下页'>下页</a> "
} else {
$str .= '<font color="#808080">下页</font> '
}
if($current_page <$pages) {
$str .= "<a href='{$href_string}&p={$pages}#ptop' class='{$class}' title='末页'>末页</a>"
} else {
$str .= '<font color="#808080">末页</font>'
}
// page infomation
$str .= <<<EOF
当前是第<font color="red">{$current_page}</font>页,
共有<font color="red">{$pages}</font>页,
<font color="red">{$total_rows}</font>条记录
EOF
return '<font face="宋体">' . $str . '</font>'
}
模板文件直接就是
^$page^
这样就行了
另外条数那个可以声明下$a_length = 10
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)