TP-Docs
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media

Recent

Welcome to TinyPortal. Please login or sign up.

March 29, 2024, 08:11:23 AM

Login with username, password and session length
Members
Stats
  • Total Posts: 195,106
  • Total Topics: 21,213
  • Online today: 358
  • Online ever: 3,540 (September 03, 2022, 01:38:54 AM)
Users Online
  • Users: 0
  • Guests: 88
  • Total: 88

[Block] Articles - Sorted, Paginated, Categorized, & Customized

Started by IchBin, September 08, 2010, 10:12:15 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

IchBin

Name of Snippet: Articles - Sorted, Paginated, Categorized, & Customized
SMF/TP versions tested:: SMF2/TP1 (Should work in SMF1.x but not tested)
Block Type:: php
Author: tim antley
Link to Discussion: Click Here
Other Requirements:
Description: This is a php article script; it will display a listing of approved articles by category, sorted by date (newest first), with paginated results.

Drop-down lists for category and number of results per page can be selected.

Total number of articles for the selected category is displayed, as well as previous/next links if applicable.

Variables in the script code allow for customization of the table display for the article's date, views, and author. Also, you can select to use the theme's CSS or override with your color preferences in the table rows.

// Articles By Date
// September 24, 2008 | Tim Antley
// Version II (Already)

// Displays x Number Of Approved Articles Per Page / Sorted By Date (Latest To Oldest)
// Simple Pagination In Table Output Form
// Retrieve URL Variables To Parse

global $db_prefix, $scripturl, $context;

// script variables for customization
$show_date = true; // show date in table; true or false
$show_views = true; // show number of views for each article
$show_author = false; // show name of author in table; true or false
$show_header = true; // show table header w/ field values
$use_theme_css = false; // use theme CSS or set using colors below

// will use numeric values if $use_theme_css set to false
$css_row = '999999'; // background color for first row; 999999 = light gray
$css_row_alt = '666666'; // alternating row color; 666666 = dark gray

$link_width = 60; // max length of article name in table
$link_target = '_blank'; // target for article link; _self = same window, _blank = new window
$link_color = 'FF9900'; // hyperlink color; actually gets set in custom CSS block below

$limit = 20; // number of results to display per page
$category = 0; // number of category, 0 = no specified category

$page = ''; // set to article # that this script is in

// no need to edit past this point! warranty void if modified...

$article = array();
$limits = array(10, 20, 30, 40, 50);
$row_class = '';

// put URL variables into array
parse_str($_SERVER['QUERY_STRING'], $url_vars);

$start = 0; // set to 0; will check for URL value soon

// check for 'cat' and 'limit' values via GET first; use POST value instead, if set
if(!empty($_GET['cat'])) $category = $_GET['cat'];
if(isset($_POST['cat'])) $category = $_POST['cat'];

if(isset($_GET['limit'])) $limit = $_GET['limit'];
if(isset($_POST['limit'])) $limit = $_POST['limit'];

if($limit < 0 || !in_array($limit, $limits)) $limit = 10;

$url_link = ''; // set to null

// build url string from array (exclude script variables)
foreach($url_vars as $key => $value)
{
$key = strtolower($key); // restrict to lowercase

if($key != 'page' && $key != 'start' && $key != 'cat' && $key != 'limit')
{
$url_link .= $key . '=' . $value . '&';
}
}

$url_link .= 'limit=' . $limit . '&';

// custom CSS block to set style
if(!$use_theme_css)
{
echo '
<style type="text/css">
.article_nav a:link, a:hover
{
text-decoration: underline;
color: #' . $link_color . ';
}
</style>';

$css = 'style=background: #';
}
else
{
$css = 'class="windowbg';
$css_row = '';
$css_row_alt = '2';
}

// crude way to get page name; $context SHOULD have this somewhere...
if(!empty($_GET['page'])) $page = $_GET['page'];

// get starting point from URL and set to integer value; already defaulted to 0 if not set
if(!empty($_GET['start'])) $start = intval($_GET['start']);

