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.
// 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>';
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?
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.
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
Try changing
unset($a, $html); // reset article count and html text for each loop
to
// reset article count and html text for each loop
$html = '';
$a = 0;
how can i change date limit to article limit? i mean i want ot show only five article.
Quote from: Mrcare on March 21, 2009, 09:50:39 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__);
Quote from: Petee on March 21, 2009, 03:36:06 PM
Quote from: Mrcare on March 21, 2009, 09:50:39 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...?
Quote from: Mrcare on March 21, 2009, 04:19:18 PM
Very nice...
how about to remove month above the article...?
Remove this line:
echo '<dt>' . date("F", mktime(0, 0, 0, $x)) . ' ' . $i . ' - ' . $a . ' Article' . ($a == 1 ? '' : 's');
Quote from: Petee on March 21, 2009, 04:24:36 PM
Quote from: Mrcare on March 21, 2009, 04:19:18 PM
Very nice...
how about to remove month above the article...?
Remove this line:
echo '<dt>' . date("F", mktime(0, 0, 0, $x)) . ' ' . $i . ' - ' . $a . ' Article' . ($a == 1 ? '' : 's');
oh.. soo very nice.... tq soo much..
what must i put in
$css_class = ''; // CSS style to display output within, or not!
if i want to using css from custom theme....?
You put your custom class name in between the single quotes. It will automatically use the custom css class.
how can change the list style not from css? i want to put arrow.
$css_class = 'myclass';
Then in your css file you add the classes to add the lists.
myclass ul{
custom attributes go here
}
myclass ul li{
custom css attributes go here.
}
Guess I missed the new questions when I last checked in; thanks JPDeni and IchBin for filling in the gap there... ::)
Quote from: IchBinâ„¢ on March 21, 2009, 05:41:43 PM
$css_class = 'myclass';
Then in your css file you add the classes to add the lists.
myclass ul{
custom attributes go here
}
myclass ul li{
custom css attributes go here.
}
TQ... working perfectly.
how about to change it to go random?
That would require some reworking of the existing code as it is. I see where that would be an interesting option though.
Could query for approved articles, picking one at random, then display the article's content from there. May be able to work on that in the next day or two...
ok... no problem to wait it... tq