PHP MYSQL分页CMS
此代码在另一个主题的回答后更新: 我在一个下午的教程中一直在关注构建CMS CMS工作得很好,但它唯一缺少的是分页。文章存档显示数据库中所有文章的列表,但我希望能够将这些文章分成多个页面。我尝试过几次,但似乎都没能成功。点击下一页链接通常会让我回到主页 我将感谢你的帮助 守则: 文章PHP MYSQL分页CMS,php,mysql,pagination,Php,Mysql,Pagination,此代码在另一个主题的回答后更新: 我在一个下午的教程中一直在关注构建CMS CMS工作得很好,但它唯一缺少的是分页。文章存档显示数据库中所有文章的列表,但我希望能够将这些文章分成多个页面。我尝试过几次,但似乎都没能成功。点击下一页链接通常会让我回到主页 我将感谢你的帮助 守则: 文章 public static function getList( $numRows=130, $order="publicationDate DESC" ) { $conn = new PDO( D
public static function getList( $numRows=130, $order="publicationDate DESC" ) {
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$offset = ($_GET['page'] - 1) * $numRows;
$rowCount = 20;
$sql = "SELECT SQL_CALC_FOUND_ROWS *, UNIX_TIMESTAMP(publicationDate) AS publicationDate FROM articles
ORDER BY " . mysql_escape_string($order) . " LIMIT :offset, :rowCount";
$st = $conn->prepare( $sql );
$st->bindValue( ":offset", $offset, PDO::PARAM_INT );
$st->bindValue( ":rowCount", $rowCount, PDO::PARAM_INT );;
$st->execute();
$list = array();
while ( $row = $st->fetch() ) {
$article = new Article( $row );
$list[] = $article;
}
// Now get the total number of articles that matched the criteria
$sql = "SELECT FOUND_ROWS() AS totalRows";
$totalRows = $conn->query( $sql )->fetch();
$conn = null;
return ( array ( "results" => $list, "totalRows" => $totalRows[0] ) );
}
$st = $conn->prepare( $sql ); $st->bindValue( ":offset", $offset, PDO::PARAM_INT ); $st->bindValue( ":rowCount", $rowCount, PDO::PARAM_INT );;
$st->execute();
$list = array();
while ( $row = $st->fetch() ) {
$article = new Article( $row );
$list[] = $article;
}
// Now get the total number of articles that matched the criteria
$sql = "SELECT FOUND_ROWS() AS totalRows";
$totalRows = $conn->query( $sql )->fetch();
$conn = null;
return ( array ( "results" => $list, "totalRows" => $totalRows[0] ) ); }
阿奎维
<ul class="noticias_archive">
<?php foreach ( $results['articles'] as $article ) { ?>
<li>
<div class="noticias_archive col-4">
<?php if ( $imagePath = $article->getImagePath( IMG_TYPE_THUMB ) ) { ?>
<a href=".?action=viewArticle&articleId=<?php echo $article->id?>"><img class="articleImageThumb" src="<?php echo $imagePath?>" alt="Article Thumbnail" /></a>
<?php } ?></div>
<div class="noticias_archive col-4">
<h3 class="top">
<a href=".?action=viewArticle&articleId=<?php echo $article->id?>"><?php echo htmlspecialchars( $article->title )?></a>
</h3>
<p>
<?php echo htmlspecialchars( $article->summary )?><br><br><?php echo date('j F Y', $article->publicationDate)?></p>
</div>
</li>
<?php } ?>
</ul>
</div>
<p>
<?php
if ( isset( $_GET['page'] ) ){
echo sprintf('<p> <a href="./?action=noticias?page=%d"> << Back </a> </p>', $_GET['page'] - 1);
echo sprintf('<p> <a href="./?action=noticias?page=%d"> Next >> </a> </p>', $_GET['page'] + 1);}?> in total.</p>
<p><a href="./">Return to Homepage</a></p>
请不要创建重复的问题。你已经问了这个问题,但没有被接受的答案:请仔细阅读我的帖子。答案在另一篇文章中有效,但我在更新代码后搞砸了,所以建议我用更新后的代码创建这篇文章。谢谢,你不需要就同一个问题提出两个不同的问题。这两者都与向基于PHP的定制CMS添加分页有关。
require( "config.php" );
$action = isset( $_GET['action'] ) ? $_GET['action'] : "";
switch ( $action ) {
case 'noticias':
noticias();
break;
case 'viewArticle':
viewArticle();
break;
default:
homepage();
}
function noticias() {
$results = array();
$data = Article::getList();
$results['articles'] = $data['results'];
$results['totalRows'] = $data['totalRows'];
$results['pageTitle'] = "Article Archive | teste";
require( TEMPLATE_PATH . "/archive.php" );
}
function viewArticle() {
if ( !isset($_GET["articleId"]) || !$_GET["articleId"] ) {
homepage();
return;
}
$results = array();
$results['article'] = Article::getById( (int)$_GET["articleId"] );
$results['pageTitle'] = $results['article']->title . " | teste2";
require( TEMPLATE_PATH . "/viewArticle.php" );
}
function homepage() {
$results = array();
$data = Article::getList( HOMEPAGE_NUM_ARTICLES );
$results['articles'] = $data['results'];
$results['totalRows'] = $data['totalRows'];
$results['pageTitle'] = "FPT - teste3";
require( TEMPLATE_PATH . "/homepage.php" );
}