TinyPortal

Development => Block Codes => Topic started by: Ianedres on September 21, 2008, 11:13:59 AM

Title: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Ianedres on September 21, 2008, 11:13:59 AM
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)
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Rafferty on September 24, 2008, 03:02:09 AM
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 ??
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Ianedres on September 24, 2008, 04:54:58 AM
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.
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Rafferty on September 24, 2008, 05:32:15 AM
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*
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Ianedres on September 24, 2008, 05:50:41 AM
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.
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Rafferty on September 24, 2008, 07:42:11 AM
Thank you, it's not in an article its in a TP centre block ...

works just fine as an article ... is this the problem?
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Ianedres on September 24, 2008, 08:12:16 AM
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...
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Rafferty on September 24, 2008, 08:17:39 AM
your efforts are appreciated
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Ianedres on September 24, 2008, 08:22:56 AM
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.
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Ianedres on September 24, 2008, 09:51:49 AM
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)
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Rafferty on September 24, 2008, 11:30:51 AM
New version updated and is working fine, beautiful job
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Ken. on September 24, 2008, 03:12:39 PM
FYI... This code works as expected in TP v1.0.5 beta 1. :up:

I've found that the text colors and the line background colors make for easy reading in most themes but there are some themes like Azure, BlueSkys and BZ14 were it is very difficult to make out the text in the lines with dark bg colors. 
Setting the theme_css to 'true' resolves the issue of course, but to me the default (false) gives a more pleasing contrast in the majority of my themes... I'll just have to play with the setting and colors some more to see if there is a combination that looks better to me.

This is a very useful code snippet Ianedres, thank you for this effort.  ;D

Article List (http://www.ourfamilyforum.org/FamilyForum/index.php?page=138)

EDIT: In trying to set the articles category's that show in the article it's only letting me show one category or all category's, is there a way to show more than one but not all? Or is it just me missing a setting?
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: IchBin on September 24, 2008, 04:14:14 PM
Guests can't view that page ken. I'd like to see what this looks like if you wouldn't mind opening it up to guests. Or I can just register, but it looks like a family forum and I'm not family. :)
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Ken. on September 24, 2008, 04:22:33 PM
Sorry, forgot to move it out of my test category, you should be able to see it now.

As for the log-in, you are welcome to have one if you want to work with this a little, just let me know and I'll make one for you.
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: IchBin on September 24, 2008, 04:58:26 PM
Very cool, and nice work Ian.  Thanks for the look Ken. No login necessary though, thanks! BTW, how tall are you Ken? I clicked on one of the articles and saw what looked like you standing next to a couple of midgets. lol But seriously if that was you, you looked like you were about 7 feet tall compared to the others...
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Ken. on September 24, 2008, 05:27:02 PM
Which picture? ... whoever looks the biggest is most likely me.
My height is 6-4, but my weight probably makes me look a little taller/bigger. :) 
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Ianedres on September 24, 2008, 06:56:14 PM
Thanks for the good words Ken and Ich. This is one of those projects that started as one thing, and morphed into adding this or that as it came to me...

It is good to know that it works in TP 1.05 as well too, since my test site doesn't have very many articles. The option for custom or theme colors was made since I use Phobos (dark) but wanted to make it flexible for everyone.

Ken, as for the list of categories being restricted, I should be able to do that by providing a static list of the categories instead of querying for all the categories. Will try to work on that this evening after work.

Appreciate the comments!
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: IchBin on September 24, 2008, 08:03:47 PM
This image Ken. If you don't want this image here, go ahead and edit my post.
http://www.ourfamilyforum.org/cpg1410/albums/brian/brian-04/normal_3-ken.jpg

Sorry to go off topic Ian. ::)
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Ken. on September 24, 2008, 10:31:57 PM
Quote from: IchBinâ,,¢ on September 24, 2008, 08:03:47 PM
This image Ken. If you don't want this image here, go ahead and edit my post.
http://www.ourfamilyforum.org/cpg1410/albums/brian/brian-04/normal_3-ken.jpg

Sorry to go off topic Ian. ::)
Yepp... that are me. :)
And they are kind of short in comparison. (2 grandkids & their mom)

Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Ken. on September 24, 2008, 10:37:22 PM
Quote from: Ianedres on September 24, 2008, 06:56:14 PM
Thanks for the good words Ken and Ich. This is one of those projects that started as one thing, and morphed into adding this or that as it came to me...

It is good to know that it works in TP 1.05 as well too, since my test site doesn't have very many articles. The option for custom or theme colors was made since I use Phobos (dark) but wanted to make it flexible for everyone.

