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 11, 2022, 07:50:15 AM

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

Author Topic: [Block] Recent comments on articles.  (Read 34389 times)

0 Members and 1 Guest are viewing this topic.

Offline IchBin

  • Friends
  • *
  • Posts: 16231
Re: [Block] Recent comments on articles.
« Reply #10 on: August 06, 2012, 08:50:27 AM »
You can use substr() on any text to limit it. You can see the examples on this page.

http://php.net/manual/en/function.substr.php

Offline MrCare

  • Jr. Member
  • **
  • Posts: 62
Re: [Block] Recent comments on articles.
« Reply #11 on: August 06, 2012, 10:28:41 AM »
Code: [Select]
</div>
' , strlen($details['comment']) > 47 ? substr($details['comment'], 0, 47) .'... ' : $details['comment'] , '
</div>
</div>
</div>';
}

$smcFunc['db_free_result']($request);

hope anybody can help ... tq

Offline IchBin

  • Friends
  • *
  • Posts: 16231
Re: [Block] Recent comments on articles.
« Reply #12 on: August 06, 2012, 11:29:01 AM »
What do you need help with?

Offline blu4lyf

  • Newbie
  • *
  • Posts: 1
Re: [Block] Recent comments on articles.
« Reply #13 on: August 12, 2012, 11:22:42 AM »
I decided to share my variation of this block of code, in case anybody else is interested in it.  Changes I made are as follows:
  • Changed it to use the member's screen name (or real name if you like) instead of their log in name.
  • Changed the formatting to make it smaller so it fits into a side block easier.
  • Added the ability to limit the length of the comment it shows (without slicing up the html characters along the way) with a default of 60 characters.
Code: [Select]
// *** Config ***

// Set the maximum number of comments to show.
$limit = 5;
$commlim = 60;

// *** End Of Config ***


// Now the code..

global $scripturl, $db_prefix, $smcFunc, $txt;

$request = $smcFunc['db_query']('', '
SELECT n.value2, n.value3, n.value4, n.value5, m.subject, m.author_id, m.author, m.shortname, o.real_name
FROM {db_prefix}tp_articles m
INNER JOIN {db_prefix}tp_variables n ON n.value5 = m.id
INNER JOIN {db_prefix}members o ON n.value3 = o.id_member
WHERE n.type = {string:type}
ORDER BY n.value4 DESC
LIMIT {int:limit}',
array('type' => 'article_comment', 'limit' => $limit)
);

while ($row = $smcFunc['db_fetch_assoc']($request))
{
// Let's make these variable a little more readable...
$details = array(
'commenter' => '<a href="' . $scripturl . '?action=profile;u=' . $row['value3'] . '">' . $row['real_name'] . '</a>',
'comment' => $row['value2'],
'date' => $row['value4'],
'articleid' => $row['value5'],
'subject' =>  $row['subject'],
'query' => $row['shortname']
);
if (strlen(html_entity_decode($details['comment'])) > $commlim)
             $details['comment'] = htmlentities(substr(html_entity_decode($details['comment']), 0, ($commlim-3)) . '...');
// Build the output...
echo '
<div class="tborder tp_article_frame">
<div class="catbg tp_subject" style="font-size: smaller;">
<span class="tp_month">' , $txt['months_short'][date("n", $details['date'])] , '</span>
<span class="tp_thindivider"></span>
<span class="tp_day">' , date("d",$details['date']) , '</span>
<span class="tp_year"> ' , date("Y",$details['date']) , '</span>
' , ($details['query'] != '' ? '<a href="' . $scripturl . '?page=' . $details['query'] . '">' . $details['subject'] . '</a>' : '<a href="' . $scripturl . '?page=' . $details['articleid'] . '">' . $details['subject'] . '</a>') , '
</div>
<div class="tp_articletext" style="font-size: smaller;">
<div class="tp_details" style="overflow: auto">
<span class="smalltext" style="font-size: xx-small;">' , $details['commenter'] , ' commented :</span><br />
' , $details['comment'] , '
</div>
</div>
</div>';
}

$smcFunc['db_free_result']($request);

Offline Rus

  • Jr. Member
  • **
  • Posts: 31
    • Local Watering Hole
Re: [Block] Recent comments on articles.
« Reply #14 on: January 31, 2013, 11:06:19 AM »
Thanks!  Just used this version and changed the formatting back to regular size.  The biggest thing I liked more was using the Screen names instead of logins.  :)

Offline wildenborch

  • Support Team
  • *
  • Posts: 364
Re: [Block] Recent comments on articles.
« Reply #15 on: November 12, 2018, 02:37:34 AM »
Hi,

I found this code which is nearly perfect for me. However, if a comment is posted by a guest, the comment is not showing in this block.

