帝国CMS实现高效随机文章调用的2种方法
看不少人喜欢用ORDER BY RAND()随机调用,随机调用是最耗资源的一种调用方式,信息数在1万以上随机调用就开始影响速度。使用时建议慎之又慎。如果确实要使用,可以用下面思路:第一种方法<?php$num=10;$total=$empire->getto……
看不少人喜欢用ORDER BY RAND()随机调用,随机调用是最耗资源的一种调用方式,信息数在1万以上随机调用就开始影响速度。使用时建议慎之又慎。如果确实要使用,可以用下面思路:
第一种方法
<?php$num=10;$total=$empire->gettotal("selectcount(*)astotalfrom{$dbtbpre}ecms_{$class_r[4]['tbname']}whereclassid=4");$start=$total-$num;$table=$class_r[4]['tbname'];if($start<1){$start=0;}else{$start=mt_rand(0,$start);}?>[e:loop={'select*from'.$dbtbpre.'ecms_'.$table.'limit'.$start.','.$num,10,24,0}]<li><ahref="<?=$bqsr['titleurl']?>"target="_blank"><?=$bqr['title']?></a></li>[/e:loop]
第二种方法:
先用php随机出信息ID,然后SQL调用用id in (php随机的ID列表)
调用举例:
<?php$randnum=10;//随机数量$randids='';$randdh='';for($i=1;$i<=$randnum;$i++){$randids.=$randdh.rand(1,100000);//1为最小ID,100000为最大ID$randdh=',';}?>[e:loop={栏目ID,显示条数,操作类型,只显示有标题图片,"idin($randids)"}]模板代码内容[/e:loop]标签高效帝国种方法