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

Recent

Welcome to TinyPortal. Please login or sign up.

May 19, 2024, 03:27:40 AM

Login with username, password and session length
Members
  • Total Members: 3,886
  • Latest: Grendor
Stats
  • Total Posts: 195,189
  • Total Topics: 21,220
  • Online today: 100
  • Online ever: 3,540 (September 03, 2022, 01:38:54 AM)
Users Online
  • Users: 0
  • Guests: 97
  • Total: 97

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

Modified Sep 28:
> Defined $row_class to avoid 'undefined' error in log.
Modified Sep 24:
> Variables in the URL will be passed along (such as 'board=xx' and so forth...) to accommodate displaying within certain areas of your site.
> Fixed 'limit' issue for results display.

// 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 = 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'],
);
}

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

Script works fine on initial page but when I select next page, change number of articles on one page or select a category, it defaults to the first article each time.

If I select an individual article from the list it displays fine ??

Ianedres

Thanks for trying it out.

The script functions as designed on my 0.98 TinyPortal installation with over 240+ articles in various categories. I tested it extensively before publishing the script here, so I feel confident that it should work without modifications, but it is always possible that it won't work for everyone.

I will need a link to your site's article to check and determine why it is not working for you.

If you select a category (and the current category should appear by default in the drop-down) and use SUBMIT, it will start at the beginning on that category. Using the NEXT or PREV links should remain within the selected category.

Rafferty

Yes thats exactly what it is not doing. I also use 0.98 on SMF 1.1.6

when I press submit or the next arrows the first article displays every time

Snippett can be found at https://16airdefence.org/forum/index.php?board=75.0

I have given you a temp log on:

Name: removed
PW: removed

Please let me know when your done so I can delete the account

*removed name and password*

Ianedres

OK. I see the difficulty...

By placing the script into a block on the page, it does not detect the article number to pass along to the PREV/NEXT link.

My intent with the script was to view it as an article first, in which it detects the article # by way of the 'page=xx' in the URL.

Not to worry, I'll try to insert a quick fix for you. Be back in a little while... (nothing else to do tonight!)

Also, I removed the name and password you provided, and also changed the password for the account on your site to be safe.

Rafferty

Thank you, it's not in an article its in a TP centre block ...

works just fine as an article ... is this the problem?

Ianedres

It appearing in a block is the issue, as on your site it apparently is set to appear only within board #75.

I manually appended '&board=75' to the url (.../forum/index.php?page=336&start=20&cat=0&board=75) and it displayed the results starting at 20...

I will send you a quick-fix via PM to avoid clouding up the forum here...


Ianedres

I see that I will need to retrieve the entire URL and parse the variables as such to avoid this in the future.

I will leave the script as is, for now, and work on this ASAP.

Ianedres

Code in original post has been updated to fix the URL variables passing through for restricted display permissions, as well as the 'limit' issue being fixed to work correctly.  8)