


Detailed explanation of the process of turning the previous page to the next page in php
前言
这几天做项目因为数据太多,需要对信息进行上下翻页展示,就自己写了翻页的代码
大致功能就是页面只显示几条信息,按上一页、下一页切换内容,当显示第一页时上一页和首页选项不可选,当页面加载到最后一页时下一页和尾页选项不可选
具体效果如下:
相关学习推荐:PHP编程从入门到精通
实现代码
1)原生PHP方法
先说一下总思路吧,首先我们要查询所有符合条件需要进行分页的总数据,计算展示的总页数。
然后获取当前显示的是第几页信息,用当前页数每页数据条数表示为总数据的第几条,再根据限制条件查询出当前页所需显示出来的数据。将每一条数据echo替换HTML结构内容中,最后显示出来
关于分页的限制条件很简单,只要查询到当前页为第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()每次读取一条信息,得到的是一个索引数组,代码里的$row['id']表示$row里面名为id的值,也可表示为$row.id
connect.php
(连接数据库)
<?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"> <?php include_once('connect.php'); $result = $link->query("select * from news"); $total=$result->rowCount();//查询出来符合条件的总数 $pages=ceil($total/4);//分页的总页数 $num = 4;//每页显示的数据条数 $cPage = $_GET['cPage'];//获取当前是显示的第几页 $start = $cPage * $num;//第一条数据 $table = $link->query("select * from news order by id desc limit {$start},$num"); $link = null;//销毁 while ($row=$table->fetch()){//每次读出一条数据,赋给$row //插入多行文本,把值替换掉 echo <<<_ <li class="media"> <a href="detail.php?id={$row['id']}"> <img class="pull-left lazy" src="/static/imghwm/default1.png" data-src="{$row['src']}" alt="Detailed explanation of the process of turning the previous page to the next page in php" > <figcaption> <h4 id="span-nbsp-class-title-row-title-span-nbsp-span-nbsp-class-news-date-row-time-span"><span class="title">{$row['title']}</span> <span class="news-date">{$row['time']}</span></h4> <p>{$row['content']}</p> </figcaption> </a> </li> _; } ?> </ul>
上下翻页:
<p 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">«首页</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?>"><上一页</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?>">下一页></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?>">尾页»</a></li> <li class="disabled"> <a href="##" id="total"><?php echo ($cPage+1)?>/<?php echo "$pages"?></a> </li> </ul> </p>
2)ajax方法
HTML代码,展示信息装在panel-body
里面
<p class="panel-body" id="content"> <ul id="list" class="media-list"> </ul> </p> <p class="page text-center"> <ul class="pagination" id="page"> <li data-i="0" id="index" class="disabled"><a href="##">«首页</a></li> <li data-i="1" class="disabled"><a href="##"><上一页</a></li> <li data-i="2"><a href="##">下一页></a></li> <li data-i="3" id="end"><a href="##">尾页»</a></li> <li class="disabled"> <a href="##" id="total"></a> </li> </ul> </p> <template id="temp"> //引用模板 <li class="media"> <a href="detail.html?id={id}"> <img class="pull-left lazy" src="/static/imghwm/default1.png" data-src="{src}" alt="Detailed explanation of the process of turning the previous page to the next page in php" > <figcaption> <h4 id="span-nbsp-class-title-title-span-nbsp-span-nbsp-class-news-date-date-span"><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);/*获取信息的总页数*/ }); function show(cPage){//替换每一页的内容 $.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
(连接数据库)
<?php $link=new PDO("mysql:host=localhost;port=3306;dbname=db","root",""); $link->query("set names utf8");
pages.php
(获取总页数)
<?php include_once('connect.php');//连接数据库 $result = $link->query("select * from news"); $row=$result->rowCount(); echo $row;
listmore.php
(获取数据库里的数据)
<?php include_once ('connect.php'); $num = 4;//每一页显示的数据条数 $cPage = $_GET['cPage'];//获取当前页 $start = $cPage * $num;//计算当前页显示的第一条数据的数目 /*从表中查询从开始$start的一共$num条数据*/ $result = $link->query("select * from news order by id desc limit {$start},$num"); $link = null; while ($row=$result->fetch()){/*每一次读取一条数据*/ $json[]=$row;/*把数据赋给json数组*/ } echo json_encode($json);/*把json数组以json格式返回给HTML*/
The above is the detailed content of Detailed explanation of the process of turning the previous page to the next page in php. For more information, please follow other related articles on the PHP Chinese website!

Effective methods to prevent session fixed attacks include: 1. Regenerate the session ID after the user logs in; 2. Use a secure session ID generation algorithm; 3. Implement the session timeout mechanism; 4. Encrypt session data using HTTPS. These measures can ensure that the application is indestructible when facing session fixed attacks.

Implementing session-free authentication can be achieved by using JSONWebTokens (JWT), a token-based authentication system where all necessary information is stored in the token without server-side session storage. 1) Use JWT to generate and verify tokens, 2) Ensure that HTTPS is used to prevent tokens from being intercepted, 3) Securely store tokens on the client side, 4) Verify tokens on the server side to prevent tampering, 5) Implement token revocation mechanisms, such as using short-term access tokens and long-term refresh tokens.

The security risks of PHP sessions mainly include session hijacking, session fixation, session prediction and session poisoning. 1. Session hijacking can be prevented by using HTTPS and protecting cookies. 2. Session fixation can be avoided by regenerating the session ID before the user logs in. 3. Session prediction needs to ensure the randomness and unpredictability of session IDs. 4. Session poisoning can be prevented by verifying and filtering session data.

To destroy a PHP session, you need to start the session first, then clear the data and destroy the session file. 1. Use session_start() to start the session. 2. Use session_unset() to clear the session data. 3. Finally, use session_destroy() to destroy the session file to ensure data security and resource release.

How to change the default session saving path of PHP? It can be achieved through the following steps: use session_save_path('/var/www/sessions');session_start(); in PHP scripts to set the session saving path. Set session.save_path="/var/www/sessions" in the php.ini file to change the session saving path globally. Use Memcached or Redis to store session data, such as ini_set('session.save_handler','memcached'); ini_set(

TomodifydatainaPHPsession,startthesessionwithsession_start(),thenuse$_SESSIONtoset,modify,orremovevariables.1)Startthesession.2)Setormodifysessionvariablesusing$_SESSION.3)Removevariableswithunset().4)Clearallvariableswithsession_unset().5)Destroythe

Arrays can be stored in PHP sessions. 1. Start the session and use session_start(). 2. Create an array and store it in $_SESSION. 3. Retrieve the array through $_SESSION. 4. Optimize session data to improve performance.

PHP session garbage collection is triggered through a probability mechanism to clean up expired session data. 1) Set the trigger probability and session life cycle in the configuration file; 2) You can use cron tasks to optimize high-load applications; 3) You need to balance the garbage collection frequency and performance to avoid data loss.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1
Easy-to-use and free code editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
