TinyPortal

Development => Block Codes => Topic started by: Freddy on October 08, 2009, 02:53:53 PM

Title: [Discussion] Simple Article Index
Post by: Freddy on October 08, 2009, 02:53:53 PM
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);
Title: Re: Simple Article Index
Post by: lurkalot on July 12, 2010, 08: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
Title: Re: Simple Article Index
Post by: Freddy on July 12, 2010, 01:41:34 PM
Welcome :)
Title: Re: Simple Article Index
Post by: Ken. on September 19, 2010, 12:15:09 PM
This is not working in SMF2 RC3/TP 1.0 RC1, any chance of an update?   8)
Title: Re: Simple Article Index
Post by: 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..

Title: Re: Simple Article Index
Post by: SN on September 19, 2010, 01:43:40 PM
Would like and update too please :)
Title: Re: Simple Article Index
Post by: Freddy on September 19, 2010, 02:04:00 PM
Hi SN, read above...
Title: Re: Simple Article Index
Post by: lurkalot on September 19, 2010, 02:12:58 PM
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
Title: Re: Simple Article Index
Post by: Freddy on September 19, 2010, 02:26:56 PM
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.
Title: Re: Simple Article Index
Post by: lurkalot on September 19, 2010, 02:45:31 PM
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.
Title: Re: Simple Article Index
Post by: Ken. on September 19, 2010, 03:32:06 PM
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..



That works, thanks Freddy.  O0
http://www.ourfamilyforum.org/FamilyForum/index.php?page=216
Now I just need to move some of my articles into the proper categories.  ^-^
Title: Re: Simple Article Index
Post by: Freddy on September 19, 2010, 03:49:23 PM
@Lurkalot, Sorry about that, I took the code from my site, which is slightly different.  I have amended the two lines and they appear to work okay now.  ::)

@Ken, glad to hear it :)
Title: Re: Simple Article Index
Post by: lurkalot on September 19, 2010, 03:58:45 PM
Thanks Freddy, that sorted it.  You're a star m8.  O0
Title: Re: Simple Article Index
Post by: Freddy on September 19, 2010, 04:11:10 PM
Hehe thanks :)

This code now has a home in the new snippets area, where the updated and correct code can be found :

http://www.tinyportal.net/index.php?topic=33208
Title: Re: Simple Article Index
Post by: Ken. on September 19, 2010, 07:04:10 PM
You know how sometimes you get one of those "Doh!" moments? 

If I copy&paste the code directly from Freddy's post it works just fine, but I wanted to do some editing so I placed it in Notepad++, did my edits and then pasted the resulting code to my article which promptly broke and gave me an error page.  :tickedoff:

You likely already know the problem... Notepad++ was set to 'Normal Text', not to 'PHP'.  :-[ 
Title: Re: Simple Article Index
Post by: Freddy on September 19, 2010, 09:16:50 PM
That's why coding is such fun  :o
Title: Re: Simple Article Index
Post by: SN on September 20, 2010, 05:44:43 PM
Quote from: Freddy on September 19, 2010, 02:04:00 PM
Hi SN, read above...

Oops Sorry Freddy, i don;t know how i missed that  :idiot2:

Its working great now thanks  O0

This is abit of a odd request, but is possible to have this in a PHP block, limited to only 5 articles for the block. without The heading area and excluding the latest 2 ( or what ever number people want to exclude ) articles by date?

The reason i ask for this is because, on my site i display 2 articles on my front page, and i have been looking for a way to list the previous 5 articles under that in a list just how you have listed it in this code.
Title: Re: Simple Article Index
Post by: ZarPrime on September 20, 2010, 10:56:44 PM
Wow, that really is an odd request ~ query for the last 7 but only show the first 5 of those last 7? :D

ZarPrime
Title: Re: Simple Article Index
Post by: SN on September 21, 2010, 09:56:27 AM
Quote from: ZarPrime on September 20, 2010, 10:56:44 PM
Wow, that really is an odd request ~ query for the last 7 but only show the first 5 of those last 7? :D

ZarPrime

;D yeah basically lol.

don;t worry if it to much hassle...it's just something i have wanted to do for some time now. so i thought i would just put it out there :P
Title: Re: Simple Article Index
Post by: ZarPrime on September 21, 2010, 03:53:41 PM
Well, I don't know if I could figure it out but Freddy may be able to.  I was just confirming your observation that it really is an odd request. ;)

ZarPrime
Title: Re: Simple Article Index
Post by: Freddy on September 21, 2010, 04:22:10 PM
It's not too tricky if you know a little SQL.  Assuming you are on SMF 2 try this out.

As always let me know how you get on :)


