Digg样式分页类


Digg Style pagination Class

我是PHP的新手,最近几天我一直在尝试构建我的第一个项目,它主要是作为博客工作的,只是为了让我了解如何查询数据库等等。

我现在想能够在我的页面上对结果进行分页,这就是我目前所拥有的:

<?php
include_once 'inc/functions.inc.php';
include_once 'inc/db.inc.php';
$db = new PDO(DB_INFO, DB_USER, DB_PASS);
$id = (isset($_GET["id"])) ? (int) $_GET["id"] : NULL;
$e = retrievePosts($db, $id);
$fulldisp = array_pop($e);
?>
<div id="blogposts">
    <?php
    if ($fulldisp == 1) {
        ?>
        <span class="postdate"><?php echo $e["date"] ?></span>
        <span class="posttitle"><?php echo $e["title"] ?></span>
        <br/>
        <br/>
        <span class="postbody">
            <?php echo $e["body"] ?>
        </span>
        <?php
    }//end if
    else {
        foreach ($e as $entry) {
            ?>
            <div class="postholder">
                <span class="postdate"><?php echo $entry["date"] ?></span>
                <span class="posttitle">
                    <a href="?id=<?php echo $entry['id'] ?>"><?php echo $entry['title'] ?></a>
                </span>
                <br/>
                <br/>
                <span class="postbody">
        <?php echo $entry["resume"] ?>
                </span>
            </div>
                    <?php
                }//end foreach
            }//end 
            ?>
</div>

这是我的功能:

<?php
function retrievePosts($db, $id = NULL) {
    if (isset($id)) { //se foi fornecido ID
        $sql = "SELECT title, body, date
            FROM blog
            WHERE id=?
            LIMIT 1";
        $stmt = $db->prepare($sql);
        $stmt->execute(array($_GET["id"]));
        $e = $stmt->fetch(); //guardar em array
        $fulldisp = 1; //uma só entrada
    } 
    else 
    {
        $sql = "SELECT id, title, resume, date
            FROM blog
            ORDER BY date DESC";   
        foreach($db->query($sql) as $row) 
        {
            $e[] = array("id" => $row["id"], "title" => $row["title"], "resume" => $row["resume"], "date" => $row["date"]);
        }
        $fulldisp = 0; //multiplas entradas
    }
    //funçoes só retornam um valor, inserimos fulldisp no fim de $e e separamos
    //os valores de novo no index.php
    array_push($e, $fulldisp);
    return $e;
}
?>

它的工作方式是,页面是根据URL构建的:

如果那里有一个id,它只显示该id的内容。

如果URL中没有id,它现在会显示页面中的所有条目。

我现在希望能够对这些条目进行分页,所以在查看了Stackoverflow之后,似乎最流行的解决方案是使用Digg样式的分页类,如下所示:http://mis-algoritmos.com/digg-style-pagination-class

我已经下载了这个类,但我不知道如何让它正常工作,我对这个类没有DB查询这一事实感到困惑(我希望使用LIMIT),我想知道Stack上的任何人是否可以帮助我在代码中实现这个类,或者是否有一个关于如何做到这一点的良好文档来源。

function retrievePosts($db, $id = NULL, $page = 1, $rpp = 5) {
    if (isset($id)) { //se foi fornecido ID
        $sql = "SELECT title, body, date
            FROM blog
            WHERE id=?
            LIMIT 1";
        $stmt = $db->prepare($sql);
        $stmt->execute(array($id));
        $e = $stmt->fetch(); //guardar em array
        $fulldisp = 1; //uma só entrada
    }
    else
    {
        if(!is_numeric($page)) {
            $page = 1;
        }
        if(!is_numeric($rpp)) {
            $rpp = 5;
        }
        $sql = "SELECT id, title, resume, date
            FROM blog
            ORDER BY date DESC
            LIMIT ?
            OFFSET ?
        ";
        $stmt = $db->prepare($sql);
        $stmt->execute(array($page, $rpp * $page));
        foreach($stmt->fetch() as $row)
        {
            $e[] = array("id" => $row["id"], "title" => $row["title"], "resume" => $row["resume"], "date" => $row["date"]);
        }
        $fulldisp = 0; //multiplas entradas
    }
    //funçoes só retornam um valor, inserimos fulldisp no fim de $e e separamos
    //os valores de novo no index.php
    array_push($e, $fulldisp);
    return $e;
}

$rpp-每页记录

我还认为:

$stmt->execute(array($_GET["id"]));

应该是:

$stmt->execute(array($id));