To use this website completely, it is necessary to store cookies on your computer.
 

PHP Block for SMF Articles

Started by DarN, 8 years ago

previous topic - next topic

0 Members and 0 Guests are viewing this topic.

DarN

8 years ago
Code Select
global $smcFunc, $scripturl;

isAllowedTo('view_articles');

$dbresult = $smcFunc['db_query']('', "
SELECT a.ID_ARTICLE, a.title
FROM {db_prefix}articles AS a
WHERE a.approved = 1
ORDER BY a.date DESC
LIMIT 10
");

while ($row = $smcFunc['db_fetch_assoc']($dbresult))
{
echo '<a href="' . $scripturl . '?action=articles;sa=view;article=' . $row['ID_ARTICLE'] . '">' . $row['title'] . '</a><br />';
}
$smcFunc['db_free_result']($dbresult);


This will post a list of links to your 10 latest articles, sorted by date descending.

antechinus

#1
8 years ago
Nice one. Thanks.
Using Internet Explorer 6 on the internet is like urinating in a public swimming pool.
It's rude, there's no excuse for it, and anyone who does it should be ashamed of themselves.

fords8

#2
8 years ago
Anyone know how I can show the summary of the articles?

I want it to output like this:

Title Here --- Summary Here

I just want the title to be clickable also. The summary is just text.

fords8

#3
8 years ago
Ok, I did some work on this and came up with:

Code Select
global $smcFunc, $scripturl;

isAllowedTo('view_articles');

$dbresult = $smcFunc['db_query']('', "
SELECT a.ID_ARTICLE, a.title, a.description
FROM {db_prefix}articles AS a
WHERE a.approved = 1
ORDER BY a.date DESC
LIMIT 10
");

while ($row = $smcFunc['db_fetch_assoc']($dbresult))
{
echo '<center><a href="' . $scripturl . '?action=articles;sa=view;article=' . $row['ID_ARTICLE'] . '">' . $row['title'] . ' --- ' . $row['description'] . '</a></center><br />';
}
$smcFunc['db_free_result']($dbresult);


This will center the title and summary of the article in the block. It will make the whole thing clickable.

antechinus

#4
8 years ago
Nice job. Thanks for that.
Using Internet Explorer 6 on the internet is like urinating in a public swimming pool.
It's rude, there's no excuse for it, and anyone who does it should be ashamed of themselves.

pzrzao

#5
8 years ago
I just tried this script and it is impressive!

To make it even more useful, is it possible to filter for specific articles categories?  For instance, if I have categories for Games, Hardware, Tips - how could I modify the script to show only the Games category?

artus

#6
7 years ago
Hi,

I made a small modification to place the code in the left or right panel. I think it looks a bit better. I just can't manage to show the user who submitted the article. Maybe someone could help?

Code Select

global $smcFunc, $scripturl;

isAllowedTo('view_articles');

$dbresult = $smcFunc['db_query']('', "
   SELECT a.ID_ARTICLE, a.title, a.description, a.date
   FROM {db_prefix}articles AS a
   WHERE a.approved = 1
   ORDER BY a.date DESC
   LIMIT 10
");

while ($row = $smcFunc['db_fetch_assoc']($dbresult))
{
   echo '<a href="' . $scripturl . '?action=articles;sa=view;article=' . $row['ID_ARTICLE'] . '"><strong>' . $row['title']. '</strong></a><br/> <span class="smalltext">Submitted by: '. '???'. '<br/>['. timeformat($row['date']). ']</span><hr />';

}
$smcFunc['db_free_result']($dbresult);

codebirth

#7
7 years ago
This should do it

Code Select
global $smcFunc, $scripturl;

isAllowedTo('view_articles');

$dbresult = $smcFunc['db_query']('', "
   SELECT a.ID_ARTICLE, a.ID_MEMBER, a.title, a.description, a.date, b.real_name
   FROM {db_prefix}articles AS a
   LEFT JOIN {db_prefix}members AS b
      ON (a.ID_MEMBER=b.id_member)
   WHERE a.approved = 1
   ORDER BY a.date DESC
   LIMIT 10
");

while ($row = $smcFunc['db_fetch_assoc']($dbresult))
{
   echo '<a href="' . $scripturl . '?action=articles;sa=view;article=' . $row['ID_ARTICLE'] . '"><strong>' . $row['title']. '</strong></a><br/> <span class="smalltext">Submitted by: <a href="'. $scripturl .'?action=profile;u='. $row['ID_MEMBER'] . '">' . $row['real_name'] . '</a><br/>['. timeformat($row['date']). ']</span><hr />';

}
$smcFunc['db_free_result']($dbresult);


Haven't tested it, but hope it works.

c o d e b i r t h
http://codebirth.com/smf

artus

#8
7 years ago
Works great! Thanks!

Here is my version for the front page:

Code Select

global $smcFunc, $scripturl;

isAllowedTo('view_articles');

$dbresult = $smcFunc['db_query']('', "
   SELECT a.ID_ARTICLE, a.ID_MEMBER, a.title, a.description, a.date, b.real_name
   FROM {db_prefix}articles AS a
   LEFT JOIN {db_prefix}members AS b
      ON (a.ID_MEMBER=b.id_member)
   WHERE a.approved = 1
   ORDER BY a.date DESC
   LIMIT 10
");

while ($row = $smcFunc['db_fetch_assoc']($dbresult))
{
echo '<a href="' . $scripturl . '?action=articles;sa=view;article=' . $row['ID_ARTICLE'] . '"><strong>' . $row['title']. '</strong></a><span class="smalltext"> (Submitted by: <a href="'. $scripturl .'?action=profile;u='. $row['ID_MEMBER'] . '">' . $row['real_name'] . '</a> ['. timeformat($row['date']). '])<br/><i>'
.$row['description'] .'</i> </span><a href="' . $scripturl . '?action=articles;sa=view;article=' . $row['ID_ARTICLE'] . '"><strong>Read more</strong></a><hr />';

}
$smcFunc['db_free_result']($dbresult);


r2d2-trader

#9
7 years ago
How do i have to modify the php-code to get the articles in 2 or more rows on the frontpage?

cu Rob
"Aller Anfang ist leicht, und die letzten Stufen werden am schwersten und seltensten erstiegen."
Johann Wolfgang von Goethe

ben sisko

#10
6 years ago
i was looking for this. thank you!

what will be the change needed to show only one 'featured article' (article=x).
changing the article number by editing the block will do.

claul

#11
5 years ago
I have tried using this code but it breaks my forum. I created a PHP block, input code and once I make the block active everything wants to appear in my side bar. Is there another way of calling recent articles or is  this the only option?


Quote from: DarN ,  8 years ago
Code Select
global $smcFunc, $scripturl;

isAllowedTo('view_articles');

$dbresult = $smcFunc['db_query']('', "
SELECT a.ID_ARTICLE, a.title
FROM {db_prefix}articles AS a
WHERE a.approved = 1
ORDER BY a.date DESC
LIMIT 10
");

while ($row = $smcFunc['db_fetch_assoc']($dbresult))
{
echo '<a href="' . $scripturl . '?action=articles;sa=view;article=' . $row['ID_ARTICLE'] . '">' . $row['title'] . '</a><br />';
}
$smcFunc['db_free_result']($dbresult);


This will post a list of links to your 10 latest articles, sorted by date descending.

feline

#12
5 years ago
Which articles ? You have a mod for this installed?
I see no error in the code, but I can't say, if the query correct for that mod.

And .. isAllowedTo('view_articles') is ugly in this case .. if anyone have no access, the block i broken.
Try:
allowedTo('view_articles')
{
   code here have access
}
else
{
   say the user him have no access
}

Many are stubborn in relation to the path, a few in relation to the target.

claul

#13
5 years ago
I currently use the PortaMX system and create articles in it. I want to make a block that shows created articles in it and searched around here and found that code. Is there an easier way to do this?

feline

#14
5 years ago
In PortaMx you can use this: Article list sample ..
Many are stubborn in relation to the path, a few in relation to the target.