Ken, as for the list of categories being restricted, I should be able to do that by providing a static list of the categories instead of querying for all the categories. Will try to work on that this evening after work.

Appreciate the comments!
Thanks Tim.

I don't want to show all of my articles so having the ability to show 3 or 4 categories would be great.
The color thing is not a huge deal, but it is a problem with some themes so you may see that question again if very many folks use the code.
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Ianedres on September 25, 2008, 07:15:36 AM
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)
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Rafferty on September 25, 2008, 08:15:57 AM
lol will not display single categories now. All categories are showing in the drop down list however when selected ALL articles are displaying.
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Ianedres on September 25, 2008, 08:28:39 AM
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...
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Ken. on September 25, 2008, 09:43:44 AM
Very nice.  :up:
http://www.ourfamilyforum.org/FamilyForum/index.php?page=138



"// Version II.5 (The Ken Edition)"  :2funny:
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Ianedres on September 25, 2008, 09:56:55 AM
Checking my code closely now, eh?  :coolsmiley:
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Ken. on September 25, 2008, 10:11:52 AM
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
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Rafferty on September 25, 2008, 11:48:46 AM
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
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Ken. on September 25, 2008, 12:25:08 PM
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.
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Rafferty on September 25, 2008, 04:29:56 PM
yes I did do Ken it didnt seem to make any difference
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Ianedres on September 25, 2008, 07:48:04 PM
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...
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Rafferty on September 26, 2008, 12:48:03 AM
Its working tho Ian with that previous version:

// 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 = true; // 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 = '_self'; // 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 = '75'; // 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(!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>  ';

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>';

mysql_free_result($query); // free up memory (nice to do)
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Ianedres on September 26, 2008, 12:29:11 PM
I think the newest code would be better for your site, in the long run, so that you can specify exactly what gets listed, rather than everything...

Once we verify that it works correctly for you, I'll most likely mark this topic for reference only, and make a new topic for this newest code. Ken has reported it works for his site, and I've tested it on my personal site also... so I feel that it is most likely something small in the array list that is giving your site trouble.

Just to clarify- the array will list ALL categories if a zero is specified anywhere in the array...
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Rafferty on September 26, 2008, 12:34:03 PM
yep it lists all cats no matter what i put in the array
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Ken. on September 26, 2008, 12:36:24 PM
Rafferty, if you post the numbers for the categories that you wish to have show up I'll post an edited copy of the newest code to see if that will work the way you want it to.
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Rafferty on September 26, 2008, 01:37:47 PM
i want them all.
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Ken. on September 26, 2008, 01:53:39 PM
Quote from: Rafferty on September 26, 2008, 01:37:47 PM
i want them all.

Umm...
Then I guess that I must have missed something along the way because if thats the case then I don't quite know why we are having this discussion. ;)
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Rafferty on September 27, 2008, 02:59:35 AM
Ken the problem isn't the category displays, its the articles display. For instance if i select articles category "reunions" in which i have about 4 articles, it displays ALL articles and the same no matter what category i select.
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Ianedres on September 27, 2008, 10:28:26 AM
If you have a zero in the line:
$cats_allowed = array(x, y, z);

then it will select all categories. You can have multiple categories there, or just one to limit to that one category. But, if you have a zero anywhere in there, it will provide select from all categories.

Let us know what you have in that line in that 2.5 version of the script, not the original post.
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Rafferty on September 27, 2008, 10:38:27 AM
I dont have it anymore, I went back to the previous version but it was as default in the copied code
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Ianedres on September 27, 2008, 10:55:10 AM
Then that is where the issue is- it is set by default to list everything.

If you go through your 'articles' in the admin panel, you can place your mouse over each category heading. The number that appears in the status line behind the link (such as "articles_catXXX") is the number you want). Use the XXX value in the $cats_allowed array to set up what you want to be made available.

Other than that, I think I will replace the original script with this 2.5 version, as it really seems to be more along what admins could make better use of on their sites.
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: dimdom on September 27, 2008, 09:05:20 PM
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?
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Ianedres on September 27, 2008, 09:35:07 PM
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!
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: dimdom on September 27, 2008, 09:46:22 PM
That fixed it.


QuoteThanks for bringing this to my attention!

Thank you very much for the quick reply.





Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Ianedres on September 27, 2008, 10:25:31 PM
I will correct this in this topic to avoid future issues...  thanks for confirming that it corrected your error log.
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Bazil Greyson on November 14, 2008, 03:02:37 PM
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
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: ed_m2 on December 06, 2008, 11:50:43 AM
god bless you (and the search function) !
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Ianedres on December 06, 2008, 05:45:01 PM
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.
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: diegolyanky on December 09, 2008, 12:45:50 PM
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 ?  :'(
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: IchBin on December 09, 2008, 02:36:40 PM
You should check out this:
http://www.simplemachines.org/community/index.php?topic=34270
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: diegolyanky on December 15, 2008, 12:35:27 PM
It worked ...

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

