高效率函數
function user_randId($table='news'){
global $dbtbpre,$empire;
$filePath = ECMS_PATH . 'e/maxID'.md5($table).'.txt'; // 文件路徑
if(file_exists($filePath)) {
$fileCreateTime = filectime($filePath); // 獲取文件創建時間(Unix 時間戳)
$oneDayAgo = strtotime('-1 day'); // 當前時間減去一天的時間戳
if ($fileCreateTime < $oneDayAgo) {
unlink($filePath);
$start = $empire->fetch1("select id from {$dbtbpre}ecms_{$table} where newstime <= ".time()." order by id ASC limit 1");
$end = $empire->fetch1("select id from {$dbtbpre}ecms_{$table} where newstime <= ".time()." order by id DESC limit 1");
$index['min'] = $start['id'];
$index['max'] = $end['id'];
file_put_contents($filePath,json_encode($index));
} else {
$index = json_decode(file_get_contents($filePath),true);
}
} else {
$start = $empire->fetch1("select id from {$dbtbpre}ecms_{$table} where newstime <= ".time()." order by id ASC limit 1");
$end = $empire->fetch1("select id from {$dbtbpre}ecms_{$table} where newstime <= ".time()." order by id DESC limit 1");
$index['min'] = $start['id'];
$index['max'] = $end['id'];
file_put_contents($filePath,json_encode($index));
}
$randnum=300; //隨機數量
$randids='';
$randdh='';
for($i=1;$i<=$randnum;$i++)
{
$randids.=$randdh.rand($index['min'],$index['max']); //1爲最小ID,100000爲最大ID
$randdh=',';
}
return $randids;
}支持不同數據表的查詢,默認爲news表,調用方式爲:
<? $randids = user_randId('news1');?>
[ecmsinfo]3,8,32,0,0,15,0,"id in ($randids)"[/ecmsinfo]高效率代碼第二版:
第二版可謂是更加智能,採用了緩存的技術,巴適
高效率調用第一版:
<?php
$randnum=32; //隨機數量
$randids='';
$randdh='';
for($i=1;$i<=$randnum;$i++)
{
$randids.=$randdh.rand(1,100); //1爲最小ID,100000爲最大ID
$randdh=',';
}
?> [e:loop={1,32,0,0,"id in ($randids)"}]
<li>
<a href="<?=$bqsr['titleurl']?>" title="<?=$bqr['title']?>"><?=$bqr['title']?></a>
</li>
[/e:loop]