1、在userfun.php中添加自定义函数:
2、模板中调用代码
- function zhann_get_keyid($tbname,$limit=15){
- global $empire,$dbtbpre;
- $maxid=$empire->gettotal("select max(id) as total from {$dbtbpre}ecms_{$tbname} ");
- $ids=array();
- $i=0;
- while($i<$limit){
- $key=rand(1,$maxid);
- $key=$empire->gettotal("select id as total from {$dbtbpre}ecms_{$tbname} where id<=$key order by id desc limit 1 ");
- if(in_array($key,$ids))continue;
- $ids[]=$key;
- $i++;
- }
- $rand=implode(',',$ids);
- return $rand;
- }
- <?php
- // 获取随机数字
- $randomNumbers = zhann_get_keyid('cidian');
- // 构建SQL查询语句
- $sql = $empire->query("select * from {$dbtbpre}ecms_cidian where id in ({$randomNumbers}) order by onclick limit 15");
- // 循环输出结果
- while($r = $empire->fetch($sql)){
- ?>
- <li><a href="<?=$r['titleurl']?>" title="<?=$r['title']?>"><?=$r['title']?></a></li>
- <?php
- }
- ?>