Php Digg样式分页类
我是PHP新手,最近几天我一直在尝试构建我的第一个项目,它主要是作为一个博客来工作的——只是为了让我感觉到如何查询数据库等等 我现在希望能够在我的页面上对结果进行分页,这是我目前所拥有的:Php Digg样式分页类,php,digg,Php,Digg,我是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
<?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样式的分页类,如下所示:
我已经下载了这个类,但我不知道如何让它正常工作,我被这个类没有DB查询的事实弄糊涂了(我希望这会使用LIMIT),我想知道堆栈上是否有人可以帮助我在我的代码中实现这个类,或者是否有一个关于如何实现这个类的文档化的源代码
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));