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