TinyPortal

Development => Block Codes => Topic started by: JPDeni on May 05, 2009, 04:33:13 PM

Title: [Discussion] Random topic
Post by: JPDeni on May 05, 2009, 04:33:13 PM
Edit by Freddy : In the first part of this thread you will find code for SMF1.  If you want SMF 2 code then that can be found here (http://www.tinyportal.net/index.php?topic=33286)...

Feel free to continue in this thread with discussion.  Cheers :)

==========================================

This is a continuation of the request for a block in this topic (http://www.tinyportal.net/index.php/topic,23006.0.html). I've given a few more options and compacted the code.

What it does: Displays the first post from random topics, rather like the "recent topics" functions. It will show topics from all boards that the user can see. You can limit it to one category of boards if you prefer. You can also limit the number of characters to display. You can set the number of posts.

Where to put it: In either a php block or a php article. If you want more than one topic to display, it would probably be best to put it into an article.


global $db_prefix, $txt, $user_info;

// If you want to limit it to one category, enter the category number here
// If you want to use all categories, set it to 0
$category = 0 ;

// Set how many posts you want to display
$limit = 5;

// If you want to limit the number of characters displayed, enter the length here
// If you want the whole post to be displayed, no matter what the length, set it to 0
$maxlength = 1000;

loadLanguage('Stats');

$result = db_query("
    SELECT m.ID_BOARD, m.icon, m.subject, m.body, IFNULL(mem.realName, m.posterName) AS posterName, m.posterTime,
         t.numReplies, t.ID_TOPIC, m.ID_MEMBER, m.smileysEnabled, m.ID_MSG, t.locked
  FROM ({$db_prefix}topics AS t, {$db_prefix}messages AS m, {$db_prefix}boards as b)
    LEFT JOIN {$db_prefix}members AS mem ON (mem.ID_MEMBER = m.ID_MEMBER)
  WHERE m.ID_MSG = t.ID_FIRST_MSG
  AND b.ID_BOARD = m.ID_BOARD
  AND $user_info[query_see_board] " .
  ($category > 0 ? "AND ID_CAT = $category" : "")
  . " ORDER BY RAND() LIMIT $limit", __FILE__, __LINE__);

while ($row = mysql_fetch_assoc($result)){
if ($maxlength > 0 && strlen($row['body']) > $maxlength)
{
$row['body'] = substr($row['body'], 0, $maxlength);
while (substr($row['body'],-1) <> ' ')
$row['body'] = substr($row['body'],0,-1);
$row['body'] .= ' ...';
}

$row['body'] = parse_bbc($row['body'], $row['smileysEnabled'], $row['ID_MSG']);

// Check that this message icon is there...
if (empty($modSettings['messageIconChecks_disable']) && !isset($icon_sources[$row['icon']]))
  $icon_sources[$row['icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['icon'] . '.gif') ? 'images_url' : 'default_images_url';

censorText($row['subject']);
censorText($row['body']);

$news['icon'] = '<img src="' . $settings[$icon_sources[$row['icon']]] . '/post/' . $row['icon'] . '.gif" align="middle" alt="' . $row['icon'] . '" border="0" />';
$news['subject'] = $row['subject'];
$news['time'] = timeformat($row['posterTime']);
$news['body'] = $row['body'];
$news['href'] = $scripturl . '?topic=' . $row['ID_TOPIC'] . '.0';
$news['link'] = '<a href="' . $scripturl . '?topic=' . $row['ID_TOPIC'] . '.0">' . $row['numReplies'] . ' ' . ($row['numReplies'] == 1 ? $txt['smf_news_1'] : $txt['smf_news_2']) . '</a>';
$news['comment_link'] = !empty($row['locked']) ? '' : '<a href="' . $scripturl . '?action=post;topic=' . $row['ID_TOPIC'] . '.' . $row['numReplies'] . ';num_replies=' . $row['numReplies'] . '">' . $txt['smf_news_3'] . '</a>';
$news['poster'] = !empty($row['ID_MEMBER']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row['ID_MEMBER'] . '">' . $row['posterName'] . '</a>' : $row['posterName'];
$news['locked'] = !empty($row['locked']);

echo '
  <div>
   <a href="', $news['href'], '">', $news['icon'], '</a> <b>', $news['subject'], '</b>
   <div class="smaller">', $news['time'], ' ', $txt[525], ' ', $news['poster'], '</div>
    <div class="post" style="padding: 2ex 0;">', $news['body'], '</div>
    ', $news['link'], $news['locked'] ? '' : ' | ' . $news['comment_link'], '<hr />
</div>';
}
mysql_free_result($result);

Title: Re: [BLOCK] Random topic
Post by: alving on May 06, 2009, 01:36:48 AM
thanks heaps, Jpdeni! 
Title: Re: [BLOCK] Random topic
Post by: alving on August 06, 2009, 01:18:32 PM
jpdeni, is it possible to include the posters' avatar on each of the randomly posted topics?  thanks in advance!  :)
Title: Re: [BLOCK] Random topic
Post by: JPDeni on August 06, 2009, 01:50:35 PM
I'm sure that it is possible, but I don't know how to do it. I don't do avatars. Sorry.
Title: Re: [BLOCK] Random topic
Post by: alving on August 09, 2009, 04:27:27 AM
can anyone please help?

thanks in advance
Title: Re: [BLOCK] Random topic
Post by: ZarPrime on August 09, 2009, 10:39:30 AM
ajg,

If you do a search in the block code snippets board for Avatar, you will find some snippets that display Avatars.  You may be able to look through some of those code snippets and use some of the code one of them uses and adapt it  to display the avatars in this block.

ZarPrime
Title: Re: [BLOCK] Random topic
Post by: Freddy on August 09, 2009, 01:18:57 PM
Working on this...
Title: Re: [BLOCK] Random topic
Post by: Freddy on August 09, 2009, 02:01:22 PM
JP, just to let you know, you need $settings to be declared in your globals - otherwise the little post thingy icon doesn't show up I found .

Here's what I came up with.  I haven't done anything really to format the output, I left it to the end user to decide how best to display the avatar.  I just did the code to grab the avatar.

global $db_prefix, $txt, $user_info, $settings, $memberContext;

// If you want to limit it to one category, enter the category number here
// If you want to use all categories, set it to 0
$category = 0 ;

// Set how many posts you want to display
$limit = 5;

// If you want to limit the number of characters displayed, enter the length here
// If you want the whole post to be displayed, no matter what the length, set it to 0
$maxlength = 1000;

loadLanguage('Stats');

$result = db_query("
   SELECT m.ID_BOARD, m.icon, m.subject, m.body, IFNULL(mem.realName, m.posterName) AS posterName, m.posterTime,
        t.numReplies, t.ID_TOPIC, m.ID_MEMBER, m.smileysEnabled, m.ID_MSG, t.locked
 FROM ({$db_prefix}topics AS t, {$db_prefix}messages AS m, {$db_prefix}boards as b)
   LEFT JOIN {$db_prefix}members AS mem ON (mem.ID_MEMBER = m.ID_MEMBER)
 WHERE m.ID_MSG = t.ID_FIRST_MSG
 AND b.ID_BOARD = m.ID_BOARD
 AND $user_info[query_see_board] " .
 ($category > 0 ? "AND ID_CAT = $category" : "")
 . " ORDER BY RAND() LIMIT $limit", __FILE__, __LINE__);

while ($row = mysql_fetch_assoc($result)){
if ($maxlength > 0 && strlen($row['body']) > $maxlength)
{
$row['body'] = substr($row['body'], 0, $maxlength);
while (substr($row['body'],-1) <> ' ')
$row['body'] = substr($row['body'],0,-1);
$row['body'] .= ' ...';
}

// Get the avatar...
$memberID = $row['ID_MEMBER'];
loadMemberData($memberID);
loadMemberContext($memberID);
$news['avatar'] = $memberContext[$memberID]['avatar']['image'];

$row['body'] = parse_bbc($row['body'], $row['smileysEnabled'], $row['ID_MSG']);

// Check that this message icon is there...
if (empty($modSettings['messageIconChecks_disable']) && !isset($icon_sources[$row['icon']]))
 $icon_sources[$row['icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['icon'] . '.gif') ? 'images_url' : 'default_images_url';

censorText($row['subject']);
censorText($row['body']);

$news['icon'] = '<img src="' . $settings[$icon_sources[$row['icon']]] . '/post/' . $row['icon'] . '.gif" align="middle" alt="' . $row['icon'] . '" border="0" />';
$news['subject'] = $row['subject'];
$news['time'] = timeformat($row['posterTime']);
$news['body'] = $row['body'];
$news['href'] = $scripturl . '?topic=' . $row['ID_TOPIC'] . '.0';
$news['link'] = '<a href="' . $scripturl . '?topic=' . $row['ID_TOPIC'] . '.0">' . $row['numReplies'] . ' ' . ($row['numReplies'] == 1 ? $txt['smf_news_1'] : $txt['smf_news_2']) . '</a>';
$news['comment_link'] = !empty($row['locked']) ? '' : '<a href="' . $scripturl . '?action=post;topic=' . $row['ID_TOPIC'] . '.' . $row['numReplies'] . ';num_replies=' . $row['numReplies'] . '">' . $txt['smf_news_3'] . '</a>';
$news['poster'] = !empty($row['ID_MEMBER']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row['ID_MEMBER'] . '">' . $row['posterName'] . '</a>' : $row['posterName'];
$news['locked'] = !empty($row['locked']);

echo '
<div>' ,
$news['avatar'] , '
</div>
<div>
<a href="', $news['href'], '">', $news['icon'], '</a> <b>', $news['subject'], '</b>
<div class="smaller">', $news['time'], ' ', $txt[525], ' ', $news['poster'], '</div>
<div class="post" style="padding: 2ex 0;">', $news['body'], '</div>
   ', $news['link'], $news['locked'] ? '' : ' | ' . $news['comment_link'], '<hr />
</div>';
}
mysql_free_result($result);


You could put some padding round the avatar maybe, split it all into two halves or whatever.
Title: Re: [BLOCK] Random topic
Post by: ZarPrime on August 09, 2009, 05:19:42 PM
Hi freddy,

I figured you would pick up on this.  Good job. :up:

I knew that $memberContext was required in the globals but I had forgotten about $settings.  Keep up the good work. ;)

ZarPrime
Title: Re: [BLOCK] Random topic
Post by: Freddy on August 09, 2009, 07:22:42 PM
No problem Zar.  Funnily enough I was working on something the other day to grab the avatars myself for a mod I am working on.  Glad I could help.
Title: Re: [BLOCK] Random topic
Post by: IchBin on August 09, 2009, 07:29:43 PM
Just remember that each time you call the loadMemberContext to grab this info, you add a query to the database. I thought it a good idea to loop through that function for an array of members once, and it added like 10 queries because I was trying to get 10 users avatars. For this code it works great, but if you wanted to get more than one user, you'd be better to combine your query into a single one just to grab the avatar info. Just my $.02 :)
Title: Re: [BLOCK] Random topic
Post by: Freddy on August 09, 2009, 07:38:39 PM
Yeah that sounds wise.  I was a little worried by the overheads, but figured it would be okay for this.  For my mod I could need to pull quite a few avatars so I will give your suggestion a go.

P.S. I was trying to look up the function in the SMF functions database but for some reason it's been down since yesterday it looks like.  They're working on it.  What I may do is take a look at the details and maybe post an alternative.
Title: Re: [BLOCK] Random topic
Post by: IchBin on August 10, 2009, 04:04:17 AM
I've blocked your PM's.... :P No seriously, some posts must have been removed or something as we have a limit of 5 posts here before you can PM. Anyway, one more post and you can PM me I think. I've been getting a ton of PM's lately, and I'm not getting notifications for any of them for some reason. So I most likely have missed something if you sent one. I'll send you an email shortly. You picked an odd place to post for my attention. lol

Title: Re: [BLOCK] Random topic
Post by: historik on August 10, 2009, 04:09:15 AM
ok thx
Title: Re: [BLOCK] Random topic
Post by: alving on August 12, 2009, 10:48:14 AM
woww... thank you very much freddy888 for the code!!!  might have some more questions for you..   ;)
Title: Re: [BLOCK] Random topic
Post by: Freddy on August 12, 2009, 11:14:12 AM
You are welcome :)

Probably just needs some work on the layout still so it's just the way you want it...
Title: Re: [BLOCK] Random topic
Post by: alving on August 19, 2009, 12:59:26 PM
hello freddy888

it took me ages to come up with the formatting.  so as not to change so much in jpdeni's code, here is what i thought.

avatar image   post icon and post title
                    date and time of post
                    display name


please get rid of the word "by" before the username.  
please put a thin border around the avatar.  
a provision to resize the avatar
a provision to have a generic avatar for those without an avatar

thanks once again!  
Title: Re: [BLOCK] Random topic
Post by: Freddy on August 19, 2009, 01:10:56 PM
Hi ajg, yes that looks do-able.  So basically you want the avatar over on the left-hand side like a forum post ?  And everything else on the right?  Not sure where you want the actual message to appear...maybe like this :


Avatar      |       post icon & title
               |       date and time
               |       display name
--------------------------------------------
main message underneath ?


please get rid of the word "by" before the username - no problem
please put a thin border around the avatar - no problem
a provision to resize the avatar - that should be fine
a provision to have a generic avatar for those without an avatar - can do I think..

I need to go to the shops now (darn RealLife!) but it shouldn't be too long a wait.

Cheers.
Title: Re: [BLOCK] Random topic
Post by: alving on August 19, 2009, 09:32:49 PM
that's exactly what i want please. thanks in advance!
Title: Re: [BLOCK] Random topic
Post by: Freddy on August 19, 2009, 09:38:13 PM
Ok mate.  I went out tonight or I would have posted it already.  I am working on it, I'll drop it in tomorrow (getting late here) at some point.  Cheers :)
Title: Re: [BLOCK] Random topic
Post by: alving on August 20, 2009, 12:05:52 AM
no worries! cheers, mate
Title: Re: [BLOCK] Random topic
Post by: Freddy on August 20, 2009, 12:40:20 PM
OK here goes...

There's some new configurations which are pretty self-explanatory but I will just mention them here.

$avatararea - sets the width of the avatar container - this is a percentage.  Depending on how wide your forum is you will probably need to play with this setting for the best and most even layout.

$avwidth, $avheight - override the actual size of the avatar image, should look OK if your avatars are mostly square.

$noavatarurl - currently set to look in the root directory - ie in the same location as your SMF index.php for a file called 'noavatar.jpg'.  Just change this to whereever you want to put it or just leave it alone.  You will need the 'no avatar' image copied to your root - I've attached a few versions - just change the name of the one you like to 'noavatar.jpg' and drop it in your forum root.

I tested this on a few themes and it seems to be fine.

Let me know how you get on :)

global $db_prefix, $txt, $user_info, $settings, $memberContext;

// Configuration...

// Percentage width of avatar area
$avatararea = 15;
// Colour of border around avatar
$avbordercol= '#fff';
// Default width and height of avatar
$avwidth = 75;
$avheight= 75;
// Path to 'no avatar' image.
$noavatarurl = 'noavatar.jpg';

// If you want to limit it to one category, enter the category number here
// If you want to use all categories, set it to 0
$category = 0 ;

// Set how many posts you want to display
$limit = 5;

// If you want to limit the number of characters displayed, enter the length here
// If you want the whole post to be displayed, no matter what the length, set it to 0
$maxlength = 1000;

loadLanguage('Stats');

$result = db_query("
   SELECT m.ID_BOARD, m.icon, m.subject, m.body, IFNULL(mem.realName, m.posterName) AS posterName, m.posterTime,
        t.numReplies, t.ID_TOPIC, m.ID_MEMBER, m.smileysEnabled, m.ID_MSG, t.locked
 FROM ({$db_prefix}topics AS t, {$db_prefix}messages AS m, {$db_prefix}boards as b)
   LEFT JOIN {$db_prefix}members AS mem ON (mem.ID_MEMBER = m.ID_MEMBER)
 WHERE m.ID_MSG = t.ID_FIRST_MSG
 AND b.ID_BOARD = m.ID_BOARD
 AND $user_info[query_see_board] " .
 ($category > 0 ? "AND ID_CAT = $category" : "")
 . " ORDER BY RAND() LIMIT $limit", __FILE__, __LINE__);

while ($row = mysql_fetch_assoc($result)){
if ($maxlength > 0 && strlen($row['body']) > $maxlength)
{
$row['body'] = substr($row['body'], 0, $maxlength);
while (substr($row['body'],-1) <> ' ')
$row['body'] = substr($row['body'],0,-1);
$row['body'] .= ' ...';
}

// Get the avatar and build the image link for it, if it exists...
$memberID = $row['ID_MEMBER'];
loadMemberData($memberID);
loadMemberContext($memberID);

       if (!empty($memberContext[$memberID]['avatar']['href']))
      {
       $news['avatar'] = '<img width="' . $avwidth . '" height="' . $avheight . '" style="border:1px solid ' . $avbordercol . ' ;padding: 2px" alt="' . $row['posterName'] . '" title="' . $row['posterName'] . '" src="' . $memberContext[$memberID]['avatar']['href'] . '" />';
       }
       else
       {
                $news['avatar'] = '<img width="' . $avwidth . '" height="' . $avheight . '" style="border:1px solid ' . $avbordercol . ' ;padding: 2px" alt="' . $row['posterName'] . '" title="' . $row['posterName'] . '" src="' . $noavatarurl . '" />';
       }

$row['body'] = parse_bbc($row['body'], $row['smileysEnabled'], $row['ID_MSG']);

       // Check that this message icon is there...
if (empty($modSettings['messageIconChecks_disable']) && !isset($icon_sources[$row['icon']]))
 $icon_sources[$row['icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['icon'] . '.gif') ? 'images_url' : 'default_images_url';

censorText($row['subject']);
censorText($row['body']);

$news['icon'] = '<img src="' . $settings[$icon_sources[$row['icon']]] . '/post/' . $row['icon'] . '.gif" align="middle" alt="' . $row['icon'] . '" border="0" />';
$news['subject'] = $row['subject'];
$news['time'] = timeformat($row['posterTime']);
$news['body'] = $row['body'];
$news['href'] = $scripturl . '?topic=' . $row['ID_TOPIC'] . '.0';
$news['link'] = '<a href="' . $scripturl . '?topic=' . $row['ID_TOPIC'] . '.0">' . $row['numReplies'] . ' ' . ($row['numReplies'] == 1 ? $txt['smf_news_1'] : $txt['smf_news_2']) . '</a>';
$news['comment_link'] = !empty($row['locked']) ? '' : '<a href="' . $scripturl . '?action=post;topic=' . $row['ID_TOPIC'] . '.' . $row['numReplies'] . ';num_replies=' . $row['numReplies'] . '">' . $txt['smf_news_3'] . '</a>';
$news['poster'] = !empty($row['ID_MEMBER']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row['ID_MEMBER'] . '">' . $row['posterName'] . '</a>' : $row['posterName'];
$news['locked'] = !empty($row['locked']);

// Display it all...
echo '
<div style="width:100%;display:table">
<div style="width: ' , $avatararea , '%;float:left;text-align:center">
               ' , $news['avatar'] , '
</div>
<div style="width: ' , 99 - $avatararea ,  '%;float:right">
<a href="', $news['href'], '">', $news['icon'], '</a> <strong>', $news['subject'], '</strong>
<div class="smaller">', $news['time'], '<br />' , $txt[525] , ' ' , $news['poster'], '</div>
</div>
<div class="post" style="padding: 2ex 0;clear:both">', $news['body'], '</div>
   ', $news['link'], $news['locked'] ? '' : ' | ' . $news['comment_link'], '<hr />
</div>';
}

mysql_free_result($result);

Title: Re: [BLOCK] Random topic
Post by: alving on August 20, 2009, 01:11:04 PM
thanks freddy888!  just another thing, can you please include the word "by" before the username.  sorry, i know i asked for it to be deleted by it just doesn't look nice without it..  my bad.. thanks heaps, though.. maybe edit the above code so as not to confuse the others..
Title: Re: [BLOCK] Random topic
Post by: Freddy on August 20, 2009, 01:18:19 PM
No problem - that's been edited now...  :D

Maybe you can drop a link so others can see it in action ?  I don't have a site I need to use it on at the moment.

Cheers :)
Title: Re: [BLOCK] Random topic
Post by: alving on August 22, 2009, 02:48:57 PM
thanks again freddy888.. i'm still playing with my site a bit, so once it's full on, i'll provide a link.. thanks once again freddy888 and  jpdeni..
Title: Re: [BLOCK] Random topic
Post by: Freddy on August 22, 2009, 04:21:24 PM
Okay my friend, no pressure...and yes thanks to JPDeni from me too  :)
Title: Re: [BLOCK] Random topic
Post by: alving on April 26, 2010, 10:34:54 AM
jpdeni and/or freddy888,

this code can display random topics from certain category/ies.  how can i display random topics from a specific board/s.  i still want the avatars displayed..

thanks in advance guys..  still using TP098 and SMF 11
Title: Re: [BLOCK] Random topic
Post by: JPDeni on April 26, 2010, 03:16:27 PM
Change


// If you want to limit it to one category, enter the category number here
// If you want to use all categories, set it to 0
$category = 0 ;


to


// If you want to limit it to one board, enter the category number here
// If you want to use all boards, set it to 0
$board = 0 ;


And change


AND $user_info[query_see_board] " .
  ($category > 0 ? "AND ID_CAT = $category" : "")
  . " ORDER BY RAND() LIMIT $limit", __FILE__, __LINE__);


to


AND $user_info[query_see_board] " .
  ($board > 0 ? "AND b.ID_BOARD = $board" : "")
  . " ORDER BY RAND() LIMIT $limit", __FILE__, __LINE__);

Title: Re: [BLOCK] Random topic
Post by: alving on April 26, 2010, 10:18:53 PM
thanks once again, jpdeni  :up: i will try out later... much appreciated
Title: Re: [BLOCK] Random topic
Post by: JPDeni on April 27, 2010, 01:58:06 AM
I didn't test it, but it should work. Just some simple substitutions.
Title: Re: [BLOCK] Random topic
Post by: mariusfv on August 22, 2010, 09:50:20 PM
Hi Freddy !

I test as admin that code(first code because I'm not interested to show avatars) in a .php article for front page, after I see is possible with a replacement to show from a specific board instead of category.

I have 2 questions :

1. Is possible after the topic appear to be the title of topic click able and linked ?... when user click the title of topic what appear in article to go to that topic ?(now is possible only if click at comments....down article page-> ugly)

2. Is possible to show random from specific boards not only 1 board( Ex: board 12, 15 etc;  ? )

I must first test as admin...before to make it for all users.

10x in advance !
Title: Re: [BLOCK] Random topic
Post by: Freddy on August 23, 2010, 05:19:36 PM
So you want to remove the 'x Comments | Write Comment' bit  and instead put the link into the topic title.... yes I can do that.

When you say 'boards' you really do mean boards and not categories ?

I am assuming you are on SMF 1 ?
Title: Re: [BLOCK] Random topic
Post by: MrCare on October 18, 2010, 04:27:49 PM
Can this code suitable with smf v2?
Title: Re: [BLOCK] Random topic
Post by: Freddy on October 18, 2010, 04:40:09 PM
Not yet, I will try and convert it.  Back later ...
Title: Re: [BLOCK] Random topic
Post by: Freddy on October 18, 2010, 05:40:18 PM
I've put the new version for SMF 2 in the new block area here :

http://www.tinyportal.net/index.php?topic=33286

It's producing no errors for me.   Test it out and let us know how you get on :)
Title: Re: [Discussion] Random topic
Post by: MrCare on October 18, 2010, 06:13:13 PM
yes.. nice.. but how to make clickable subject link?
Title: Re: [Discussion] Random topic
Post by: MrCare on October 18, 2010, 06:35:57 PM
I find it.. tq..

Now im using a simple code
  // Display it all...
   echo '
     <div style="width:100%;display:table">
         <img src="http://myweb/bullet.png" alt="" style="margin: 0pt 2px 0pt 0pt;" border="0"><a href="', $news['href'], '">', $news['subject'], '</strong>
       
   ';   
}

mysql_free_result($result);


i want to show 8 topics with to colums... how can i get it?
Title: Re: [Discussion] Random topic
Post by: Freddy on October 18, 2010, 06:39:04 PM
I'm away for a few days now,  See if you can figure it out.  Have fun.
Title: Re: [Discussion] Random topic
Post by: MrCare on October 20, 2010, 06:49:47 AM
hope there is a way to limit subject character. tq
Title: Re: [Discussion] Random topic
Post by: MrCare on October 26, 2010, 05:59:31 AM
Quote from: QUBBAH on October 20, 2010, 06:49:47 AM
hope there is a way to limit subject character. tq

Hope anybody can help.
Title: Re: [Discussion] Random topic
Post by: ZarPrime on October 26, 2010, 02:33:12 PM
QUBBAH,

Freddy is away on a brief holiday.  I'm sure he will return shortly and will be able to help you out.  Please be patient. ;)

ZarPrime
Title: Re: [Discussion] Random topic
Post by: Freddy on October 26, 2010, 08:18:04 PM
Thanks ZP :)

I am back now, but have a lot of other stuff I need to catch up on first.  I'll get to you at some point, just not sure exactly when at the moment.
Title: Re: [Discussion] Random topic
Post by: MrCare on November 06, 2013, 03:13:53 PM
is this code is still suitable with smf 2.0.6?
Title: Re: [Discussion] Random topic
Post by: IchBin on November 06, 2013, 10:08:17 PM
As far as I can tell, the code that Freddy posted should work with SMF2 and TP 1. No way to know for sure unless you try it. :)
Title: Re: [Discussion] Random topic
Post by: MrCare on November 12, 2013, 10:50:54 PM
i tried it with smf 2.0.6 and it cant be done... its okay with smf 2.0 rc3
Title: Re: [Discussion] Random topic
Post by: ZarPrime on November 13, 2013, 02:00:23 PM
MrCare,

