News: April 7th, 2019, The Tinyportal team is pleased to announce the release of ~~ TinyPortal 1.6.3! GET IT NOW!!!

Login  |  Register
HTML5 Icon HTML5 Icon
TP on Social Media
Welcome, Guest. Please login or register.
Did you miss your activation email?

September 15, 2019, 01:30:27 AM

Login with username, password and session length

Recent

Members
  • Total Members: 3752
  • Latest: Alizee
Stats
  • Total Posts: 188879
  • Total Topics: 20751
  • Online Today: 38
  • Online Ever: 629
  • (November 08, 2018, 01:36:54 PM)
Users Online
Users: 0
Guests: 38
Total: 38

Author Topic: [Block] Article Archive List (With Date Range)  (Read 9406 times)

0 Members and 1 Guest are viewing this topic.

Offline Ianedres

  • Friends
  • *
  • Posts: 684
  • $foo['ianedres'] < $foo['ichbin']
    • Clickity click dot dot dot com thingy
[Block] Article Archive List (With Date Range)
« on: October 28, 2008, 03:30:01 AM »
This php code snippet will retrieve a range of articles within one category, displayed in the order of the most current article's date until your specified ending date.

Place the code within a php article, set your variables described below, and display the article.

Four variables to set for customization:
$category - number of the article category; zero = all articles without a category
$cat_title - name for header; displayed above list
$date_limit - oldest date to search to; must be a proper date value (mm/dd/yyyy)
$css_class - optional; use CSS class from theme's style.css file to set output to style

Minor error-checking performed, as it has no external input values; generic error message will display if database query fails for any reason or returns no results based on your specified category or date range.

Tested under TinyPortal v0.98
This block will always contain the most recent version of this snippet.
Code: [Select]
// Article Archive : Version 1.0
// Oct 27 2008 | Ianedres | www.BayouMX.com

// Retrieve range of articles (newest first), list by year and month, provide direct link to article

global $db_prefix, $scripturl;

// set these variables here;
$category = 0; // numeric value for what category to display
$cat_title = 'Article Archive'; // header title text
$date_limit = '1/1/1970'; // set date to limit range of articles; mm/dd/yyyy or 1/1/1970
$css_class = ''; // CSS style to display output within, or not!

// script variables; warranty void if edited below this line!
$query = '';
$articles = array();
$row = '';
$cats = array();
$html = '';
if(empty($date_limit)) $date_limit = '1/1/1970'; // default to oldest date possible if left blank
$date_limit = date("U", strtotime($date_limit)); // convert date above to unix timestamp (don't edit this line)