What needs to be changed / added to also show the guest comments?

Code: [Select]
   
   
// *** Config ***

// Set the maximum number of comments to show.
$limit = 25;
$commlim = 90;

// *** End Of Config ***


// Now the code..

global $scripturl, $db_prefix, $smcFunc, $txt;

$request = $smcFunc['db_query']('', '
SELECT n.value2, n.value3, n.value4, n.value5, m.subject, m.author_id, m.author, m.shortname, o.member_name
FROM {db_prefix}tp_articles m
INNER JOIN {db_prefix}tp_variables n ON n.value5 = m.id
INNER JOIN {db_prefix}members o ON n.value3 = o.id_member
WHERE n.type = {string:type}
ORDER BY n.value4 DESC
LIMIT {int:limit}',
array('type' => 'article_comment', 'limit' => $limit)
);

while ($row = $smcFunc['db_fetch_assoc']($request))
{
// Let's make these variable a little more readable...
$details = array(
'commenter' => '<a href="' . $scripturl . '?action=profile;u=' . $row['value3'] . '">' . $row['member_name'] . '</a>',
'comment' => $row['value2'],
'date' => $row['value4'],
'articleid' => $row['value5'],
'subject' =>  $row['subject'],
'query' => $row['shortname']
);
if (strlen(html_entity_decode($details['comment'])) > $commlim)
             $details['comment'] = htmlentities(substr(html_entity_decode($details['comment']), 0, ($commlim-3)) . '...');
// Build the output...
echo '
<div class="tborder tp_article_frame">
<div class="catbg tp_subject" style="font-size: large;">


' , ($details['query'] != '' ? '<a href="' . $scripturl . '?page=' . $details['query'] . '">' . $details['subject'] . '</a>' : '<a href="' . $scripturl . '?page=' . $details['articleid'] . '">' . $details['subject'] . '</a>') , '
</div><B>
<div class="">On
<span class="tp_thindivider"></span>
<span class="">' , date("l",$details['date']) , '</span>
<span class="">' , date("d",$details['date']) , '</span>
<span class="">' , $txt['months_short'][date("n", $details['date'])] , '</span>
<span class=""> ' , date("Y",$details['date']) , '</span>
<span class=""> ' , date("H:i:s",$details['date']) , '</span>
<span class="tp_day"> ' , $details['commenter'] , '</span>
<span class="">commented: </B></span>

<div class="" style="color: #330000; font-size: 16px""><br>

' , $details['comment'] , '

</div>
<br></div>';
}


$smcFunc['db_free_result']($request);
« Last Edit: November 12, 2018, 04:41:39 AM by wildenborch »

Offline @rjen

  • Support Team
  • *
  • Posts: 3285
    • FJR-club Nederland
Re: [Block] Recent comments on articles.
« Reply #16 on: November 12, 2018, 09:10:08 AM »
You are using an inner join on the member table. Guests are not members so will not be included. Try a left OUTER join instead...
Running Latest TP on SMF2.1 at: www.fjr-club.nl

Testing TP on SMF2.0
Testing TP on SMF2.1 (latest Github)
Want to check what php version I am testing on? See: PHP Info

Offline wildenborch

  • Support Team
  • *
  • Posts: 364
Re: [Block] Recent comments on articles.
« Reply #17 on: November 12, 2018, 11:50:05 AM »
Thanks @rjen,

Changing

      INNER JOIN {db_prefix}tp_variables n ON n.value5 = m.id
      INNER JOIN {db_prefix}members o ON n.value3 = o.id_member

into

      LEFT OUTER JOIN {db_prefix}tp_variables n ON n.value5 = m.id
      LEFT OUTER JOIN {db_prefix}members o ON n.value3 = o.id_member

was indeed the solution.

Do you know what I should add to show "Guest" in case a guest comment is posted?




Offline tino

  • Developer
  • *
  • Posts: 2613
Re: [Block] Recent comments on articles.
« Reply #18 on: November 12, 2018, 01:33:54 PM »
Code: [Select]

IFNULL(o.member_name, 'Guest') AS member_name


Offline @rjen

  • Support Team
  • *
  • Posts: 3285
    • FJR-club Nederland
Re: [Block] Recent comments on articles.
« Reply #19 on: November 12, 2018, 01:35:17 PM »
Code: [Select]

IFNULL(o.member_name, 'Guest') AS member_name


Better try...

Code: [Select]

IFNULL(o.member_name, $txt['tp-guest']) AS member_name

Running Latest TP on SMF2.1 at: www.fjr-club.nl

Testing TP on SMF2.0
Testing TP on SMF2.1 (latest Github)
Want to check what php version I am testing on? See: PHP Info