TinyPortal

Development => Support => Topic started by: DonaldDasher on September 14, 2008, 01:12:57 PM

Title: Articles Block question
Post by: DonaldDasher on September 14, 2008, 01:12:57 PM
How do I display titles and links to recently created articles in the Articles block?
Title: Re: Articles Block question
Post by: JPDeni on September 14, 2008, 01:26:17 PM
You want a list of articles, sorted by date, with the most recent at the top? What exactly do you want to display? Just the title and link? Or do you want the date as well? Intro? Author? Number of views? Number of comments? Rating?
Title: Re: Articles Block question
Post by: DonaldDasher on September 14, 2008, 01:31:34 PM
Just the title and a link would be good. A date would be nice too I guess.

I've tried looking at the block's settings but I can't get it to display just what I need.
Title: Re: Articles Block question
Post by: JPDeni on September 14, 2008, 03:19:56 PM
In a php block:


global $db_prefix, $scripturl, $user_info;

$query = db_query(
    "SELECT subject, id, date
     FROM {$db_prefix}tp_articles
     WHERE approved = 1
     ORDER BY date DESC
     LIMIT 10", __FILE__, __LINE__);

while ($row = mysql_fetch_assoc($query))
{
  echo '<a href="' . $scripturl . '?page=' . $row['id'] . '">' . $row['subject'] . '</a> ' . timeformat($row['date'], $user_info['time_format']) . '<br />';
}
Title: Re: Articles Block question
Post by: DonaldDasher on September 14, 2008, 03:26:26 PM
Perfect - thanks!
Title: Re: Articles Block question
Post by: JPDeni on September 15, 2008, 10:53:33 PM
I saw that you wanted to modify this to use the short title.


global $db_prefix, $scripturl, $user_info;

$query = db_query(
    "SELECT subject, shortname, date
     FROM {$db_prefix}tp_articles
     WHERE approved = 1
     ORDER BY date DESC
     LIMIT 10", __FILE__, __LINE__);

while ($row = mysql_fetch_assoc($query))
{
  echo '<a href="' . $scripturl . '?page=' . $row['shortname'] . '">' . $row['subject'] . '</a> ' . timeformat($row['date'], $user_info['time_format']) . '<br />';
}


That should work.
Title: Re: Articles Block question
Post by: DonaldDasher on September 15, 2008, 10:54:48 PM
Yes, it works fabulously - thanks again ;)
Title: Re: Articles Block question
Post by: Ianedres on September 16, 2008, 01:27:02 AM
Actually, JP, that latest version won't link properly if the 'shortname' has no value. I added the 'if' clause to swap to the article's id number if 'shortname' was empty.

global $db_prefix, $scripturl, $user_info;

$query = db_query(
    "SELECT subject, id, date, shortname
     FROM {$db_prefix}tp_articles
     WHERE approved = 1
     ORDER BY date DESC
     LIMIT 10", __FILE__, __LINE__);

while ($row = mysql_fetch_assoc($query))
{
  if(empty($row['shortname'])) $row['shortname'] = $row['id'];

  echo '<a href="' . $scripturl . '?page=' . $row['shortname'] . '">' . $row['subject'] . '</a> ' . timeformat($row['date'], $user_info['time_format']) . '<br />';
}
Title: Re: Articles Block question
Post by: JPDeni on September 16, 2008, 02:14:54 AM
Good idea. Thanks!! :)