Login  |  Register
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media

Recent

Welcome, Guest. Please login or register.
Did you miss your activation email?

August 19, 2022, 08:31:40 PM

Login with username, password and session length
Members
  • Total Members: 3843
  • Latest: Tricky
Stats
  • Total Posts: 193479
  • Total Topics: 21088
  • Online today: 60
  • Online ever: 1035
  • (November 26, 2020, 04:45:12 AM)
Users Online
Users: 0
Guests: 45
Total: 45

Author Topic: [Discussion] Simple Article Index  (Read 25396 times)

0 Members and 2 Guests are viewing this topic.

Offline Freddy

  • Moderator
  • *
  • Posts: 1615
    • AiDreams
[Discussion] Simple Article Index
« on: October 08, 2009, 07:53:53 AM »
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.

Code: [Select]
// 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);
« Last Edit: October 27, 2011, 09:40:44 AM by Freddy »

Offline lurkalot

  • Administrator
  • *
  • Posts: 7409
    • Camera Craniums
Re: Simple Article Index
« Reply #1 on: July 12, 2010, 01:52:50 AM »
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

Offline Freddy

  • Moderator
  • *
  • Posts: 1615
    • AiDreams
Re: Simple Article Index
« Reply #2 on: July 12, 2010, 06:41:34 AM »
Welcome :)

Offline Ken.

  • Support Team
  • *
  • Posts: 4362
    • Our FamilyForum
Re: Simple Article Index
« Reply #3 on: September 19, 2010, 05:15:09 AM »
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.

Offline Freddy

  • Moderator
  • *
  • Posts: 1615
    • AiDreams
Re: Simple Article Index
« Reply #4 on: September 19, 2010, 06:37:18 AM »
HI Ken,

I think you just need to change this part :

$request = db_query etc etc

to

$request = tpdb_query etc etc..


Offline SN

  • Full Member
  • ***
  • Posts: 137
    • SpursNetwork - Tottenham Hotspur Fansite
Re: Simple Article Index
« Reply #5 on: September 19, 2010, 06:43:40 AM »
Would like and update too please :)

Offline Freddy

  • Moderator
  • *
  • Posts: 1615
    • AiDreams
Re: Simple Article Index
« Reply #6 on: September 19, 2010, 07:04:00 AM »
Hi SN, read above...

Offline lurkalot

  • Administrator
  • *
  • Posts: 7409
    • Camera Craniums
Re: Simple Article Index
« Reply #7 on: September 19, 2010, 07:12:58 AM »
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

Offline Freddy

  • Moderator
  • *
  • Posts: 1615
    • AiDreams
Re: Simple Article Index
« Reply #8 on: September 19, 2010, 07:26:56 AM »
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 :

Code: [Select]
$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 :

Code: [Select]
$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.
« Last Edit: September 19, 2010, 08:47:52 AM by Freddy »

Offline lurkalot

  • Administrator
  • *
  • Posts: 7409
    • Camera Craniums
Re: Simple Article Index
« Reply #9 on: September 19, 2010, 07:45:31 AM »

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 :

Code: [Select]
$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.