如何对我的 CMS 进行分页 [英] How do I Paginate my CMS

查看:42
本文介绍了如何对我的 CMS 进行分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在关注 http://www.elated.com/articles/cms-in-an-afternoon-php-mysql/ .

CMS 运行良好,但唯一缺乏的是分页.文章存档显示数据库中所有文章的列表,但我希望能够将这些文章分成几页.我已经尝试了几次,但似乎永远无法让它发挥作用.点击下一页链接通常会将我带回主页.

感谢您的帮助

代码:

<块引用>

config.php

getMessage() );}set_exception_handler('handleException');?>

存档.php

<h1>文章存档</h1><ul id="头条新闻";类=存档"><?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><p class="summary"><?php echo htmlspecialchars( $article->summary )?></p><?php } ?><p><?php echo $results['totalRows']?>文章<?php echo ( $results['totalRows'] != 1 ) ?'s' : '' ?></p><p><a href=./">返回主页</a></p><?php 包含templates/include/footer.php"?>

<块引用>

文章.php

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'];}公共函数 storeFormValues ( $params ) {//存储所有参数$this->__construct( $params );//解析并存储发布日期如果 ( isset($params['publicationDate']) ) {$publicationDate = expand ('-', $params['publicationDate']);如果(计数($publicationDate)== 3){列表 ( $y, $m, $d ) = $publicationDate;$this->publicationDate = mktime ( 0, 0, 0, $m, $d, $y );}}}公共静态函数 getById( $id ) {$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );$sql = "SELECT *, UNIX_TIMESTAMP(publicationDate) AS PublicationDate FROM 文章 WHERE id = :id";$st = $conn->prepare( $sql );$st->bindValue( ":id", $id, PDO::PARAM_INT );$st->execute();$row = $st->fetch();$conn = 空;if ( $row ) 返回新文章( $row );}公共静态函数 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 文章由订购".mysql_escape_string($order) ."限制:numRows";$st = $conn->prepare( $sql );$st->bindValue( ":numRows", $numRows, PDO::PARAM_INT );$st->execute();$list = 数组();while ( $row = $st-> fetch() ) {$article = 新文章( $row );$list[] = $article;}//现在获取符合条件的文章总数$sql = "SELECT FOUND_ROWS() AS totalRows";$totalRows = $conn->query($sql)->fetch();$conn = 空;return ( array ( "results" => $list, "totalRows" => $totalRows[0]));}?>

解决方案

你需要修改你的代码以$_GET页码并在查询中使用limit和offset

你可以看到这个例子

http://www.tutorialspoint.com/php/mysql_paging_php.htm

I've been following the Build a CMS in an Afternoon tutorial at http://www.elated.com/articles/cms-in-an-afternoon-php-mysql/ .

The CMS works great but the only thing it's lacking is pagination. The article archive displays the list of all of the articles that are in the database, but I want to be able to separate these into pages. I've attempted it a few times but can never seem to get it to work. Clicking on a next page link usually brings me back to the homepage.

I will apreciate your help

Code:

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

解决方案

You need to modify your code to $_GET the page number and use limit and offset in the query

you can see this example

http://www.tutorialspoint.com/php/mysql_paging_php.htm

这篇关于如何对我的 CMS 进行分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