帝国cms7.5内容随机调取(高效率简易版)

高效率函数

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]
© 版权声明
THE END
喜欢就支持一下吧
点赞13
评论 共9条

请登录后发表评论