Thanks a lot !!
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Renegd98 on December 24, 2008, 06:45:10 PM
This will great for use once the problem with displaying articles is fixed in Beta 3....
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: limteam on January 16, 2009, 04:58:19 PM
how can i:
-add "search an article by letter";
-put the $limit on the results shown on page;
-show results from specific allowed categories $cats_allowed ;
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Ianedres on January 16, 2009, 06:40:07 PM
Searching is not implemented in this snippet.

Forthcoming releases of TP will have article searching, and depending on how its code works, may try to work with the snippet code to produce some results.

I assume your other two questions are related to the first?
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: limteam on January 16, 2009, 08:02:20 PM
A| B| C| D|

when i select letter A underneath there should be listed all articles that start on letter A.and other two questions are related to the first one...
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: limteam on January 17, 2009, 02:47:04 PM
this is a code:


$mode = (isset($_GET['mode'])) ? intval($_GET['mode']) : '0';
switch ($mode)
{
case '1':

$cats_allowed = array(1, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 19, 20, 21, 22, 23, 24, 25, 26); // valid category numbers; '0' = all categories; separate values with commas

$name = mysql_escape_string(trim($_GET['name']));
$name = substr($name,0,1);

foreach($cats_allowed as $v){
$num = mysql_num_rows(mysql_query("SELECT (`id`) FROM `smf_tp_articles` WHERE `subject` LIKE '$name%' AND 'category' = ".$v.""));
}

$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$all = ceil($num/50);
if ($page>$all || $page<1) {
$page=1; $start=0;
} else {
$start = $page*50-50;

    if($num<1){   
    echo '<b>None!!!!</b><br/>';   
    }else{   
    echo 'Articles: '.$num.'<br/>';
    foreach($cats_allowed as $v){
    $WR=mysql_query("SELECT * FROM `smf_tp_articles` WHERE `subject` LIKE '$name%'  AND 'category' = ".$v." ORDER BY `id` DESC LIMIT ".$start.",50");
    }   
    while($q=mysql_fetch_array($WR)){
    $q['subject'] = htmlspecialchars($q['subject']);
    echo '<a href="./index.php?page='.$q['id'].'">'.$q['subject'].'</a><br/>';
       
}

for($i =1; $i <= $all; $i++)
{
echo $pr=(($i == 1 || $i == $all || abs($i-$page) < 2) ? ($i == $page ? " [$i] " : ' <b><a href="./index.php?action=Leha&mode=1&amp;name='.$name.'&amp;page='.$i.'">'.$i.'</a></b>') : (($pr == ' ... ' || $pr == '')? '' : ' ... '));
}
}
break;



default:
echo '<a href="./index.php?action=Leha&mode=1&amp;name=A">A</a>| ';
echo '<a href="./index.php?action=Leha&mode=1&amp;name=B">B</a>| ';
echo '<a href="./index.php?action=Leha&mode=1&amp;name=C">C</a>| ';
echo '<a href="./index.php?action=Leha&mode=1&amp;name=D">D</a>| ';
echo '<a href="./index.php?action=Leha&mode=1&amp;name=E">E</a>| ';
echo '<a href="./index.php?action=Leha&mode=1&amp;name=F">F</a>| ';
echo '<a href="./index.php?action=Leha&mode=1&amp;name=G">G</a>| ';
echo '<a href="./index.php?action=Leha&mode=1&amp;name=H">H</a>| ';
echo '<a href="./index.php?action=Leha&mode=1&amp;name=I">I</a>| ';
echo '<a href="./index.php?action=Leha&mode=1&amp;name=J">J</a>| ';
echo '<a href="./index.php?action=Leha&mode=1&amp;name=K">K</a>| ';
echo '<a href="./index.php?action=Leha&mode=1&amp;name=L">L</a>| ';
echo '<a href="./index.php?action=Leha&mode=1&amp;name=M">M</a>| ';
echo '<a href="./index.php?action=Leha&mode=1&amp;name=N">N</a>| ';
echo '<a href="./index.php?action=Leha&mode=1&amp;name=O">O</a>| ';
echo '<a href="./index.php?action=Leha&mode=1&amp;name=P">P</a>| ';
echo '<a href="./index.php?action=Leha&mode=1&amp;name=Q">Q</a>| ';
echo '<a href="./index.php?action=Leha&mode=1&amp;name=R">R</a>| ';
echo '<a href="./index.php?action=Leha&mode=1&amp;name=S">S</a>| ';
echo '<a href="./index.php?action=Leha&mode=1&amp;name=T">T</a>| ';
echo '<a href="./index.php?action=Leha&mode=1&amp;name=U">U</a>| ';
echo '<a href="./index.php?action=Leha&mode=1&amp;name=V">V</a>| ';
echo '<a href="./index.php?action=Leha&mode=1&amp;name=W">W</a>| ';
echo '<a href="./index.php?action=Leha&mode=1&amp;name=X">X</a>| ';
echo '<a href="./index.php?action=Leha&mode=1&amp;name=Y">Y</a>| ';
echo '<a href="./index.php?action=Leha&mode=1&amp;name=Z">Z</a>| ';
break;
}


But this code don't work with $cats_allowed.
who can repair that script?
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Ianedres on January 18, 2009, 08:02:34 AM
The code you have posted in the last post is not mine, and I don't see a practical approach in combining what you ask as it relates to the original snippet topic here.

I'll also ask that you start your own topic for support requests for such things to keep this topic for the snippet clear of any confusion.

Once the next version of TP is released, I do plan to upgrade this and other snippets for compatibility.
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: nasir on March 06, 2009, 06:21:49 PM
 Can you tell me does it work for TP v1.0

if it does,,,where to put the code ??
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: G6Cad on March 06, 2009, 06:25:35 PM
Did you read the first post and the rest of them as they hold all info you need, we do not run around with silverplatter and serv our members, they have to do some work on their own....
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Ianedres on March 06, 2009, 06:51:54 PM
Thanks G6...

Any snippet's I have posted will have all the information in the first message, along with the most current version of the code with any changes.

It is intended to be used as a php article, just to be clear.
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: dimdom on October 14, 2009, 07:28:47 PM
Is there a way to add the first image in every article (with fixed dimensions) in the list?
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: ZarPrime on October 15, 2009, 12:44:32 AM
dimdom,

Though I haven't tried this snippet, ,it's my understanding that this code shows the articles in a list form so I don't see how there would be room for an image.  Have you even tried the snippet yet?  The next time Ianedres is on, perhaps he can comment on this since it's his code.

ZarPrime
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: dimdom on October 15, 2009, 08:32:19 AM
Yes, I am using it for the last 18 months.  :)

