After a period of using SMF, I coded this solution myself, I want to share to all. Code more as you want but please don't say this core is your own (better is say nothing :D). You can paste below functions to end of SSI.php and call it from your template as 3 last lines below:
function diff( $no , $mang )
{
$mlogic = true;
for( $i = 0 ; $i < count( $mang ); $i++ )
{
if( $no == $mang[ $i ] )
{
$mlogic = false;
}
}
return $mlogic;
}
function RandomTopics( $max_no )
{
global $smcFunc;
$tls = array();
$topicList = $smcFunc[ 'db_query' ]( '', '
SELECT id_first_msg
FROM {db_prefix}topics
WHERE locked = 0
AND approved = 1
ORDER BY RAND()'
);
while( $row = $smcFunc[ 'db_fetch_assoc' ]( $topicList ) )
{ $tls[] = $row[ 'id_first_msg' ]; }
$smcFunc[ 'db_free_result' ]( $topicList );
$c = 0; $rno = array();
while( $c < $max_no )
{
$ran = rand( 0, count( $tls ) - 1 );
if( $c == 0 )
{
$rno[ $c ] = $ran;
$c++;
}
else
if( diff( $ran, $rno ) )
{
$rno[ $c ] = $ran;
$c++;
}
}
for( $c = 0; $c < $max_no; $c++ )
{
ssi_fetchPosts( $tls[ $rno[ $c ] ] );
}
}
//Output:
include('SSI.php');
echo '<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8" />
</head>
<body>
';
RandomTopics( 5 );
echo '
</body>
</html>';
I have intended to remove above codes due to community don't pay attention to, but then I have left this with a simpliest codes for your idea. Good bye!
It's completely fine, some tweaks on how it looks might be in order but the way you utilize ssi_fetchPosts() is great
How can this be done when someone wishes to have certain boards be randomized and NOT all topics? For example, let says I have 10 boards and only wish to have 1 board or 5 boards randomized. What's it gonna be in the code for that?
Thanks very much.!
This may be done by change some codes in query data.
Quote from: Tomy Tran © 2012 on December 20, 2011, 03:30:57 AM
This may be done by change some codes in query data.
Can you help? because I m not a coder. I have no idea where to start.
I am not a coder too, just see codes in SSI.php file and imitate, try and test. See what I added and changed as Bold below:
...
function RandomTopics( $max_no, $ex_boards = null, $ex_topics = null )
{
global $smcFunc;
$tls = array();
// Excluding certain boards...
$ex_boards = empty( $ex_boards ) ? array() : (is_array( $ex_boards ) ? $ex_boards : array( $ex_boards ));
// Excluding certain topics...
$ex_topics = empty( $ex_topics ) ? array() : (is_array( $ex_topics ) ? $ex_topics : array( $ex_topics ));
$topicList = $smcFunc[ 'db_query' ]( '', '
SELECT id_first_msg
FROM {db_prefix}topics
WHERE locked = 0
AND approved = {int:is_approved}
' . (empty( $ex_boards ) ? '' : '
AND id_board NOT IN ({array_int:ex_boards})') . '
' . (empty( $ex_topics ) ? '' : '
AND id_topic NOT IN ({array_int:ex_topics})') . '
ORDER BY RAND()',
array(
'ex_boards' => empty( $ex_boards ) ? '' : $ex_boards,
'ex_topics' => empty( $ex_topics ) ? '' : $ex_topics,
'is_approved' => 1,
)
);
while( $row = $smcFunc[ 'db_fetch_assoc' ]( $topicList ) )
{ $tls[] = $row[ 'id_first_msg' ]; }
$smcFunc[ 'db_free_result' ]( $topicList );
...
}
//Output:
...
//Random 05 topics, exclude board ID: 13 % 57, exclude topic ID: 1 & 2
RandomTopics( 5, array( 57, 13 ), array( 1, 2 ) );
/*
//Random 09 topics, exclude board ID: 13, exclude topic ID: 2
RandomTopics( 9, array( 13 ), array( 2 ) );
//Random 05 topics, no any board or topic is excluded
RandomTopics( 5, null, null );
*/
...