基本cms的Php分页
基本cms的Php分页,php,pdo,pagination,Php,Pdo,Pagination,我对分页有问题,我必须承认我是php编程的新手。 希望您能告诉我如何为这些代码分页。 php,主页功能 function homepage() { $results = array(); $data = Article::getList( HOMEPAGE_NUM_ARTICLES ); $results['articles'] = $data['results']; $results['totalRows'] = $data['totalRows']; $results['
我对分页有问题,我必须承认我是php编程的新手。
希望您能告诉我如何为这些代码分页。
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;
}
公共静态函数getList($numRows=1000000,$order=“publicationDate DESC”){
$conn=新PDO(DB_DSN、DB_用户名、DB_密码);
$sql=“从文章中选择sql\u CALC\u FOUND\u ROWS*,UNIX\u时间戳(publicationDate)作为publicationDate 按“$ORDER.”订购。限额:numRows; $st=$conn->prepare($sql); $st->bindValue(“:numRows”,$numRows,PDO::PARAM_INT); $st->execute(); $list=array(); 而($row=$st->fetch()){ $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}";
同时从代码中删除
。它只对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));
我已删除
,如何用您的代码更新我的代码?以及如何进行分页,如1,2,3,4,5@AlpÖzkan分页只是与不同的&page=#
参数链接。感谢您的努力,您已经解决了我的问题