Watch it in action here: http://www.authorway.com/SMF/index.php?page=138

There is room for images.
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: IchBin on October 15, 2009, 02:05:18 PM
So you want to add a tiny little image for each line?
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: dimdom on October 15, 2009, 03:40:33 PM
Quote from: IchBinâ„¢ on October 15, 2009, 02:05:18 PM
So you want to add a tiny little image for each line?

Yes.  :)

The first image in every article (if there is one).
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: IchBin on October 15, 2009, 04:55:17 PM
But how would you tell what the image is when it would be tiny enough to fit on each line? Honestly, I don't see this happening. You'd have to do some sort of text search to find an image in the article text, which to me would be very, very complicated.
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Ianedres on October 23, 2009, 04:34:34 PM
There would be several issues to address your request.

It would have to parse for BBC image tags in the article itself, thus adding many more queries (and code) to the script, and just would not seem practical.

Thanks Ich and Zar!
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Rafferty on July 15, 2010, 03:54:06 AM
Does this script have to be updated for use with TP 1.0 Beta 5-1?

I have had the block up for sometime on the previous version but has stopped working since upgrading to beta 5
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: ZarPrime on July 15, 2010, 04:18:15 AM
Rafferty,

Welcome back to our new site.

Yes, undoubtedly, if this snippet has database calls, it will have to be updated for the latest version.  I'm sure that Freddy or Ichbin will take a look at this as soon as they can and see if it can easily be updated.  What version of SMF are you using?  They will ask.

ZarPrime
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: IchBin on July 15, 2010, 02:46:19 PM
There was a problem with the way that TP was saving code in beta 5.1. YOu should probably update to beta5.2

If you're still running SMF 1.1.x it should work fine regardless of the TP version. But if you're using SMF2 it will need to be converted. Let us know which version you're running.
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Rafferty on September 06, 2010, 01:03:51 PM
Link to my site: https://aadaa.asn.au
SMF version: SMF ver. 2.0 RC3
TP version: TP ver. 1.0 Beta 5-2
Theme name and version: Default
Browser Name and Version: FF 3.6
Mods installed: many
Related Error messages: None

Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: IchBin on September 07, 2010, 04:42:06 PM
Try this code and see if it works. It worked on my SMF2 site.
// 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)
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Rafferty on September 08, 2010, 03:54:38 AM
Beautiful, thx very much Ich

Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: The Wizard on November 11, 2010, 03:18:56 PM
Ok but this is a waste of time...
Link to my site: http://www.tribeuniverse.com
SMF version: 2.0 RC4
TP version: 1.0 RC1
Default Forum Language: English
Theme name and version:CurveLife
Browser Name and Version: IE
Mods installed:Tiny Portal, Aeva Media
Related Error messages: None

