1. 程式人生 > >PDO分頁 url重寫

PDO分頁 url重寫

要求

  1. 寫程式碼新增 條資料,PDO入庫(標題不可重複
  2. 分頁功能實現,頁面連結為list_頁碼.html

寫一個正則(連結地址的正則) 新建一個  .htaccess  裡面下一下內容

展示的程式碼

<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/9/14
 * Time: 8:57
 */
$page = empty($_GET['page'])?1:$_GET['page'];
$pdo = new PDO('mysql:host=localhost;dbname=musicl;charset=utf8;','root','root');
$sql = "select count(*) from text";
$count = $pdo->query($sql)->fetchAll();
//條數
$count = $count[0]['count(*)'];
$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['name']?></td>
            <td><?php echo $v['author']?></td>
            <td><?php echo $v['link']?></td>
            <td><?php echo $v['content']?></td>
            <td><?php echo $v['time']?></td>
        </tr>
    <?php }?>
</table>
<a href="list_<?php echo $page-1?>.html">上一頁</a>
<a href="list_<?php echo $page+1?>.html">下一頁</a>
</body>
</html>