【问题标题】:Digg Style pagination ClassDigg 风格分页类
【发布时间】:2012-09-14 11:12:21
【问题描述】:

我是 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

我已经下载了这个类,但我不知道如何让它正常工作,我对这个类没有数据库查询这一事实感到困惑(我希望它使用 LIMIT ),我想知道 Stack 上的任何人是否可以帮助我在我的代码中实现这个类,或者是否有详细记录的资源来说明如何做到这一点。

【问题讨论】:

    标签: php digg


    【解决方案1】:
    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));
    

    【讨论】:

      猜你喜欢
      • 2015-10-22
      • 1970-01-01
      • 2020-12-05
      • 1970-01-01
      • 1970-01-01
      • 2016-04-15
      • 2015-08-31
      • 2016-09-26
      • 2016-10-25
      相关资源
      最近更新 更多