What does the block look like?  Any errors in your error log?

ZarPrime
Title: Re: [Discussion] Random topic
Post by: Freddy on November 27, 2013, 05:43:50 PM
If he's using the SMF2 code I put here then I think all it needs is converting up to use the smcFunc...

http://www.tinyportal.net/index.php?topic=33286
Title: Re: [Discussion] Random topic
Post by: MrCare on December 03, 2013, 05:24:52 AM
Try to add smfunc but cant be done..
global $db_prefix, $txt, $user_info, $settings, $smcFunc, $memberContext, $scripturl, $boardurl;
Title: Re: [Discussion] Random topic
Post by: MrCare on December 03, 2013, 05:33:00 AM
Yes fine the problem..

Note: It appears that your database may require an upgrade. Your forum's files are currently at version SMF 2.0.6, while your database is at version 2.0 RC3. The above error might possibly go away if you execute the latest version of upgrade.php.

How can i solve this.. coz my forum can't be upgrade.. always stuck while upgrade.. tq
Title: Re: [Discussion] Random topic
Post by: IchBin on December 03, 2013, 05:53:14 AM
That is not an error that TP has anything to do with. You should check with the SMF folks for your problem.
Title: Re: [Discussion] Random topic
Post by: MrCare on June 06, 2014, 04:41:28 PM
Still having a problem to make random topic for new smf version 2.0.7 and latest tp version.
Title: Re: [Discussion] Random topic
Post by: MrCare on June 16, 2014, 07:47:14 AM
Quote from: MrCare on June 06, 2014, 04:41:28 PM
Still having a problem to make random topic for new smf version 2.0.7 and latest tp version.
still waiting...
Title: Re: [Discussion] Random topic
Post by: Freddy on June 17, 2014, 10:25:51 PM
Haven't you learnt to code yet ? ;)