// Block code to display x number of articles ordered by date and offset to ignore first x articles.
// SMF 2
// Freddy

// Configuration

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

// Limit the number of articles shown.
$limit = 5;

// Set the offset - in other words ignore the first x articles
$offset = 2;

// End Config


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

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

$request = tpdb_query("SELECT id, date, subject, views, shortname FROM {$db_prefix}tp_articles WHERE category IN ($cats) ORDER BY date DESC LIMIT $limit OFFSET $offset", __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);
Title: Re: Simple Article Index
Post by: ZarPrime on September 21, 2010, 04:56:00 PM
Freddy,

Nice. O0 That's why you are a coder and I'm just support. :D

ZarPrime
Title: Re: Simple Article Index
Post by: Freddy on September 21, 2010, 04:58:41 PM
there's no 'just' about it ZP, you make a fine job of support :)
Title: Re: Simple Article Index
Post by: SN on September 21, 2010, 07:21:46 PM
Freddy, you never cease to amaze me... it's perfect.

Thanks Mate!
Title: Re: Simple Article Index
Post by: Freddy on September 21, 2010, 08:14:03 PM
No problem my friend, happy to help :)
Title: Re: Simple Article Index
Post by: Mick on December 30, 2010, 01:08:30 AM
Thank Freddy ;)
Title: Re: Simple Article Index
Post by: Freddy on December 30, 2010, 05:34:02 PM
Welcome mate :)
Title: Simple Article Index Modified for the small left or right blocks
Post by: saks on April 08, 2011, 07:54:38 AM
it show titles of 5 last articles for selected categories:
// Configuration

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

// The heading area.

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

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

$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 LIMIT 0, 5", __FILE__, __LINE__);

echo '
<table  width="100%">';
$i = 0;
while ($row = mysql_fetch_assoc($request))
{

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

echo '
<tr class="smalltext">
<td style="padding: 3px 0px 3px 8px"><li><a href="http://www.paraplanoff.net/' , $scripturl, '?page=' , $pageid , '">' , $row['subject'] , '</a></li></td>
</tr>
';
    if ($i == 4) {break;}
    $i = $i+1;
}

echo '
</table>';

mysql_free_result($request);
Title: Re: Simple Article Index
Post by: ZarPrime on April 08, 2011, 02:48:46 PM
saks,

I'll wait for Freddy to respond because I don't really understand what your question is.  However, it does look like you are missing some code in there and your category array is wrong because there should be a comma between each of the categories.

ZarPrime
Title: Re: Simple Article Index
Post by: Freddy on April 08, 2011, 03:06:58 PM
Sorry no idea what the question is either.  Please give us more details.
Title: Re: Simple Article Index
Post by: saks on April 11, 2011, 03:04:35 AM
The block which I have specified, works well. It shows last five articles from the chosen categories and it is convenient for using in the left or right panel on the main page of a site. I didn't have a question, I only wanted to show to its everything, that is possible it to whom it will appear useful.
Title: Re: Simple Article Index
Post by: lurkalot on April 11, 2011, 08:50:57 AM
saks, as it states it's a simple article index.  I don't think it has any permissions to adjust.  Freddy might be able to mod it for you. 

I suppose you could set the whole block to groups that should see it, as a workaround.

Or try this block. Not sure what it's like as I haven't tried it, but I believe it has permission settings.  http://www.tinyportal.net/index.php?topic=33195.0
Title: Re: Simple Article Index
Post by: Freddy on April 11, 2011, 10:54:14 AM
Yes, just a simple block - you could use block permissions as Lurkalot suggests.
Title: Re: Simple Article Index
Post by: saks on October 23, 2011, 10:44:15 AM
After update for last TP version this block is not work in my site, and show error:
Fatal error: Call to undefined function tpdb_query() in /usr/local/www/data-dist/saks/Themes/default/TPsubs.template.php(114) : eval()'d code on line 13

How can I fix it ?
Title: Re: Simple Article Index
Post by: lurkalot on October 23, 2011, 11:00:54 AM
saks,   SMF 2.x ?

