Since my server configuration is relatively poor, using sql's rand() to randomize is definitely not possible. It is too slow, so I changed my mind and first built a table with two fields, one is the article ID and the other is the column ID. Then I use this table to perform regional randomization, so that I can randomly The high efficiency of the machine was achieved, but there was a problem. Using SQL was unreliable, so I wrote a PHP code. After testing, the homepage could be loaded within two seconds, but it was always random and bad, so I used my caching plug-in to fix the page.... This is all for the future.
<?php
$file_cache = "./news.json";
//缓存整个news表的数据
if(!file_exists($file_cache) || time()-filemtime($file_cache)>86400){
//查询分类
$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
foreach($file_arrs as $k=>$v){
//增加变量
for ($rani=1; $rani<=30; $rani++){
$rand_id.= $v[mt_rand(0,count($v)-1)].",";
}
$rand_id = rtrim($rand_id,",");
$rand_data[] = $rand_id;
$rand_id = "";
$rani=1;
}
?>I wrote it for two days, thought about it, and finally tested it for a long time before writing it like this. If the server configuration is still too small, you can try to split the cache file and call it. Of course, this requires you to open it again.
To use the code, first put the PHP code at the top of the home page template, and then you can directly call the universal tag.
[ecmsinfo]1,20,32,1,0,13,0,"id in ($rand_data[0])"[/ecmsinfo]
[ecmsinfo]2,20,32,1,0,13,0,"id in ($rand_data[1])"[/ecmsinfo]
[ecmsinfo]3,20,32,1,0,13,0,"id in ($rand_data[2])"[/ecmsinfo]For example, if you don't know PHP when calling like this, you should first understand the PHP syntax. You can ask me to improve the code!