I will try and convert it over the next few days if I get time.
Title: Re: [Discussion] Random topic
Post by: Freddy on June 17, 2014, 11:12:05 PM
I had time to do it tonight between the football almost.  Try this :

http://www.tinyportal.net/index.php?topic=33286.msg282305#msg282305 (http://www.tinyportal.net/index.php?topic=33286.msg282305#msg282305)

I leave you to do the styling and CSS.
Title: Re: [Discussion] Random topic
Post by: ayozeve on October 14, 2015, 03:03:16 PM
hi, how can i exclude categories from the random topics? Thanks a lot.
Title: Re: [Discussion] Random topic
Post by: illori on October 14, 2015, 03:11:49 PM
are you using the code linked in the post above yours? if so you can define categories to use, but not categories you want to ignore. so you can list all the categories you want to use.
Title: Re: [Discussion] Random topic
Post by: ayozeve on October 14, 2015, 03:52:48 PM
yes i am using this:

global $txt, $user_info, $settings, $memberContext, $scripturl, $boardurl, $smcFunc;

// Configuration...

// Percentage width of avatar area
$avatararea = 30;
// Colour of border around avatar
$avbordercol= '#fff';
// Default width and height of avatar
$avwidth = 35;
$avheight= 35;
// Path to 'no avatar' image.
$noavatarurl = $boardurl . '/Themes/default/images/noavatar.jpg';

