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

Recent

Welcome to TinyPortal. Please login or sign up.

Members
  • Total Members: 3,963
  • Latest: BiZaJe
Stats
  • Total Posts: 195,913
  • Total Topics: 21,308
  • Online today: 884
  • Online ever: 8,223 (February 19, 2025, 04:35:35 AM)
Users Online
  • Users: 0
  • Guests: 553
  • Total: 553

[block] Approved Articles / Sorted, Paginated, Categorized, & Customized

Started by tim antley, September 21, 2008, 11:13:59 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Ianedres

This is version II.5, which sets an array of allowed categories for the drop down box.

If you set $cats_allowed to '0' only, all categories will be listed in the drop-down menu. Otherwise, specifying a list of valid categories (separate each number with a comma; i.e. 1, 2, 5, 10) will select only those.

Of special interest here is that the first category in the array is the default category to start with, so 10, 5, 99, 3 will list category #10 on the first viewing, but allow categories 5, 99, and 3 to also be selected. (The numbers don't have to be in numerical order.)

If you place 0 anywhere in the array, it will show all categories in the drop-down.

There is no error-checking for valid categories in the array... hopefully, an admin can select the proper categories!

Edit: Defined $row_class variable in this code to avoid throwing undefined error.

// Articles By Date
// September 25, 2008 | Tim Antley
// Version II.5 (The Ken Edition)

// 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
// Restrictive Category Selection

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

$cats_allowed = array(0); // valid category numbers; '0' = all categories; separate values with commas

$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(isset($_GET['cat'])) $category = $_GET['cat'];
if(isset($_POST['cat'])) $category = $_POST['cat'];

if(!isset($category)) $category = $cats_allowed[0]; // default to first value if not set through POST or URL
if(!in_array($category, $cats_allowed)) $category = $cats_allowed[0]; // validate category against allowed categories

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 = db_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 = mysql_fetch_assoc($query)) // populate category array
{
$cats[] = array(
'id' => $row['id'],
'cat_name' => $row['value1'],
);
}

// create drop-down list by parsing against allowed categories
if(count($cats_allowed) > 1 || !in_array(0, $cats_allowed))
{
foreach($cats as $key => $value)
{
if(!in_array($value['id'], $cats_allowed))
{
unset($cats[$key]);
}
}
}

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

if($category != 0)
{
$query = db_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 = db_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 = db_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 = mysql_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> &nbsp;';

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">&nbsp; Date :</td>';
if($show_views) echo '<td align="center">&nbsp; Views &nbsp;</td>';
if($show_author) echo '<td align="left">&nbsp; Author &nbsp;</td>';

echo '<td align="left">&nbsp;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"> &nbsp;' . date("M j, Y", $item['date']) . ' :</td>';
}

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

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

echo '&nbsp;</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>';

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

Rafferty

lol will not display single categories now. All categories are showing in the drop down list however when selected ALL articles are displaying.

Ianedres

Did you place a single category number in the array $cats_allowed (under the $limit variable) ??

$cats_allowed   = array(x);

Using your valid categories for x, of course...

Ken.

" If everything seems under control, you're not going fast enough." - Mario Andretti
Yesterday When I was Young.

Ianedres


Ken.

Yepp, trying to do so because in my experience if it's not working correctly then there is a really good chance a setting has been made incorrectly or overlooked... and it's embarrassing to me when someone has to point out something that me should have been able to see for myself.  :o
" If everything seems under control, you're not going fast enough." - Mario Andretti
Yesterday When I was Young.

Rafferty

I tried several different variations but same display, all articles. No worries tho i went back to the previous version you gave me which works fine

Ken.

Look for this line of code Rafferty:

$cats_allowed = array(0); // valid category numbers; '0' = all categories; separate values with commas


And set it to your categories numbers like so:

$cats_allowed = array(9, 20, 23); // valid category numbers; '0' = all categories; separate values with commas

... with "9, 20, 23" being whatever categories you wish to have display.
" If everything seems under control, you're not going fast enough." - Mario Andretti
Yesterday When I was Young.

Rafferty


Ianedres

I'll be more than happy to take a look at it on your site, if I can have access to edit the block. Should be something minor...

This website is proudly hosted on Crocweb Cloud Website Hosting.