按ID订购新闻结果

时间:2013-11-16 12:13:17

标签: php mysql

我已经剪切了一个新闻脚本(cutephp),它创建了包含5行,ID,出版物日期,摘要,标题和内容的新闻条目。

日期目前以日 - 月 - 年格式存储 - 这对我需要的内容很好,但就目前情况而言 - 如果我在同一天创建多个帖子 - 订单会搞砸。

使用以下代码创建帖子:

editarticle.php上的表格:

<ul>

      <li>
        <label for="title">Article Title</label>
        <input type="text" name="title" id="title" placeholder="Name of the article" required     autofocus maxlength="255" value="<?php echo htmlspecialchars( $results['article']->title )?>" />
      </li>

      <li>
        <label for="summary">Article Summary</label>
        <textarea name="summary" id="summary" placeholder="Brief description of the article" required maxlength="1000" style="height: 5em;"><?php echo htmlspecialchars( $results['article']->summary )?></textarea>
      </li>

      <li>
        <label for="content">Article Content</label>
        <textarea name="content" id="content" placeholder="The HTML content of the article" required maxlength="100000" style="height: 30em;"><?php echo htmlspecialchars( $results['article']->content )?></textarea>
      </li>

      <li>
        <input type="hidden" name="publicationDate" id="publicationDate" required maxlength="10" value="<?php echo $publicationDate; ?>" />
      </li>


    </ul>

发给admin.php的帖子:

function newArticle() {

$results = array();
$results['pageTitle'] = "New Article";
$results['formAction'] = "newArticle";

if ( isset( $_POST['saveChanges'] ) ) {

// User has posted the article edit form: save the new article
$article = new Article;
$article->storeFormValues( $_POST );
$article->insert();
header( "Location: admin.php?status=changesSaved" );

} elseif ( isset( $_POST['cancel'] ) ) {

// User has cancelled their edits: return to the article list
header( "Location: admin.php" );
} else {

// User has not posted the article edit form yet: display the form
$results['article'] = new Article;
require( TEMPLATE_PATH . "/admin/editArticle.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 . "/hptest.php" );
}

并使用:

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

<div class="four columns services first-column mobile-two">
                   <h3 class="services-title" style="text-align:left"><a href="news.php?action=viewArticle&articleId=<?php echo $article->id?>"><?php echo htmlspecialchars( $article->title )?></a></h3>
                    <p align="left"><?php echo htmlspecialchars( $article->summary )?></p>
<p align="left">Published By <i><a href="index.php#our-team">Jack</a></i> on <i><?php echo date('j F', $article->publicationDate)?></i></p>
                </div>


<?php } ?>

我如何按ID递增排序结果?目前,订购有点混乱,如下所示:http://dev.pixxl.us/bwc/news.php?action=archive 虽然那里没有时间戳 - 我可以告诉你,它们每天都是最新的。

提前致谢。

已编辑,获取列表:

 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;
 }

1 个答案:

答案 0 :(得分:1)

似乎你只需要像这样改变对getList的调用:

$data = Article::getList( HOMEPAGE_NUM_ARTICLES, "publicationDate DESC, ID DESC" );

我怀疑ID是您的mysql数据库中ID字段的正确名称。

顺便说一句:奇怪的是看到一个PDO实现与mysql_escape_string结合使用