// If you want to limit it to one category, enter the category number here
// If you want to use all categories, set it to 0
$category = 0 ;

// Set how many posts you want to display
$limit = 5;

// If you want to limit the number of characters displayed, enter the length here
// If you want the whole post to be displayed, no matter what the length, set it to 0
$maxlength = 1000;

loadLanguage('Stats');

$result = $smcFunc['db_query']('', "
SELECT m.id_board, m.icon, m.subject, m.body, IFNULL(mem.real_name, m.poster_name) AS poster_name, m.poster_time,
t.num_replies, t.id_topic, m.id_member, m.smileys_enabled, m.id_msg, t.locked
FROM ({db_prefix}topics AS t, {db_prefix}messages AS m, {db_prefix}boards as b)
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
WHERE m.id_msg = t.id_first_msg
AND b.id_board = m.id_board
AND $user_info[query_see_board] " .
($category > 0 ? "AND id_cat = {int:cat}" : "")
. " ORDER BY RAND() LIMIT {int:limit}",
array(
'cat' => $category,
'limit' => $limit)
);

while ($row = $smcFunc['db_fetch_assoc']($result))
{
if ($maxlength > 0 && strlen($row['body']) > $maxlength)
{
$row['body'] = substr($row['body'], 0, $maxlength);
while (substr($row['body'],-1) <> ' ')
$row['body'] = substr($row['body'],0,-1);
$row['body'] .= ' ...';
}

// Get the avatar and build the image link for it, if it exists...
$memberID = $row['id_member'];
loadMemberData($memberID);
loadMemberContext($memberID);

if (!empty($memberContext[$memberID]['avatar']['href']))
{
$news['avatar'] = '<img width="' . $avwidth . '" height="' . $avheight . '" style="border:1px solid ' . $avbordercol . ' ;padding: 2px" alt="' . $row['poster_name'] . '" title="' . $row['poster_name'] . '" src="' . $memberContext[$memberID]['avatar']['href'] . '" />';
}
else
{
$news['avatar'] = '<img width="' . $avwidth . '" height="' . $avheight . '" style="border:1px solid ' . $avbordercol . ' ;padding: 2px" alt="' . $row['poster_name'] . '" title="' . $row['poster_name'] . '" src="' . $noavatarurl . '" />';
}

$row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']);

// Check that this message icon is there...
if (empty($modSettings['messageIconChecks_disable']) && !isset($icon_sources[$row['icon']]))
$icon_sources[$row['icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['icon'] . '.gif') ? 'images_url' : 'default_images_url';

censorText($row['subject']);
censorText($row['body']);

$news['icon'] = '<img src="' . $settings[$icon_sources[$row['icon']]] . '/post/' . $row['icon'] . '.gif" align="middle" alt="' . $row['icon'] . '" border="0" />';
$news['subject'] = $row['subject'];
$news['time'] = timeformat($row['poster_time']);
$news['body'] = $row['body'];
$news['href'] = $scripturl . '?topic=' . $row['id_topic'] . '.0';
$news['link'] = '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['num_replies'] . ' ' . ($row['num_replies'] == 1 ? $txt['ssi_comment'] : $txt['ssi_comments']) . '</a>';
$news['comment_link'] = !empty($row['locked']) ? '' : '<a href="' . $scripturl . '?action=post;topic=' . $row['id_topic'] . '.' . $row['num_replies'] . ';num_replies=' . $row['num_replies'] . '">' . $txt['ssi_write_comment'] . '</a>';
$news['poster'] = !empty($row['id_member']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>' : $row['poster_name'];
$news['locked'] = !empty($row['locked']);

// Display it all...try your own styling. Might be better in a table.
echo '
<div style="width:100%;display:table">
<div style="width: ' , $avatararea , '%;float:left;text-align:center">
                ' , $news['avatar'] , '
</div>
<div style="width: ' , 99 - $avatararea ,  '%;float:right">
<a href="', $news['href'], '">', $news['icon'], '</a> <strong>', $news['subject'], '</strong>
<div class="smaller">', $news['time'], '<br />' , $txt['by'] , ' ' , $news['poster'], '</div>
</div>
<div class="post" style="padding: 2ex 0;clear:both">', $news['body'], '</div>
    ', $news['link'], $news['locked'] ? '' : ' | ' . $news['comment_link'], '<hr />
</div>';
}

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



but if i change $category = 0 ; for another number or numbers separate by comma, it doesn't show anything. Can you help me?
Title: Re: [Discussion] Random topic
Post by: illori on October 14, 2015, 03:57:14 PM
try something like this

$category = array(1,3,10);
Title: Re: [Discussion] Random topic
Post by: ayozeve on October 14, 2015, 04:02:31 PM
at the beginning i used it... and it show:

Wrong value type sent to the database. Integer expected. (cat)
Title: Re: [Discussion] Random topic
Post by: illori on October 14, 2015, 04:24:37 PM
then i guess the script will only work with 1 category or all categories.
Title: Re: [Discussion] Random topic
Post by: Freddy on October 15, 2015, 10:22:26 PM
Quote from: illori on October 14, 2015, 04:24:37 PM
then i guess the script will only work with 1 category or all categories.
Yes that is correct. I just adapted it so you can exclude categories. Try this...

Just set this to zero...

$category = 0;

Then set $exclude to an array of cat ids. For example...

$exclude = array(12, 5, 3);

and it will ignore those.

global $txt, $user_info, $settings, $memberContext, $scripturl, $boardurl, $smcFunc;

// Configuration...

// Percentage width of avatar area
$avatararea = 30;
// Colour of border around avatar
$avbordercol= '#fff';
// Default width and height of avatar
$avwidth = 35;
$avheight= 35;
// Path to 'no avatar' image.
$noavatarurl = $boardurl . '/Themes/default/images/noavatar.jpg';

// If you want to limit it to one category, enter the category number here
// If you want to use all categories, set it to 0. If you want to exclude cats below set this to zero too.
$category = 0 ;

// Exclude categories ? Use the category ids eg. $exclude = array(3,6);
// Also set $category above to 0 (zero) for this to work.
$exclude = array();

// Set how many posts you want to display
$limit = 5;

// If you want to limit the number of characters displayed, enter the length here
// If you want the whole post to be displayed, no matter what the length, set it to 0
$maxlength = 1000;

loadLanguage('Stats');

$result = $smcFunc['db_query']('', "
SELECT m.id_board, m.icon, m.subject, m.body, IFNULL(mem.real_name, m.poster_name) AS poster_name, m.poster_time,
t.num_replies, t.id_topic, m.id_member, m.smileys_enabled, m.id_msg, t.locked
FROM ({db_prefix}topics AS t, {db_prefix}messages AS m, {db_prefix}boards as b)
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
WHERE m.id_msg = t.id_first_msg
AND b.id_board = m.id_board
AND $user_info[query_see_board]" .
($category > 0 ? " AND id_cat = {int:cat}" : "")
.
(!empty($exclude) ? " AND id_cat NOT IN ({array_int:exclude})" : "")
. "
ORDER BY RAND() LIMIT {int:limit}",
array(
'exclude' => $exclude,
'cat' => $category,
'limit' => $limit)
);

while ($row = $smcFunc['db_fetch_assoc']($result))
{
if ($maxlength > 0 && strlen($row['body']) > $maxlength)
{
$row['body'] = substr($row['body'], 0, $maxlength);
while (substr($row['body'],-1) <> ' ')
$row['body'] = substr($row['body'],0,-1);
$row['body'] .= ' ...';
}

// Get the avatar and build the image link for it, if it exists...
$memberID = $row['id_member'];
loadMemberData($memberID);
loadMemberContext($memberID);

if (!empty($memberContext[$memberID]['avatar']['href']))
{
$news['avatar'] = '<img width="' . $avwidth . '" height="' . $avheight . '" style="border:1px solid ' . $avbordercol . ' ;padding: 2px" alt="' . $row['poster_name'] . '" title="' . $row['poster_name'] . '" src="' . $memberContext[$memberID]['avatar']['href'] . '" />';
}
else
{
$news['avatar'] = '<img width="' . $avwidth . '" height="' . $avheight . '" style="border:1px solid ' . $avbordercol . ' ;padding: 2px" alt="' . $row['poster_name'] . '" title="' . $row['poster_name'] . '" src="' . $noavatarurl . '" />';
}

$row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']);

// Check that this message icon is there...
if (empty($modSettings['messageIconChecks_disable']) && !isset($icon_sources[$row['icon']]))
$icon_sources[$row['icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['icon'] . '.gif') ? 'images_url' : 'default_images_url';

censorText($row['subject']);
censorText($row['body']);

$news['icon'] = '<img src="' . $settings[$icon_sources[$row['icon']]] . '/post/' . $row['icon'] . '.gif" align="middle" alt="' . $row['icon'] . '" border="0" />';
$news['subject'] = $row['subject'];
$news['time'] = timeformat($row['poster_time']);
$news['body'] = $row['body'];
$news['href'] = $scripturl . '?topic=' . $row['id_topic'] . '.0';
$news['link'] = '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['num_replies'] . ' ' . ($row['num_replies'] == 1 ? $txt['ssi_comment'] : $txt['ssi_comments']) . '</a>';
$news['comment_link'] = !empty($row['locked']) ? '' : '<a href="' . $scripturl . '?action=post;topic=' . $row['id_topic'] . '.' . $row['num_replies'] . ';num_replies=' . $row['num_replies'] . '">' . $txt['ssi_write_comment'] . '</a>';
$news['poster'] = !empty($row['id_member']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>' : $row['poster_name'];
$news['locked'] = !empty($row['locked']);

// Display it all...try your own styling. Might be better in a table.
echo '
<div style="width:100%;display:table">
<div style="width: ' , $avatararea , '%;float:left;text-align:center">
                ' , $news['avatar'] , '
</div>
<div style="width: ' , 99 - $avatararea ,  '%;float:right">
<a href="', $news['href'], '">', $news['icon'], '</a> <strong>', $news['subject'], '</strong>
<div class="smaller">', $news['time'], '<br />' , $txt['by'] , ' ' , $news['poster'], '</div>
</div>
<div class="post" style="padding: 2ex 0;clear:both">', $news['body'], '</div>
    ', $news['link'], $news['locked'] ? '' : ' | ' . $news['comment_link'], '<hr />
</div>';
}

$smcFunc['db_free_result']($result);
Title: Re: [Discussion] Random topic
Post by: ayozeve on October 15, 2015, 11:40:26 PM
Thanks a lot but i have just insert it and it doesnt't work for me :-[. I wrote this:

global $txt, $user_info, $settings, $memberContext, $scripturl, $boardurl, $smcFunc;

// Configuration...

// Percentage width of avatar area
$avatararea = 8;
// Colour of border around avatar
$avbordercol= '#fff';
// Default width and height of avatar
$avwidth = 35;
$avheight= 35;
// Path to 'no avatar' image.
$noavatarurl = $boardurl . '/Themes/default/images/noavatar.jpg';

// If you want to limit it to one category, enter the category number here
// If you want to use all categories, set it to 0. If you want to exclude cats below set this to zero too.
$category = 0 ;

// Exclude categories ? Use the category ids eg. $exclude = array(3,6);
// Also set $category above to 0 (zero) for this to work.
$exclude = array(54,40,13,43,47,14,38,86,19,83,61,30,34,16,32,17,28,53,41,8,44,48,9,39,45,85,18,82,60,29,35,11,31,10,26,64,59,65,66,77,78,67,68,84,69,81,70,71,72,73,74,75,76);

// Set how many posts you want to display
$limit = 1;

// If you want to limit the number of characters displayed, enter the length here
// If you want the whole post to be displayed, no matter what the length, set it to 0
$maxlength = 1000;

loadLanguage('Stats');

$result = $smcFunc['db_query']('', "
SELECT m.id_board, m.icon, m.subject, m.body, IFNULL(mem.real_name, m.poster_name) AS poster_name, m.poster_time,
t.num_replies, t.id_topic, m.id_member, m.smileys_enabled, m.id_msg, t.locked
FROM ({db_prefix}topics AS t, {db_prefix}messages AS m, {db_prefix}boards as b)
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
WHERE m.id_msg = t.id_first_msg
AND b.id_board = m.id_board
AND $user_info[query_see_board]" .
($category > 0 ? " AND id_cat = {int:cat}" : "")
.
(!empty($exclude) ? " AND id_cat NOT IN ({array_int:exclude})" : "")
. "
ORDER BY RAND() LIMIT {int:limit}",
array(
'exclude' => $exclude,
'cat' => $category,
'limit' => $limit)
);

while ($row = $smcFunc['db_fetch_assoc']($result))
{
if ($maxlength > 0 && strlen($row['body']) > $maxlength)
{
$row['body'] = substr($row['body'], 0, $maxlength);
while (substr($row['body'],-1) <> ' ')
$row['body'] = substr($row['body'],0,-1);
$row['body'] .= ' ...';
}

// Get the avatar and build the image link for it, if it exists...
$memberID = $row['id_member'];
loadMemberData($memberID);
loadMemberContext($memberID);

if (!empty($memberContext[$memberID]['avatar']['href']))
{
$news['avatar'] = '<img width="' . $avwidth . '" height="' . $avheight . '" style="border:1px solid ' . $avbordercol . ' ;padding: 2px" alt="' . $row['poster_name'] . '" title="' . $row['poster_name'] . '" src="' . $memberContext[$memberID]['avatar']['href'] . '" />';
}
else
{
$news['avatar'] = '<img width="' . $avwidth . '" height="' . $avheight . '" style="border:1px solid ' . $avbordercol . ' ;padding: 2px" alt="' . $row['poster_name'] . '" title="' . $row['poster_name'] . '" src="' . $noavatarurl . '" />';
}

$row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']);

// Check that this message icon is there...
if (empty($modSettings['messageIconChecks_disable']) && !isset($icon_sources[$row['icon']]))
$icon_sources[$row['icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['icon'] . '.gif') ? 'images_url' : 'default_images_url';

censorText($row['subject']);
censorText($row['body']);

$news['icon'] = '<img src="' . $settings[$icon_sources[$row['icon']]] . '/post/' . $row['icon'] . '.gif" align="middle" alt="' . $row['icon'] . '" border="0" />';
$news['subject'] = $row['subject'];
$news['time'] = timeformat($row['poster_time']);
$news['body'] = $row['body'];
$news['href'] = $scripturl . '?topic=' . $row['id_topic'] . '.0';
$news['link'] = '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['num_replies'] . ' ' . ($row['num_replies'] == 1 ? $txt['ssi_comment'] : $txt['ssi_comments']) . '</a>';
$news['comment_link'] = !empty($row['locked']) ? '' : '<a href="' . $scripturl . '?action=post;topic=' . $row['id_topic'] . '.' . $row['num_replies'] . ';num_replies=' . $row['num_replies'] . '">' . $txt['ssi_write_comment'] . '</a>';
$news['poster'] = !empty($row['id_member']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>' : $row['poster_name'];
$news['locked'] = !empty($row['locked']);

// Display it all...try your own styling. Might be better in a table.
echo '
<div style="width:100%;display:table">
<div style="width: ' , $avatararea , '%;float:left;text-align:center">
                ' , $news['avatar'] , '
</div>
<div style="width: ' , 99 - $avatararea ,  '%;float:right">
<a href="', $news['href'], '">', $news['icon'], '</a> <strong>', $news['subject'], '</strong>
<div class="smaller">', $news['time'], '<br />' , $txt['by'] , ' ' , $news['poster'], '</div>
</div>
<div class="post" style="padding: 2ex 0;clear:both">', $news['body'], '</div>
    ', $news['link'], $news['locked'] ? '' : ' | ' . $news['comment_link'], '<hr />
</div>';
}

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



I only let 2 board to show (to see it is correct), but it doesn't work. Are there any bad????
The problem with this script is that can show private content from boards, i guess.

Thanks so much.
Title: Re: [Discussion] Random topic
Post by: Freddy on October 16, 2015, 11:59:56 AM
$exclude needs to be the cats you want left out.
Title: Re: [Discussion] Random topic
Post by: ayozeve on October 16, 2015, 12:43:09 PM
Yes, i understood but it didn't work for me. It show all the boards and the exlude boards too.
Title: Re: [Discussion] Random topic
Post by: illori on October 16, 2015, 12:47:25 PM
are you entering the numbers for the CATEGORIES or the numbers for the BOARDS. you need to make sure they are the id numbers for the CATEGORIES, if you have a category with both public and private boards in it, then you need to exclude that CATEGORY.
Title: Re: [Discussion] Random topic
Post by: ayozeve on October 16, 2015, 01:17:27 PM
Quote from: illori on October 16, 2015, 12:47:25 PM
are you entering the numbers for the CATEGORIES or the numbers for the BOARDS. you need to make sure they are the id numbers for the CATEGORIES, if you have a category with both public and private boards in it, then you need to exclude that CATEGORY.

You have rights, I was put id boards instead id cats. Thanks a lot men!!
Title: Re: [Discussion] Random topic
Post by: Ken. on October 16, 2015, 06:30:51 PM
Quote from: ayozeve on October 16, 2015, 01:17:27 PM
Thanks a lot men!!

Some are... some aren't.  8)
Title: Re: [Discussion] Random topic
Post by: VladTepes on March 31, 2017, 01:13:54 PM
How can I get this to display in exactly the same manner / format as the 'Recent Topics' block?

(So just avatar, title, author, date/time)

I don't want any preview etc.

Thanks