It is very troublesome to find codes every time for website optimization, so I will give some backup here, including some customized functions. In fact, most of them are PHP codes.
Category 1: Highly efficient random calling code
<?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]
Randomly efficient evolved version:
This version of the code is mainly to facilitate the retrieval of random articles in the specified column. In order to facilitate template editing, it is not encapsulated into a function.
<?php
$file_cache = ECMS_PATH."news.json";
//缓存整个news表的数据
if(!file_exists($file_cache)){
//查询分类
$hm_class=$empire->query("select classid from {$dbtbpre}enewsclass");
while($hm_r=$empire->fetch($hm_class))
{
//查询分类下面的所有id
$hm_news=$empire->query("SELECT id FROM `{$dbtbpre}ecms_news` WHERE `classid` = {$hm_r[classid]}");
while($hm_nr=$empire->fetch($hm_news))
{
$news_data[$hm_r['classid']][] = $hm_nr[id];
}
}
file_put_contents($file_cache,json_encode($news_data));
}
//获取缓存数据表
$file_arrs = json_decode(file_get_contents($file_cache),true);
//根据数据表获取id,每个栏目随机分配文章id
foreach($file_arrs as $k=>$v){
$num = count($v)-1;
//增加变量
for ($rani=1; $rani<=30; $rani++){
$rand_id.= $v[mt_rand(0,$num)].",";
}
$rand_id = rtrim($rand_id,",");
$rand_data[] = $rand_id;
$rand_id = "";
$rani=1;
}
?>
Calling method:
<?php
$classid = $rand_data[$class_r[$GLOBALS[navclassid]][classid]-1];
?>
[ecmsinfo]0,20,32,0,3,14,0,"id in ($classid)"[/ecmsinfo]
Highly efficient random tag calling
<?php
$num=$empire->num("select tagid from {$dbtbpre}enewstags");
$randnum=100;
$randids='';
$randdh='';
for($i=1;$i<=$randnum;$i++)
{
$randids.=$randdh.rand(1,$num);
$randdh=',';
}
?>
[e:loop={"select tagname,tagid from phome_enewstags where tagid in ($randids) limit $randnum",32,24,0}]
<a target="_blank" href="/tag/<?=$bqr['tagid']?>/" title="<?=$bqr['tagname']?>"><?=$bqr['tagname']?></a> |
[/e:loop]
Randomly call tags. In fact, this can be modified to randomly call the information of all tables. You can modify it yourself.