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: 453
  • Total: 453

[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.

dimdom

Hello all,

I use this code:

// 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 = '3F3F3F'; // background color for first row; 999999 = light gray
$css_row_alt = '525252'; // alternating row color; 666666 = dark gray

$link_width = 100; // 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 = 50; // number of results to display per page

$cats_allowed = array(0, 1, 50, 60, 166, 167); // 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);

// 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' => 'Όλα Ï,,α άρθρα'); // 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 = 'ΌλεÏ, οι καÏ,,ηγορίεÏ,';

$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> ανά σελίδα ';

echo '<input type="submit" name="Υποβολή" value="Υποβολή">';
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 . ' Άρθρα' . ($total_rows > 1 ? '' : '') . ' διαθέσιμα | ' . $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 . '">Προηγούμενη σελίδα</a> | ';

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

if($next_pg < $total_rows) echo ' | <a href="' . $scripturl . '?' . $url_link . 'page=' . $page . '&start=' . $next_pg . '&cat=' . $category . '">Επόμενη

σελίδα</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; Ημερομηνία :</td>';
if($show_views) echo '<td align="center">&nbsp; ΘεάσειÏ, &nbsp;</td>';
if($show_author) echo '<td align="left">&nbsp; ΣÏ...γγραφεύÏ, &nbsp;</td>';

echo '<td align="left">&nbsp;ΤίÏ,,λοÏ, άρθροÏ... </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">Î"εν Ï...πάρχοÏ...ν διαθέσιμα άρθρα.</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 . '">Προηγούμενη σελίδα</a> | ';

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

if($next_pg < $total_rows) echo ' | <a href="' . $scripturl . '?' . $url_link . 'page=' . $page . '&start=' . $next_pg . '&cat=' . $category . '">Επόμενη σελίδα</a>';

echo ' | ΣÏ...νολικόÏ, αριθμόÏ, άρθρων : ' . $total_rows;

echo '</div>';

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


But in error log I keep getting errors concerning "Undefined variable: row_class"

For example...



http://www.mysite.com/forum/index.php?limit=50&amp;page=138&amp;start=50&amp;cat=0
8: Undefined variable: row_class
Ἀρχεῖο: /home/mysite/public_html/forum/Themes/default/TPortal.template.php (eval?)
Î"ραμμή: 274


Any help with that errors?

Ianedres

Dimdom,

To correct the undefined error, add this one line to your copy of the script, using two single quotes at the end before the semicolon:$row_class = '';

You can place this under the $limits = array(10, 20, 30, 40, 50); line.

The error-checking is very verbose in SMF; an undefined error is not a serious error, but obviously something that needed to be corrected.

Thanks for bringing this to my attention!

dimdom

That fixed it.


QuoteThanks for bringing this to my attention!

Thank you very much for the quick reply.






Ianedres

I will correct this in this topic to avoid future issues...  thanks for confirming that it corrected your error log.

Bazil Greyson

This is almost everything I have ever wished for! However, is there any way to add an option to sort the list based upon article author instead of by category? In an ideal world, both would be options.

Thanks
-baz

www.suburbanconspiracy.com

ed_m2


Ianedres

Quote from: Bazil Greyson on November 14, 2008, 03:02:37 PMThis is almost everything I have ever wished for! However, is there any way to add an option to sort the list based upon article author instead of by category? In an ideal world, both would be options.

This probably could be done with minor modifications to the existing code by placing all the authors into an array, and selecting from there...  might find some time to play with it this weekend and can try to implement this.

diegolyanky

Why when I try to generate this php article, i give this 404 error ?

Forbidden
You don't have permission to access /index.php on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.


What are I doing wrong ?  :'(


diegolyanky

It worked ...

And, my gift for you is the " SPANISH TRANSLATION " of this snippet.

Thanks a lot !!

This website is proudly hosted on Crocweb Cloud Website Hosting.