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);
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
Welcome :)
This is not working in SMF2 RC3/TP 1.0 RC1, any chance of an update? 8)
HI Ken,
I think you just need to change this part :
$request = db_query etc etc
to
$request = tpdb_query etc etc..
Would like and update too please :)
Hi SN, read above...
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
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.
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.
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. ^-^
@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 :)
Thanks Freddy, that sorted it. You're a star m8. O0
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
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'. :-[
That's why coding is such fun :o
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.
Wow, that really is an odd request ~ query for the last 7 but only show the first 5 of those last 7? :D
ZarPrime
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
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
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);
Freddy,
Nice. O0 That's why you are a coder and I'm just support. :D
ZarPrime
there's no 'just' about it ZP, you make a fine job of support :)
Freddy, you never cease to amaze me... it's perfect.
Thanks Mate!
No problem my friend, happy to help :)
Thank Freddy ;)
Welcome mate :)
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);
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
Sorry no idea what the question is either. Please give us more details.
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.
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
Yes, just a simple block - you could use block permissions as Lurkalot suggests.
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 ?
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.
Yes! I use SMF 2.01. I will be very grateful for this updating
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 ?
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);
This might be a bit small. I'll update it if you like.
That's fine thanks mate O0
It`s realy work! Thank you !
Glad it's working. O0
Marking topic solved.
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);
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)
);
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
Good stuff :)