Select Only the Newest Article From a Section

The code below shows how to use OnpubAPI to get the newest article from a specific Onpub section without having to write any SQL queries of your own.

<?php

// Establish the connection to the Onpub database.
$pdo = new PDO"mysql:host=localhost;dbname=test""test""test" );

// Include the OnpubAPI classes.
include 'onpub/api/onpubapi.php';

// Construct an OnpubArticles object. This represents the OnpubArticles table
// in the database.
$oarticles = new OnpubArticles($pdo);

// Create a query options object. We will use this to order the articles to
// be selected below by date (newest to oldest).
$qo = new OnpubQueryOptions();
$qo->orderBy 'created';
$qo->order 'DESC';
// Limit the result to only 1 row from the database.
$qo->rowLimit 1;

// Now let's select the latest article from section ID 2. Change the second
// argument (2) to the section ID number you want to select the article from.
$articles $oarticles->select($qo2);

// Since the above method returned an array with one result, let's copy that
// result to a new variable.
$article $articles[0];

// Now let's print out some of the article object's fields.
en('<b>ID</b>: ' $article->ID12);
en('<b>Title</b>: ' $article->title12);
en('<b>Content</b>:<br>' $article->content11);
en('<b>Created</b>: ' $article->getCreated()->format('M j, Y, g:i a'));

?>

See Also

Comments

Feel free to comment on this article below. Any abusive/offensive material will be removed without notice.

blog comments powered by Disqus