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

Recent

Welcome to TinyPortal. Please login or sign up.

March 29, 2024, 04:43:31 AM

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

[Discussion] Simple Article Index

Started by freddy888, October 08, 2009, 02:53:53 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Freddy

I had an enquiry about the article index I use on my site.  It's pretty straightforward and I adapted it to work with more than one category.  This will simply list the articles in the categories by date.  There's no pagination or anything so if you have hundreds of articles then you'd probably want to try something else - I believe there are other blocks that do this, so try searching.

I use this in a PHP article and simply link to the page from a menu.

You could change the SQL 'ORDER BY date DESC' to 'ORDER BY subject' and make it alphabetical if you want that instead.

Here's the code, just customise the settings and text as you wish.


// Configuration

// Specify your categories, comma separated if more than one category.
$categories = array(3,10);

// The heading area.

echo '
<div style="text-align: center; border: 1px solid #242526; ">
<h3>Articles Index</h3>
<p>A short introduction can go in here...maybe an image too.</p>
</div>
<br />';

// Now for the code...
global $scripturl, $db_prefix;

$cats = implode(', ', $categories);

$request = db_query("SELECT id, date, subject, views, shortname FROM {$db_prefix}tp_articles WHERE category IN ($cats) ORDER BY date DESC", __FILE__, __LINE__);

echo '
<table class="tborder" width="100%">
<tr class="titlebg">
<td>Subject</td>
<td>Date</td>
<td align="center">Views</td>
</tr>';

while ($row = mysql_fetch_assoc($request))
{
if ($row['shortname'])
{
$pageid = $row['shortname'];
}
else
{
$pageid = $row['id'];
}

echo '
<tr class="windowbg">
<td style="padding: 3px 0px 3px 10px"><strong><a href="' , $scripturl, '?page=' , $pageid , '">' , $row['subject'] , '</a></strong></td>
<td style="padding: 3px 0px 3px 10px">' , date('jS, F Y', $row['date']) , '</td>
<td align="center" style="padding: 3px 0px 3px 15px">' , $row['views'] , '</td>
</tr>
';
}

echo '
</table>';

mysql_free_result($request);

lurkalot

I know it's a old thread but I'm glad I found it. 

Freddy, thanks for sharing this one, it works a treat.  O0

Freddy


Ken.

This is not working in SMF2 RC3/TP 1.0 RC1, any chance of an update?   8)
" If everything seems under control, you're not going fast enough." - Mario Andretti
Yesterday When I was Young.

Freddy

HI Ken,

I think you just need to change this part :

$request = db_query etc etc

to

$request = tpdb_query etc etc..


SN

Would like and update too please :)

Freddy


lurkalot

Quote from: Freddy on September 19, 2010, 01:37:18 PM
HI Ken,

I think you just need to change this part :

$request = db_query etc etc

to

$request = tpdb_query etc etc..

Thanks Freddy, that works a treat now on my SMF2 testbed.  O0

Freddy

#8
Great.  Another thing that occurred to me a week or two back on my site is that this will display inactive articles too.  So to remedy that use this line in place of the database query :

SMF 1 :

$request = db_query("SELECT id, date, subject, views, shortname FROM {$db_prefix}tp_articles WHERE category IN ($cats) AND off = 0 ORDER BY date DESC", __FILE__, __LINE__);

SMF 2 :

$request = tpdb_query("SELECT id, date, subject, views, shortname FROM {$db_prefix}tp_articles WHERE category IN ($cats) AND off = 0 ORDER BY date DESC", __FILE__, __LINE__);

And then it won't show your inactive articles.

I will have to add this to the snippets board later too, so it can be found more easily.

Edit : corrected wrong code... should be okay now.

lurkalot

Quote from: Freddy on September 19, 2010, 02:26:56 PM

Another thing that occurred to me a week or two back on my site is that this will display inactive articles too.  So to remedy that use this line in place of the database query :

SMF 2 :

$request = tpdb_query("SELECT id, date, subject, authorID, author, views, shortname FROM {$db_prefix}tp_articles WHERE category LIKE '8' AND off = 0 ORDER BY date DESC", __FILE__, __LINE__);

And then it won't show your inactive articles.

I will have to add this to the snippets board later too, so it can be found more easily.

Freddy just replaced this on my testsite, and the articles don't show at all now  ??? Put the code back as was and they show again.