Now that I have gotten that stuff out of the way....

I have changed your code a little for my use and so I'll post it here too -

echo '<div style="background-image:url(\'http://www.tribeuniverse.com/tribe-images/tribe-images/big_backgrounds/nature.jpg\');"> ';


// 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 = false; // show date in table; true or false
$show_views = false; // show number of views for each article
$show_author = false; // show name of author in table; true or false
$show_header = false; // 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 = ''; // background color for first row; 999999 = light gray
$css_row_alt = ''; // alternating row color; 666666 = dark gray

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

$limit = 20; // number of results to display per page
$category = 6; // 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 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)


Now my question -
Is there anyway to list the articles in alphabeticaly?

Thanks

The Wizard
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: IchBin on November 12, 2010, 12:17:47 AM
You keep saying it's a waste of time. What about our time? It's a waste of our time to have to ask people which versions of software they are running because the database queries are different, as well as the database row names, between the different versions of SMF and TP. It may appear as a waste of time to you, but it certainly saves us from having to ask you to do so.

You never said what you want to order it by alphabetically by. By the title? By the query title?

You should be able to change this section to get what you're asking.
ORDER BY date DESC

Change to "ORDER BY subject DESC"  if you want to sort it by the title. You can remove or leave DESC depending on which way you want it ordered alphabetically.
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: The Wizard on November 12, 2010, 01:49:20 AM
QuoteYou keep saying it's a waste of time. What about our time? It's a waste of our time to have to ask people which versions of software they are running because the database queries are different, as well as the database row names, between the different versions of SMF and TP. It may appear as a waste of time to you, but it certainly saves us from having to ask you to do so.

Sorry I just cut and pasted and should have left that out. I'm trully sorry if you were offended.
I would like to thank you for all the help you and your staff have provided. Without it I would have been lost.

Thanks

The Wizard
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: lurkalot on November 12, 2010, 09:41:39 AM
Quote from: The Wizard on November 11, 2010, 03:18:56 PM

I have changed your code a little for my use and so I'll post it here too -


Thanks for sharing.  O0  Would be handy if you edited your post above, and explained what changes you had made to the block.  Saves people having to try it just to find out what it does differently.   ;)
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: uniektekniek on December 21, 2010, 01:38:13 AM
Hey all, great block this one.. im having a slight problem with the "next page" links or sorting anything in general.. it just goes to a blank page.. sounds liek the same issue Rafferty was having on the first page of this thread, but it seems that was sorted out privately and theres not heaps of info on how to fix it... any ideas?? i've tried both php articles and blocks and the updated code for SMF2 posted by ichbin..



Link to my site: http://www.souldonsound.com/forum
SMF version: 2.0 RC3
TP version: 1.0 beta 5.2
Default Forum Language: English
Theme name and version:default
Browser Name and Version: FF 3.6
Mods installed: too many to list
Related Error messages: N/A
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: uniektekniek on December 21, 2010, 02:03:27 AM
nevermind.. just forgot to set the article its in.. all works great!

$page         = '';      // set to article # that this script is in
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: uniektekniek on December 21, 2010, 02:22:39 AM
so close to being perfect.. defining specific categories isnt working for me though.. is there something im missing in the below code? those 3 in the $cats_allowed variable are all i want.. i have made a seperate category for the article this code is in in an attempt to hide it from the 'No Specified Category' view.. but i cant seem to get rid of that category in the listings..



// 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   = true;   // 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         = 5;      // number of results to display per page
$cats_allowed   = array(1, 4, 10);   // valid category numbers; '0' = all categories; separate values with commas

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

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

$article      = array();
$limits         = array(5, 10, 20);
$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)

Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: IchBin on December 21, 2010, 04:36:41 PM
It doesn't look to me like that $cats_allowed variable is even used. I don't have any where to test this at the moment. What are you trying to do? Are you only wanting certain categories available to display?
Title: Re: [block] Approved Articles / Sorted, Paginated, Categorized, & Customized
Post by: Ianedres on December 23, 2010, 02:13:42 AM
This snippet only displays the articles in the specified category through the $category variable, or if it is a '0' value, will read through all the categories...