Home > Web Front-end > JS Tutorial > body text

Implement PDO paging and url rewriting

坏嘻嘻
Release: 2018-09-14 16:08:43
Original
1662 people have browsed it

PDO (PHP Data Objects) defines a lightweight unified interface in PHP that can be used to access different types of databases.

Requirements

  1. Write code to add a piece of data, PDO into the database (the title cannot be repeated

  2. Paging function is implemented, page link Write a regex for list_page number.html

(regular link address) Create a new .htaccess with the next content

Implement PDO paging and url rewriting

Displayed code

<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/9/14
 * Time: 8:57
 */
$page = empty($_GET[&#39;page&#39;])?1:$_GET[&#39;page&#39;];
$pdo = new PDO(&#39;mysql:host=localhost;dbname=musicl;charset=utf8;&#39;,&#39;root&#39;,&#39;root&#39;);
$sql = "select count(*) from text";
$count = $pdo->query($sql)->fetchAll();
//条数
$count = $count[0][&#39;count(*)&#39;];
$limit = 3;
//总页数
$numpage = ceil($count/$limit);
//偏移量
$last  = ($page-1)*$limit;
if($page<1)
{
    $page=1;
    return false;
}

if($page>$numpage)
{
    $page=$numpage;
    return false;
}
$sql = "select * from text limit $last,$limit";
$data=$pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<table border="1">
    <tr>
        <td>标题</td>
        <td>作者</td>
        <td>链接</td>
        <td>内容</td>
        <td>时间</td>
    </tr>
    <?php foreach($data as $v){?>
        <tr>
            <td><?php echo $v[&#39;name&#39;]?></td>
            <td><?php echo $v[&#39;author&#39;]?></td>
            <td><?php echo $v[&#39;link&#39;]?></td>
            <td><?php echo $v[&#39;content&#39;]?></td>
            <td><?php echo $v[&#39;time&#39;]?></td>
        </tr>
    <?php }?>
</table>
<a href="list_<?php echo $page-1?>.html">上一页</a>
<a href="list_<?php echo $page+1?>.html">下一页</a>
</body>
</html>
Copy after login

Related recommendations:

PDO--PHPDataObjects

php pdo insert and pdo insertId usage

The above is the detailed content of Implement PDO paging and url rewriting. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!