function
get_rand_thread(
$num
= 20,
$cond
=
array
()){
global
$db
,
$forumlist_show
;
$tablepre
=
$db
->tablepre;
$fids
= arrlist_values(
$forumlist_show
,
'fid'
);
$cond
[
'fid'
] =
$fids
;
$condsql
= db_cond_to_sqladd(
$cond
);
$maxmincount
= db_sql_find_one(
"SELECT MAX(tid), MIN(tid), COUNT(*) FROM {$tablepre}thread {$condsql}"
);
list(
$max
,
$min
,
$count
) =
array_values
(
$maxmincount
);
if
(
$count
<=
$num
){
$sql
=
"SELECT * FROM {$tablepre}thread {$condsql}"
;
$threads
= db_sql_find(
$sql
);
shuffle(
$threads
);
}
else
{
$threads
=
array
();
do
{
$rndtids
=
array
();
for
(
$i
=
$num
;
$i
>0;
$i
--){
do
{
$rand
= rand(
$min
,
$max
);
}
while
(in_array(
$rand
,
$rndtids
));
$rndtids
[] =
$rand
;
}
$cond
[
'tid'
] =
$rndtids
;
$condsql
= db_cond_to_sqladd(
$cond
);
$sql
=
"SELECT * FROM {$tablepre}thread {$condsql}"
;
$rndthreads
= db_sql_find(
$sql
);
$threads
+=
$rndthreads
;
}
while
(
count
(
$threads
)<
$num
);
}
if
(
$threads
)
foreach
(
$threads
as
&
$thread
) thread_format(
$thread
);
return
$threads
;
}