Php 基本 cms 的分页
Php pagination for basic cms
我有分页问题,我不得不承认我是 php 中编程的新手。
我希望你能告诉我如何为这些代码做分页。
index.php,首页功能。
function homepage() {
$results = array();
$data = Article::getList( HOMEPAGE_NUM_ARTICLES );
$results['articles'] = $data['results'];
$results['totalRows'] = $data['totalRows'];
$results['pageTitle'] = "Widget News";
require( TEMPLATE_PATH . "/homepage.php" );
}
Article.php获取文章功能
public static function getList( $numRows=1000000, $order="publicationDate DESC" ) {
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$sql = "SELECT SQL_CALC_FOUND_ROWS *, UNIX_TIMESTAMP(publicationDate) AS<br> publicationDate FROM articles
ORDER BY " .$order. " LIMIT :numRows";
$st = $conn->prepare( $sql );
$st->bindValue( ":numRows", $numRows, PDO::PARAM_INT );
$st->execute();
$list = array();
while ( $row = $st->fetch() ) {
$article = new Article( $row );
$list[] = $article;
}
index.php 显示文章 foreach
<?php foreach ($results['articles'] as $article){?>
<li>
<h2>
<span class="pubDate"><?php echo date('j F',$article->publicationDate)?></span>
<a href=".?action=viewArticle&articleId=<?php echo $article->id?>">
<?php echo htmlspecialchars($article->title)?>
</a>
</h2>
<p class="summary"><?php echo htmlspecialchars($article->summary)?></p>
</li>
<?php }?>
谢谢。
您需要在 MySql 字符串中使用 OFFSET
。
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$itemsPerPage = 10;
$offset = ($page * $itemsPerPage) - $itemsPerPage;
$sql = "SELECT * FROM articles OFFSET {$offset} LIMIT {$itemsPerPage}";
同时从您的代码中删除 <br>
。只对HTML有效,其他地方会报错
最终函数(开始)如下所示:
public static function getList($numRows = 10, $order = "publicationDate DESC") {
$conn = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$order = preg_replace("/[^a-zA-Z\s]/", '', $order);
$numRows = intval($numROws);
$offset = ($page * $numRows) - $numRows;
$sql = "SELECT
SQL_CALC_FOUND_ROWS *,
UNIX_TIMESTAMP(`publicationDate`) AS 'publicationDate'
FROM `articles`
ORDER BY {$order}
LIMIT :numRows
OFFSET :offset";
$st = $conn->prepare( $sql );
$st->execute(array(':numRows' => $numRows, ':offset' => $pffset));
我有分页问题,我不得不承认我是 php 中编程的新手。
我希望你能告诉我如何为这些代码做分页。
index.php,首页功能。
function homepage() {
$results = array();
$data = Article::getList( HOMEPAGE_NUM_ARTICLES );
$results['articles'] = $data['results'];
$results['totalRows'] = $data['totalRows'];
$results['pageTitle'] = "Widget News";
require( TEMPLATE_PATH . "/homepage.php" );
}
Article.php获取文章功能
public static function getList( $numRows=1000000, $order="publicationDate DESC" ) {
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$sql = "SELECT SQL_CALC_FOUND_ROWS *, UNIX_TIMESTAMP(publicationDate) AS<br> publicationDate FROM articles
ORDER BY " .$order. " LIMIT :numRows";
$st = $conn->prepare( $sql );
$st->bindValue( ":numRows", $numRows, PDO::PARAM_INT );
$st->execute();
$list = array();
while ( $row = $st->fetch() ) {
$article = new Article( $row );
$list[] = $article;
}
index.php 显示文章 foreach
<?php foreach ($results['articles'] as $article){?>
<li>
<h2>
<span class="pubDate"><?php echo date('j F',$article->publicationDate)?></span>
<a href=".?action=viewArticle&articleId=<?php echo $article->id?>">
<?php echo htmlspecialchars($article->title)?>
</a>
</h2>
<p class="summary"><?php echo htmlspecialchars($article->summary)?></p>
</li>
<?php }?>
谢谢。
您需要在 MySql 字符串中使用 OFFSET
。
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$itemsPerPage = 10;
$offset = ($page * $itemsPerPage) - $itemsPerPage;
$sql = "SELECT * FROM articles OFFSET {$offset} LIMIT {$itemsPerPage}";
同时从您的代码中删除 <br>
。只对HTML有效,其他地方会报错
最终函数(开始)如下所示:
public static function getList($numRows = 10, $order = "publicationDate DESC") {
$conn = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$order = preg_replace("/[^a-zA-Z\s]/", '', $order);
$numRows = intval($numROws);
$offset = ($page * $numRows) - $numRows;
$sql = "SELECT
SQL_CALC_FOUND_ROWS *,
UNIX_TIMESTAMP(`publicationDate`) AS 'publicationDate'
FROM `articles`
ORDER BY {$order}
LIMIT :numRows
OFFSET :offset";
$st = $conn->prepare( $sql );
$st->execute(array(':numRows' => $numRows, ':offset' => $pffset));