$query = tpdb_query(
"SELECT id, value1, type
FROM {$db_prefix}tp_variables
WHERE type = 'category'
", __FILE__, __LINE__);

$cats[0] = array('id' => 0, 'cat_name' => 'No Specific Category'); // pre-populate generic category

while ($row = tpdb_fetch_assoc($query)) // populate category array
{
$cats[] = array(
'id' => $row['id'],
'cat_name' => $row['value1'],
);
}

if($category != 0)
{
$query = tpdb_query(
    "SELECT *
FROM {$db_prefix}tp_articles
WHERE approved = 1
AND category = {$category}
", __FILE__, __LINE__);
}
else
{
$query = tpdb_query(
    "SELECT *
FROM {$db_prefix}tp_articles
WHERE approved = 1
", __FILE__, __LINE__);
}
$total_rows = tpdb_num_rows($query); // get number of articles approved

if($category != 0)
{
$query = tpdb_query(
    "SELECT id, value1
FROM {$db_prefix}tp_variables
WHERE id = {$category}
", __FILE__, __LINE__);

$cat_desc = mysql_fetch_array($query);
$cat_desc = $cat_desc['value1'];

if(empty($cat_desc)) $cat_desc = 'No Category Description In Database';

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

}
else
{
$cat_desc = 'No Specified Category';

$query = tpdb_query(
    "SELECT id, date, subject, shortname, author, views, category
     FROM {$db_prefix}tp_articles
     WHERE approved = 1
ORDER BY date DESC
LIMIT {$start}, {$limit}
", __FILE__, __LINE__);
}

while ($row = tpdb_fetch_assoc($query)) // place article info into $article array
{
$article[] = array(
'id' => $row['id'],
'date' => $row['date'],
'subject' => $row['subject'],
'shortname' => $row['shortname'],
'author' => $row['author'],
'views' => $row['views'],
'category' => $row['category'],
);
}

// display category select menu form w/ submit button
echo '<form name="article_menu" method="POST" action="' . $scripturl . '?' . $url_link . 'page=' . $page . '">';
echo '<select name="cat">';

foreach($cats as $item)
{
echo '<option ' . ($item['id'] == $category ? 'selected ' : '') . 'value="' . $item['id'] . '">' . $item['cat_name'] . '</option>';
}
echo '</select>  ';

echo '<select name="limit">';
foreach($limits as $limit_sel)
{
echo '<option ' . ($limit == $limit_sel ? 'selected ' : '') . 'value="' . $limit_sel . '">' . $limit_sel . '</option>';
}
echo '</select> Per Page ';

echo '<input type="submit" name="Submit" value="Submit">';
echo '</form>';

// display article table with info
if(count($article) > 0)
{

// simple pagination routine; took entirely too long to get this right...
if($start + $limit <= $total_rows)
{
$next_pg = $start + $limit;
}
else
{
$next_pg = $total_rows;
}

if($start - $limit >= 0)
{
$prev_pg = $start - $limit;
}
else
{
$prev_pg = 0;
}

// start of top navigation block
echo '<hr /><div class="titlebg">' . $total_rows . ' Article' . ($total_rows > 1 ? 's' : '') . ' Available | ' . $cat_desc . '</div>';

echo '<div class="titlebg" class="article_nav">';

if($start > 1) echo '<a href="' . $scripturl . '?' . $url_link . 'page=' . $page . '&start=' . $prev_pg . '&cat=' . $category . '">Previous Page</a> | ';

echo $start + 1 . ' - ' . $next_pg;

if($next_pg < $total_rows) echo ' | <a href="' . $scripturl . '?' . $url_link . 'page=' . $page . '&start=' . $next_pg . '&cat=' . $category . '">Next Page</a>';

echo '</div><hr />';

echo '<table border="0" cellspacing="0" cellpadding="0">';

if($show_header) // display a table header
{
echo '<tr class="windowbg2">';

if($show_date) echo '<td align="right">  Date :</td>';
if($show_views) echo '<td align="center">  Views  </td>';
if($show_author) echo '<td align="left">  Author  </td>';

echo '<td align="left"> Article Title </td>';
}

foreach($article as $item)
{
if(empty($item['shortname'])) $item['shortname'] = $item['id']; // make shortname = id if it doesn't already exist (for JPDeni!)

if($use_theme_css)
{
echo '<tr class="windowbg' . ($row_class = $row_class == $css_row ? $css_row_alt : $css_row) . '">';
}
else
{
echo '<tr style="background: #' . ($row_class = $row_class == $css_row ? $css_row_alt : $css_row) . '">';
}

if($show_date)
{
echo '<td align="right">  ' . date("M j, Y", $item['date']) . ' :</td>';
}

if($show_views) echo '<td align="center"> (' . $item['views'] . ')' . '</td>';
if($show_author) echo '<td align="left"> (' . $item['author'] . ')</td>';

echo '<td align="left"> <a href="' . $scripturl . '?page=' . $item['shortname'] . '" target="' . $link_target . '">';
echo substr($item['subject'], 0, $link_width) . '</a>';

echo ' </td></tr>';
}

echo '</table>';
}
else // no articles = simple notice of nothing found; no table used
{
echo '<div class="titlebg">No Available Articles At This Time</div><hr />';
}

// start of navigation block
echo '<hr /><div class="titlebg" class="article_nav">';

if($start > 1) echo '<a href="' . $scripturl . '?' . $url_link . 'page=' . $page . '&start=' . $prev_pg . '&cat=' . $category . '">Previous Page</a> | ';

echo $start + 1 . ' - ' . $next_pg;

if($next_pg < $total_rows) echo ' | <a href="' . $scripturl . '?' . $url_link . 'page=' . $page . '&start=' . $next_pg . '&cat=' . $category . '">Next Page</a>';

echo ' | Total Articles : ' . $total_rows;

echo '</div>';

tpdb_free_result($query); // free up memory (nice to do)

lurkalot

Note: This block snippet no longer works, I believe it needs updating to use $smcFunc to get it working.  If anyone could update this for us it would be appreciated.