// set database query; date descending (newest first), with approved articles within specified category
$query = db_query(
"SELECT id, date, subject, shortname, author, views, category
FROM {$db_prefix}tp_articles
WHERE category = {$category} AND approved = 1 AND date > {$date_limit}
ORDER BY date DESC
", __FILE__, __LINE__);

// get number of articles returned
$num_articles = mysql_affected_rows();

// check for valid result; display error message if so
if($num_articles < 1)
{
echo '<hr /><div class="error">Unable To Locate Articles Using Specified Parameters!</div><hr />';
return;
}

// display header
echo '<div class="titlebg">' . $cat_title . ' (' . $num_articles . ' Article' . ($num_articles == 1 ? '' : 's') . ' Total)' . '</div>';

// set array of articles
while($row = mysql_fetch_assoc($query))
{
if(empty($row['shortname'])) $row['shortname'] = $row['id']; // set shortname to id if empty

$articles[] = array(
'id' => $row['id'],
'date' => $row['date'],
'subject' => $row['subject'],
'shortname' => $row['shortname'],
'author' => $row['author'],
'views' => $row['views'],
'category' => $row['category'],
'year' => date("Y", $row['date']),
'month' => date("m", $row['date']),
);
}

// used to set loop variables
$max = $articles[0]['date'];
$min = end($articles);
$min = $min['date'];
$start_date = getdate($max);
$end_date = getdate($min);
$start_month = $start_date['mon'];
$end_month = $end_date['mon'];

// start output loop
echo '<div class="' . $css_class . '">';
echo '<ul>';

// start at most recent year in article array
for($i = $start_date['year']; $i >= $end_date['year']; $i = $i - 1)
{
// set ending month for this loop; avoid listing months past oldest article
if($i > $end_date['year'])
{
$end_month = 1; 
}
else
{
$end_month = $end_date['mon'];
}

// month loop; start at most recent month in article array; avoid listing months ahead of articles
for($x = $start_month; $x >= $end_month; $x = $x - 1)
{
{
unset($a, $html); // reset article count and html text for each loop

foreach($articles as $item)
{
if($item['year'] == $i && $item['month'] == $x) // i.e. article occurs within the current loop
{
$html .= '<li>';
$html .= date("m/d", $item['date']) . ' : ';
$html .= '<a href="' . $scripturl . '?page=' . $item['shortname'] . '" ';
$html .= 'title=" Displayed ' . $item['views'] . ' Time' . ($item['views'] == 1 ? '' : 's') . ' - Click To View Now "';
$html .= ' >';
$html .= $item['subject'] . '</a>' . '</li>';
$a++; // used to count articles for current month
}
}

// display output html only if not empty; cheesy way to parse array output, but it works!
if(!empty($html))
{
echo '<dt>' . date("F", mktime(0, 0, 0, $x)) . ' ' . $i . ' - ' . $a . ' Article' . ($a == 1 ? '' : 's');
echo '<ul>' . $html . '</ul>';
echo '</dt>';
}
}
}

$start_month = 12; // at end of first loop, set back to 12 to start counting downward again
}

// end of list and div
echo '</ul></div>';

MiLoKa

  • Guest
Re: [Block] Article Archive List (With Date Range)
« Reply #1 on: December 16, 2008, 06:39:13 PM »
I get always the following output:

Unable To Locate Articles Using Specified Parameters!

I checked the category number its ok but why do I get always the same output?

Offline Ianedres

  • Friends
  • *
  • Posts: 684
  • $foo['ianedres'] < $foo['ichbin']
    • Clickity click dot dot dot com thingy
Re: [Block] Article Archive List (With Date Range)
« Reply #2 on: December 16, 2008, 08:43:00 PM »
It is returning a result of zero from the database query.

The query looks for 'approved' articles only- so if your articles are 'enabled' for viewing, they won't appear.

Also, check your date range; it defaults to the earliest possible (1/1/1970) but it you altered that variable to an invalid format it may produce an error.

mikemikemike

  • Guest
Re: [Block] Article Archive List (With Date Range)
« Reply #3 on: February 23, 2009, 06:26:32 AM »
Hello Ianedres,

Great code  :up:

Only it give an error (on my error log)

8: Undefined variable: html
Bestand: /xxxxx/TPortal.template.php (main sub template - eval?)
Regel: 103

8: Undefined variable: a
Bestand: /xxxxx/TPortal.template.php (main sub template - eval?)
Regel: 109

maybe you can tell me how i can fix it...

thanx

JPDeni

  • Guest
Re: [Block] Article Archive List (With Date Range)
« Reply #4 on: February 23, 2009, 07:26:18 AM »
Try changing

Code: [Select]
unset($a, $html); // reset article count and html text for each loop

to

Code: [Select]
// reset article count and html text for each loop
$html = '';
$a = 0;

alhaudhie

  • Guest
Re: [Block] Article Archive List (With Date Range)
« Reply #5 on: March 21, 2009, 03:50:39 AM »
how can i change date limit to article limit? i mean i want ot show only five article.

Petee

  • Guest
Re: [Block] Article Archive List (With Date Range)
« Reply #6 on: March 21, 2009, 09:36:06 AM »
how can i change date limit to article limit? i mean i want ot show only five article.

$query      = db_query(
   "SELECT id, date, subject, shortname, author, views, category
    FROM {$db_prefix}tp_articles
    WHERE category = {$category} AND approved = 1 AND date > {$date_limit}
    ORDER BY date DESC LIMIT 5
    ", __FILE__, __LINE__);

alhaudhie

  • Guest
Re: [Block] Article Archive List (With Date Range)
« Reply #7 on: March 21, 2009, 10:19:18 AM »
how can i change date limit to article limit? i mean i want ot show only five article.

$query      = db_query(
   "SELECT id, date, subject, shortname, author, views, category
    FROM {$db_prefix}tp_articles
    WHERE category = {$category} AND approved = 1 AND date > {$date_limit}
    ORDER BY date DESC LIMIT 5
    ", __FILE__, __LINE__);

Very nice...
how about to remove month above the article...?

Petee

  • Guest
Re: [Block] Article Archive List (With Date Range)
« Reply #8 on: March 21, 2009, 10:24:36 AM »
Very nice...
how about to remove month above the article...?

Remove this line:

Code: [Select]
echo '<dt>' . date("F", mktime(0, 0, 0, $x)) . ' ' . $i . ' - ' . $a . ' Article' . ($a == 1 ? '' : 's');

alhaudhie

  • Guest
Re: [Block] Article Archive List (With Date Range)
« Reply #9 on: March 21, 2009, 10:31:16 AM »
Very nice...
how about to remove month above the article...?

Remove this line:

Code: [Select]
echo '<dt>' . date("F", mktime(0, 0, 0, $x)) . ' ' . $i . ' - ' . $a . ' Article' . ($a == 1 ? '' : 's');
oh.. soo very nice.... tq soo much..