基本厘米的Php分页


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&amp;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));