If so, TP uses the new and improved $smcFunc['db_query'] for TP + SMF2 that's probably why it stopped working.  Your snippet will need updating to work.  I'm not sure if Freddy updated the snippet yet, but I have the updated version running on my site, if you need it.
Title: Re: Simple Article Index
Post by: saks on October 23, 2011, 11:49:23 AM
Yes! I use SMF 2.01. I will be very grateful for this updating
Title: Re: Simple Article Index
Post by: Freddy on October 23, 2011, 11:56:35 AM
Thanks Lurkalot that saves me some work :)

If you want to post it here I will add it to the snippets board.

I don't suppose you can get a screen shot of it in action if you get a minute ?
Title: Re: Simple Article Index
Post by: lurkalot on October 23, 2011, 12:10:21 PM
No problem, this should be the one. Don't forget to make a note of your categories array before you swap the code ;) 

@Freddy, this is also in the team board, it was updated by Brad for me when I upgraded.  I don't have many articles to be honest, but I'll try and get a screengrab shortly.


// Configuration

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

// The heading area.

echo '
<div style="text-align: center; border: 1px solid #242526; ">   
   <h3>Articles Index</h3>
   <p>This is a list of articles, submitted by our members.</p>
</div>
<br />';

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

$request = $smcFunc['db_query']('', '
SELECT id, date, subject, views, shortname
FROM {db_prefix}tp_articles
WHERE category IN ({array_int:cats})
AND off = 0
ORDER BY date DESC',
array('cats' => $categories)
);

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

while ($row = $smcFunc['db_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>';

$smcFunc['db_free_result']($request);
Title: Re: Simple Article Index
Post by: lurkalot on October 23, 2011, 12:28:26 PM
This might be a bit small.  I'll update it if you like.
Title: Re: Simple Article Index
Post by: Freddy on October 23, 2011, 12:46:20 PM
That's fine thanks mate  O0
Title: Re: Simple Article Index
Post by: saks on October 25, 2011, 01:52:18 AM
It`s realy work! Thank you !
Title: Re: Simple Article Index
Post by: lurkalot on October 25, 2011, 08:33:13 AM
Glad it's working.  O0

Marking topic solved.
Title: Re: [Discussion] Simple Article Index
Post by: wildenborch on June 18, 2013, 01:00:21 PM
Hi,

The following code posted by Lurkalot code is working on my site but I want to limit the number of articles to the latest 10
How can this be done?
// Configuration

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

// The heading area.

echo '
<div style="text-align: center; border: 1px solid #242526; ">   
   <h3>Articles Index</h3>
   <p>This is a list of articles, submitted by our members.</p>
</div>
<br />';

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

$request = $smcFunc['db_query']('', '
SELECT id, date, subject, views, shortname
FROM {db_prefix}tp_articles
WHERE category IN ({array_int:cats})
AND off = 0
ORDER BY date DESC',
array('cats' => $categories)
);

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

while ($row = $smcFunc['db_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>';

$smcFunc['db_free_result']($request);
Title: Re: [Discussion] Simple Article Index
Post by: Freddy on June 18, 2013, 01:18:19 PM
Change :

$request = $smcFunc['db_query']('', '
SELECT id, date, subject, views, shortname
FROM {db_prefix}tp_articles
WHERE category IN ({array_int:cats})
AND off = 0
ORDER BY date DESC',
array('cats' => $categories)
);


to :

$request = $smcFunc['db_query']('', '
SELECT id, date, subject, views, shortname
FROM {db_prefix}tp_articles
WHERE category IN ({array_int:cats})
AND off = 0
ORDER BY date DESC
LIMIT 10',
array('cats' => $categories)
);
Title: Re: [Discussion] Simple Article Index
Post by: wildenborch on June 18, 2013, 03:19:39 PM
Freddy,

Thank you very much!
I also inlcuded: AND approved = 1
and it works! So I'm very happy!

$request = $smcFunc['db_query']('', '
SELECT id, date, subject, views, shortname
FROM {db_prefix}tp_articles
WHERE category IN ({array_int:cats})
                AND approved = 1
AND off = 0
ORDER BY date DESC
LIMIT 10',
array('cats' => $categories)
);




Best regards,
Wildenborch
Title: Re: [Discussion] Simple Article Index
Post by: Freddy on June 18, 2013, 04:00:24 PM
Good stuff :)