国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php實現(xiàn)網(wǎng)頁上一頁下一頁翻頁過程詳解

2024-05-05 00:09:57
字體:
來源:轉載
供稿:網(wǎng)友

前言

這幾天做項目因為數(shù)據(jù)太多,需要對信息進行上下翻頁展示,就自己寫了翻頁的代碼
大致功能就是頁面只顯示幾條信息,按上一頁、下一頁切換內(nèi)容,當顯示第一頁時上一頁和首頁選項不可選,當頁面加載到最后一頁時下一頁和尾頁選項不可選

具體效果如下:

php,網(wǎng)頁,上一頁,下一頁,翻頁

實現(xiàn)代碼

1)原生PHP方法

先說一下總思路吧,首先我們要查詢所有符合條件需要進行分頁的總數(shù)據(jù),計算展示的總頁數(shù)。

然后獲取當前顯示的是第幾頁信息,用當前頁數(shù)每頁數(shù)據(jù)條數(shù)表示為總數(shù)據(jù)的第幾條,再根據(jù)限制條件查詢出當前頁所需顯示出來的數(shù)據(jù)。將每一條數(shù)據(jù)echo替換HTML結構內(nèi)容中,最后顯示出來

關于分頁的限制條件很簡單,只要查詢到當前頁為第1頁時,首頁和上一頁選項跳轉鏈接都固定在第一頁同時設置選項disabled不可選,尾頁也是相同的步驟。

具體代碼如下:

當前頁cPage需要傳過來,我的辦法是初始cPage=0

list.php*

<a href="listmore.php?cPage=0" rel="external nofollow" rel="external nofollow" class="pull-right">更多>></a>$row=$table->fetch()每次讀取一條信息,得到的是一個索引數(shù)組,代碼里的$row['id']表示$row里面名為id的值,也可表示為$row.id

connect.php(連接數(shù)據(jù)庫)

<?php$link=new PDO("mysql:host=localhost;port=3306;dbname=db","root","");$link->query("set names utf8");listmore.php<ul id="list" class="media-list"><?phpinclude_once('connect.php');$result = $link->query("select * from news");$total=$result->rowCount();//查詢出來符合條件的總數(shù)$pages=ceil($total/4);//分頁的總頁數(shù)$num = 4;//每頁顯示的數(shù)據(jù)條數(shù)$cPage = $_GET['cPage'];//獲取當前是顯示的第幾頁$start = $cPage * $num;//第一條數(shù)據(jù)$table = $link->query("select * from news order by id desc limit {$start},$num");$link = null;//銷毀while ($row=$table->fetch()){//每次讀出一條數(shù)據(jù),賦給$row//插入多行文本,把值替換掉echo <<<_<li class="media"><a href="detail.php?id={$row['id']}" rel="external nofollow" ><img class="pull-left" src="{$row['src']}"><figcaption><h4><span class="title">{$row['title']}</span> <span class="news-date">{$row['time']}</span></h4><p>{$row['content']}</p></figcaption></a></li>_;}?></ul>

上下翻頁:

<div class="page text-center"><ul class="pagination" id="page"><li data-i="0" id="index" class="<?php if ($cPage==0) echo 'disabled'; ?>"><a href="listmore.php?cPage=0" rel="external nofollow" rel="external nofollow" >«首頁</a></li><li data-i="1" class="<?php if ($cPage==0) echo 'disabled';?>"><a href="listmore.php?cPage=<?php echo $cPage>0?$cPage-1:0?>" rel="external nofollow" ><上一頁</a></li><li data-i="2" class="<?php if ($cPage==$pages-1) echo 'disabled'?>"><a href="listmore.php?cPage=<?php echo $cPage==($pages-1)?$pages-1:$cPage+1?>" rel="external nofollow" >下一頁></a></li><li data-i="3" id="end" class="<?php if ($cPage==$pages-1) echo 'disabled'?>"><a href="listmore.php?cPage=<?php echo $pages-1?>" rel="external nofollow" >尾頁»</a></li><li class="disabled"><a href="##" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="total"><?php echo ($cPage+1)?>/<?php echo "$pages"?></a></li></ul></div>

2)ajax方法

HTML代碼,展示信息裝在panel-body里面

