php如何实现翻页显示

php如何实现翻页显示,第1张

<? 

//设置当前页显示的数量(这个数量可任意设置) 

$limit=20 

//初始化数据库搜索起始记录 

if (!empty($start)) $start=0 

mysql_connect("localhost","","") 

mysql_select_db(database) 

//设置数据库记录总数 

$result=mysql_query("select * from table") 

$num_max=mysql_numrows($result) 

$result=mysql_query("select * from table order by id desc limit $start,$limit) 

$num=mysql_numrows($result) 

echo "<table><tr><td>翻页功能</td></tr>" 

if (!empty($num)) { 

for ($i=0$i<$num$i++) { 

$val=mysql_result($result,$i,"val") 

$val1=mysql_result($result,$i,"val1") 

echo "<tr><td>$val</td><td>$val1</td></tr>" 

echo "<tr><td>" 

//设置向前翻页的跳转 

$prve=$start-$limit 

if ($prve>=0) { 

echo "<a href=page.php?start=$prve>prve</a>" 

//设置向后翻页的跳转 

$next=$start+$limit 

if ($next<$num_max) { 

echo "<a href=page.php?start=$next>next</a>" 

echo "</td></tr></table>" 

?>

建议你先还是用其他办法作吧!我从phpaaCMS弄了个分页·蛮好的:

<?php

include("conn.php")

function selectLimit($sql, $numrows=-1, $offset=-1) {

if($offset==-1){

$sql .= ' LIMIT ' . $numrows

}else{

$sql .= ' LIMIT ' . $offset . ', ' . $numrows

}

$res = mysql_query ( $sql )

if ($res !== false) {

$arr = array ()

$row = mysql_fetch_assoc ( $res )

while ($row) {

$arr [] = $row

$row = mysql_fetch_assoc ( $res )

}

return $arr

} else {

return false

}

}

function getArticleList($str=''){

global $db

$curpage = empty($_GET['page'])?0:($_GET['page']-1)

//定义默认数据

$init_array =array(

'row' =>0,

'titlelen' =>0,

'keywords' =>0,

'type' =>'',

'cid' =>'',

'order' =>'id',

'orderway' =>'desc'

)

//用获取的数据覆盖默认数据

$str_array = explode('|',$str)

foreach($str_array as $_str_item){

if(!empty($_str_item)){

$_str_item_array = explode('=',$_str_item)

if(!empty($_str_item_array[0])&&!empty($_str_item_array[1])){

$init_array[$_str_item_array[0]]=$_str_item_array[1]

}

}

}

//定义要用到的变量

$row = $init_array['row']

/* $titlelen = $init_array['titlelen']

$keywords = $init_array['keywords']

$type = $init_array['type']

$cid = $init_array['cid']

$order = $init_array['order']

$orderway = $init_array['orderway']*/

//文章标题长度控制

if(!empty($titlelen)){

$title="substring(a.title,1,".$titlelen.") as title"

}else{

$title="a.title"

}

//根据条件数据生成条件语句

$where = ""

if(!empty($cid)){

$where .= " and a.cid in (".$cid.")"

}else{

if(isset($_GET['id'])&&!empty($_GET['id'])&&is_numeric($_GET['id'])){

$where .= " and a.cid in (".$_GET['id'].")"

}

}

if($type=='image'){

$where .= " and a.pic is not null"

}

if(!empty($keywords)){

$where .= " and a.title like '".$keywords."%' or a.content like '".$keywords."%'"

}

$sql = "select * from news order by datetime desc"

global $pageList

$query = mysql_query("SELECT * FROM `news`")

$total = mysql_num_rows($query)

$pageList['pagination_total_number'] = $total

$pageList['pagination_perpage'] = empty($row)?$pageList['pagination_total_number']:$row

return selectLimit($sql,$pageList['pagination_perpage'],$curpage*$row)

}

function getArticleInfo($page_url,$page = 8) {

global $pageList

//当前第几页

$curpage = empty($_GET['page'])?1:$_GET['page']

$realpages = 1

if($pageList['pagination_total_number'] >$pageList['pagination_perpage']) {//需要分页

$offset = 2

//实际总分页数

$realpages = @ceil($pageList['pagination_total_number'] / $pageList['pagination_perpage'])

$pages = $realpages

if($page >$pages) {

$from = 1

$to = $pages

} else {

$from = $curpage - $offset

$to = $from + $page - 1

if($from <1) {

$to = $curpage + 1 - $from

$from = 1

if($to - $from <$page) {

$to = $page

}

} elseif($to >$pages) {

$from = $pages - $page + 1

$to = $pages

}

}

$phpaa_page = ''

$page_url .= strpos($page_url, '?') ? '&' : '?'

$phpaa_page = ($curpage - $offset >1 &&$pages >$page ? '<a href="'.$page_url.'page=1" class="first">首页</a>' : '').

($curpage >1? '<a href="'.$page_url.'page='.($curpage - 1).'" class="prev">上一页</a>' : '上一页')

for($i = $from$i <= $to$i++) {

$phpaa_page .= $i == $curpage ? '<strong style="color:#ffa000">'.$i.'</strong>' :

'<a href="'.$page_url.'page='.$i.($i == $pages ? '#' : '').'">'.$i.'</a>'

}

$phpaa_page .= ($to <$pages ? '<a href="'.$page_url.'page='.$pages.'" class="last">...'.$pages.'</a>': '')

$phpaa_page .= ($curpage <$pages ? '<a href="'.$page_url.'page='.($curpage + 1).'" class="next">下一页</a>' : '下一页')

$phpaa_page .= ($to <$pages ? '<a href="'.$page_url.'page='.$pages.'" class="last">尾页</a>': '')

$phpaa_page = $phpaa_page ? '<div class="pages">共 '.$pageList['pagination_total_number'].' 条 '.$phpaa_page.'</div>' : ''

}

return $phpaa_page

}

?>

<?php foreach(getArticleList("cid=".$_GET['id']."|row=2") as $list){?>//需要分多少页

<tr>

<td height="30" align="left"><a href="html/<?php echo $list['id'].".html"?>" target="_blank"><?php echo $list['title']?></a></td><br>

</tr>

<?php

}

?>

<?php echo getArticleInfo("fenye.php?id=".$_GET['id'])?>//你所需要分页页面的url


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/6097518.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-14
下一篇 2023-03-14

发表评论

登录后才能评论

评论列表(0条)

保存