dtwr2012 2013-11-16 12:13
浏览 15
已采纳

按ID订购新闻结果

I've got a cut up a news script (cutephp) that creates news entries with 5 rows, ID, publications date, summary, Title and content.

The date is currently stored in a day-month-year format - which is fine for what I need, but as it stands - if I create multiple posts on the same day - the order gets messed up.

Posts are created using the following code:

The form on 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>

Which posts to 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" );
}

}

Results are called using:

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

and displayed using:

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

How would I go about ordering the results by ID in ascending? at the moment the ordering is kind of messed up, as can be seen here: http://dev.pixxl.us/bwc/news.php?action=archive Although there are no timestamps there - i can tell you that they are oldest-newest each day.

Thanks in advance.

Edited, getlist:

 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条回答 默认 最新

  • douwei3280 2013-11-16 12:51
    关注

    it seems that you need simply to change the call of getList like this:

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

    i suspect that ID is the correct name for your ID-Field in your mysql database.

    By the way: strange to see a PDO implementation in combination with mysql_escape_string

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