<div class="panel-body" id="content"><ul id="list" class="media-list"></ul></div><div class="page text-center"><ul class="pagination" id="page"><li data-i="0" id="index" class="disabled"><a href="##" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >«首頁</a></li><li data-i="1" class="disabled"><a href="##" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><上一頁</a></li><li data-i="2"><a href="##" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >下一頁></a></li><li data-i="3" id="end"><a href="##" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >尾頁»</a></li><li class="disabled"><a href="##" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="total"></a></li></ul></div><template id="temp"> //引用模板<li class="media"><a href="detail.html?id={id}" rel="external nofollow" ><img class="pull-left" src="{src}"><figcaption><h4><span class="title">{title}</span> <span class="news-date">{date}</span></h4><p>{content}</p></figcaption></a></li></template>

JS代碼:

var html=$('#temp').html();var curPage=0,pages=0;$.getJSON('php/pages.php',function (res) {pages=Math.ceil(res/4);/*獲取信息的總頁數(shù)*/});function show(cPage){//替換每一頁的內(nèi)容$.getJSON('php/listmore.php',{cPage:cPage},function (json) {var str='';$('#list').empty();json.forEach(function (el) {str+=html.replace('{id}',el.id).replace('{title}',el.title).replace('{src}',el.src).replace('{content}',el.content).replace('{date}',el.time);});$('#list').html(str);});$('#total').html((curPage+1)+'/'+pages);}setTimeout(function () {show(0);},100);$('#page').on('click','li',function () {//上下翻頁,翻遍當前頁的值var i=$(this).data('i');//jquery里特有的獲取data-*屬性的方法switch (i){case 0:curPage=0;break;case 1:curPage>0?curPage--:0;break;case 2:curPage<(pages-1)?curPage++:pages-1;break;case 3:curPage=pages-1;break;}show(curPage);disabled(curPage);})function disabled(curPage) {//關于臨界值禁止選擇if (curPage==0){/*當前頁為第一頁,首頁和上一頁選項禁止點擊*/$('#index').addClass('disabled').next().addClass('disabled');$('#end').removeClass('disabled').prev().removeClass('disabled');} else if (curPage==pages-1){$('#index').removeClass('disabled').next().removeClass('disabled');$('#end').addClass('disabled').prev().addClass('disabled');} else {/*當前頁為最后一頁,尾頁和下一頁選項禁止點擊*/$('#index').removeClass('disabled').next().removeClass('disabled');$('#end').removeClass('disabled').prev().removeClass('disabled');}}

connect.php(連接數(shù)據(jù)庫)

<?php$link=new PDO("mysql:host=localhost;port=3306;dbname=db","root","");$link->query("set names utf8");

pages.php(獲取總頁數(shù))

<?phpinclude_once('connect.php');//連接數(shù)據(jù)庫$result = $link->query("select * from news");$row=$result->rowCount();echo $row;

listmore.php(獲取數(shù)據(jù)庫里的數(shù)據(jù))

<?phpinclude_once ('connect.php');$num = 4;//每一頁顯示的數(shù)據(jù)條數(shù)$cPage = $_GET['cPage'];//獲取當前頁$start = $cPage * $num;//計算當前頁顯示的第一條數(shù)據(jù)的數(shù)目/*從表中查詢從開始$start的一共$num條數(shù)據(jù)*/$result = $link->query("select * from news order by id desc limit {$start},$num");$link = null;while ($row=$result->fetch()){/*每一次讀取一條數(shù)據(jù)*/$json[]=$row;/*把數(shù)據(jù)賦給json數(shù)組*/}echo json_encode($json);/*把json數(shù)組以json格式返回給HTML*/

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關教程知識閱讀請移步到PHP教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 安国市| 苏尼特右旗| 瓦房店市| 林口县| 天镇县| 黄陵县| 屏东市| 宝山区| 屏南县| 永和县| 乌拉特中旗| 安丘市| 方山县| 隆昌县| 定南县| 定襄县| 雅江县| 永和县| 营山县| 察隅县| 迁安市| 上林县| 宁安市| 沙田区| 靖江市| 华宁县| 新河县| 佳木斯市| 黄冈市| 新沂市| 隆化县| 巴马| 陆良县| 宝应县| 高台县| 江门市| 梓潼县| 湾仔区| 行唐县| 乐都县| 油尖旺区|