2015-04-23 27 views
1

我一直在关注如何在下午教程中编译CMS http://www.elated.com/articles/cms-in-an-afternoon-php-mysql/如何为我的CMS分页

该CMS的伟大工程,但它唯一缺乏的是分页。文章存档显示数据库中所有文章的列表,但我希望能够将这些文章分隔成多个页面。我已经尝试了几次,但似乎无法让它工作。点击下一页链接通常会将我带回主页。

我会apreciate您的帮助

代码:

的config.php

<?php ini_set("display_errors", true); 
date_default_timezone_set("europe/lisbon"); 
define("DB_DSN", "mysql:host=localhost;dbname=cms"); 
define("DB_USERNAME", "username"); 
define("DB_PASSWORD", "password"); 
define("CLASS_PATH", "classes"); 
define("TEMPLATE_PATH", "templates"); 
define("HOMEPAGE_NUM_ARTICLES", 5); 
define("ADMIN_USERNAME", "admin"); 
define("ADMIN_PASSWORD", "mypass"); 
require(CLASS_PATH . "/Article.php"); 

function handleException($exception) { 
echo "Sorry, a problem occurred. Please try later."; 
error_log($exception->getMessage()); } 

set_exception_handler('handleException'); ?> 

archive.php

<?php include "templates/include/header.php" ?> 

     <h1>Article Archive</h1> 

     <ul id="headlines" class="archive"> 

<?php foreach ($results['articles'] as $article) { ?> 

     <li> 
      <h2> 
      <span class="pubDate"><?php echo date('j F Y', $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 } ?> 

     </ul> 

     <p><?php echo $results['totalRows']?> article<?php echo ($results['totalRows'] != 1) ? 's' : '' ?> in total.</p> 

     <p><a href="./">Return to Homepage</a></p> 

<?php include "templates/include/footer.php" ?> 

article.php

<?php 

/** 
* Class to handle articles 
*/ 

class Article 
{ 
    public $id = null; 

    public $publicationDate = null; 

    public $title = null; 

    public $summary = null; 

    public $content = null; 


    public function __construct($data=array()) { 
    if (isset($data['id'])) $this->id = (int) $data['id']; 
    if (isset($data['publicationDate'])) $this->publicationDate = (int) $data['publicationDate']; 
    if (isset($data['title'])) $this->title = preg_replace ("/[^\.\,\-\_\'\"\@\?\!\:\$ a-zA-Z0-9()]/", "", $data['title']); 
    if (isset($data['summary'])) $this->summary = preg_replace ("/[^\.\,\-\_\'\"\@\?\!\:\$ a-zA-Z0-9()]/", "", $data['summary']); 
    if (isset($data['content'])) $this->content = $data['content']; 
    } 


    public function storeFormValues ($params) { 

    // Store all the parameters 
    $this->__construct($params); 

    // Parse and store the publication date 
    if (isset($params['publicationDate'])) { 
     $publicationDate = explode ('-', $params['publicationDate']); 

     if (count($publicationDate) == 3) { 
     list ($y, $m, $d) = $publicationDate; 
     $this->publicationDate = mktime (0, 0, 0, $m, $d, $y); 
     } 
    } 
    } 


    public static function getById($id) { 
    $conn = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD); 
    $sql = "SELECT *, UNIX_TIMESTAMP(publicationDate) AS publicationDate FROM articles WHERE id = :id"; 
    $st = $conn->prepare($sql); 
    $st->bindValue(":id", $id, PDO::PARAM_INT); 
    $st->execute(); 
    $row = $st->fetch(); 
    $conn = null; 
    if ($row) return new Article($row); 
    } 


    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 publicationDate FROM articles 
      ORDER BY " . mysql_escape_string($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; 
    } 

    // 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])); 
    } 


?> 
+0

您的LIMIT(在getList函数内)必须设置两个参数,如(http://www.mysqltutorial.org/mysql-limit.aspx)。然后,您可以使用$ numRows变量传递每个页面的行数。不要忘记创建链接“<< back”和“next >>”()通过$ _GET发送下一页 –

回答