TP-Docs
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media

Recent

Welcome to TinyPortal. Please login or sign up.

December 12, 2024, 11:19:04 PM

Login with username, password and session length
Members
Stats
  • Total Posts: 195,443
  • Total Topics: 21,252
  • Online today: 167
  • Online ever: 6,457 (November 30, 2024, 02:40:09 PM)
Users Online
  • Users: 0
  • Guests: 126
  • Total: 126

[Block] Recent comments on articles.

Started by freddy888, October 16, 2009, 05:42:31 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

IchBin

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

MrCare

</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

IchBin


blu4lyf

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.
// *** 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);

Rus

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.  :)

wildenborch

#15
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?

   
   
// *** 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);

@rjen

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

wildenborch

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?




tino



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


@rjen

Quote from: tino on November 12, 2018, 08:33:54 PM


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



Better try...



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


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