帝国高效率随机调用功能扩展
时间:2023年09月17日
/来源:网络
/编辑:佚名
知识点总结
1.PHP代码
function ZQrand($classid , $num){
global $dbtbpre,$empire;
/* 检测json文件是否存在 */
$file_arrs = glob(ECMS_PATH."d/json/*.json");
$file_cache = $file_arrs[0];
//缓存整个news表的数据
if(!file_exists($file_cache) || time()-@filemtime($file_cache)>86400){
//创建文件夹
@mkdir(ECMS_PATH."d/json/",0777);
//查询分类
$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_nr[id];
$all_id[] = $hm_nr[id];
}
file_put_contents(ECMS_PATH . "d/json/" . $hm_r['classid'] . ".json",json_encode($news_data));
unset($news_data);
}
file_put_contents(ECMS_PATH . "d/json/" . allId . ".json",json_encode($all_id));
}
$num = (int) empty($num)?10:$num;
$classid = (int)$classid;
// 获取所有的文件
$hm_class=$empire->fetch1("select classid from {$dbtbpre}enewsclass where classid = {$classid}");
if(empty($hm_class[classid])){
// 随机调用
$file_arrs = json_decode(file_get_contents(ECMS_PATH."d/json/allId.json"),true);
}else{
//调用栏目
$file_arrs = json_decode(file_get_contents(ECMS_PATH."d/json/{$classid}.json"),true);
if(empty($file_arrs)){
$file_arrs = json_decode(file_get_contents(ECMS_PATH."d/json/allId.json"),true);
}
}
if(empty($file_arrs)){
return '';
}else{
for ($rani=1; $rani<=$num; $rani++){
$rand_id.= $file_arrs[mt_rand(0,count($file_arrs)-1)].",";
}
$rand_id = rtrim($rand_id,",");
$where = "id in ($rand_id)";
return $where;
}
}
2.js代码,主要用于关闭弹窗,定时的
setTimeout(function(){
window.close();
document.onload = window.close();
},1000);
2022.8.19 代码改进:
function ZQrand($classid , $num = 100){
global $dbtbpre,$empire;
ZQcread();
$num = (int) $num*3;
$classid = (int)$classid;
// 获取所有的文件
$hm_class=$empire->fetch1("select classid from {$dbtbpre}enewsclass where classid = {$classid} and `bclassid` != 0");
if(empty($hm_class[classid])){
// 随机调用
$file_arrs = json_decode(file_get_contents(ECMS_PATH."d/json/allId.json"),true);
}else{
//调用栏目
$file_arrs = json_decode(file_get_contents(ECMS_PATH."d/json/classId.json"),true);
$file_arrs = $file_arrs[$classid];
}
if(empty($file_arrs)){
return '';
}else{
for ($rani=0; $rani<$num; $rani++){
$rand_id.= $file_arrs[mt_rand(0,count($file_arrs)-1)].",";
}
$rand_id = rtrim($rand_id,",");
$where = "id in ($rand_id)";
return $where;
}
}
function ZQcread(){//创建缓存文件
global $dbtbpre,$empire;
/* 检测json文件是否存在 */
$class_cache = ECMS_PATH."d/json/classId.json";
$all_cache = ECMS_PATH."d/json/allId.json";
//缓存整个news表的数据
// time()-@filemtime($file_cache)>86400
if(!file_exists($class_cache) || !file_exists($all_cache)){
//创建文件夹
@mkdir(ECMS_PATH."d/json/",0777);
//查询分类
$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))
{
$class_data[$hm_r['classid']][] = $hm_nr[id];
$all_data[] = $hm_nr[id];
}
}
file_put_contents($class_cache,json_encode($class_data));
file_put_contents($all_cache,json_encode($all_data));
}
}
客户反馈高效率调用会出现bug,经过查证发现,主要是num参数限制了,已改进
10.5 根据自己的需求修改,备份于此
function ZQrand($classid , $num = 60){
global $dbtbpre,$empire;
/* 检测json文件是否存在 */
$file_arrs = glob(ECMS_PATH."d/json/*.json");
$file_cache = $file_arrs[0];
//缓存整个news表的数据
if(!file_exists($file_cache)){
//创建文件夹
@mkdir(ECMS_PATH."d/json/",0777);
//查询分类
$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_nr[id];
}
file_put_contents(ECMS_PATH . "d/json/" . $hm_r['classid'] . ".json",json_encode($news_data));
unset($news_data);
}
//查询所有
$more = 0;
$hm_news2=$empire->query("SELECT id FROM `{$dbtbpre}ecms_news`");
while($hm_nr2=$empire->fetch($hm_news2))
{
$all_id[] = $hm_nr2[id];
if($more>=1000){
file_put_contents(ECMS_PATH . "d/json/allId{$hm_nr2[id]}.json",json_encode($all_id));
unset($all_id);
$more = 0;
}
$more++;
}
}
$num = (int) empty($num)?10:$num;
$classid = (int)$classid;
// 获取所有的文件
$hm_class=$empire->fetch1("select classid from {$dbtbpre}enewsclass where classid = {$classid} and bclassid != 0");
if(empty($hm_class[classid])){
// 随机调用
$file_arrs = glob(ECMS_PATH."d/json/allId*.json");
$counts = count(glob(ECMS_PATH."d/json/allId*.json")) - 1;
// print_r($file_arrs);
// echo $counts;
$file_arrs = $file_arrs[mt_rand(0,$counts)];
// echo $file_arrs;
$file_arrs = json_decode(file_get_contents($file_arrs),true);
}else{
//调用栏目
$file_arrs = json_decode(file_get_contents(ECMS_PATH."d/json/{$classid}.json"),true);
}
1.PHP代码
function ZQrand($classid , $num){
global $dbtbpre,$empire;
/* 检测json文件是否存在 */
$file_arrs = glob(ECMS_PATH."d/json/*.json");
$file_cache = $file_arrs[0];
//缓存整个news表的数据
if(!file_exists($file_cache) || time()-@filemtime($file_cache)>86400){
//创建文件夹
@mkdir(ECMS_PATH."d/json/",0777);
//查询分类
$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_nr[id];
$all_id[] = $hm_nr[id];
}
file_put_contents(ECMS_PATH . "d/json/" . $hm_r['classid'] . ".json",json_encode($news_data));
unset($news_data);
}
file_put_contents(ECMS_PATH . "d/json/" . allId . ".json",json_encode($all_id));
}
$num = (int) empty($num)?10:$num;
$classid = (int)$classid;
// 获取所有的文件
$hm_class=$empire->fetch1("select classid from {$dbtbpre}enewsclass where classid = {$classid}");
if(empty($hm_class[classid])){
// 随机调用
$file_arrs = json_decode(file_get_contents(ECMS_PATH."d/json/allId.json"),true);
}else{
//调用栏目
$file_arrs = json_decode(file_get_contents(ECMS_PATH."d/json/{$classid}.json"),true);
if(empty($file_arrs)){
$file_arrs = json_decode(file_get_contents(ECMS_PATH."d/json/allId.json"),true);
}
}
if(empty($file_arrs)){
return '';
}else{
for ($rani=1; $rani<=$num; $rani++){
$rand_id.= $file_arrs[mt_rand(0,count($file_arrs)-1)].",";
}
$rand_id = rtrim($rand_id,",");
$where = "id in ($rand_id)";
return $where;
}
}
2.js代码,主要用于关闭弹窗,定时的
setTimeout(function(){
window.close();
document.onload = window.close();
},1000);
2022.8.19 代码改进:
function ZQrand($classid , $num = 100){
global $dbtbpre,$empire;
ZQcread();
$num = (int) $num*3;
$classid = (int)$classid;
// 获取所有的文件
$hm_class=$empire->fetch1("select classid from {$dbtbpre}enewsclass where classid = {$classid} and `bclassid` != 0");
if(empty($hm_class[classid])){
// 随机调用
$file_arrs = json_decode(file_get_contents(ECMS_PATH."d/json/allId.json"),true);
}else{
//调用栏目
$file_arrs = json_decode(file_get_contents(ECMS_PATH."d/json/classId.json"),true);
$file_arrs = $file_arrs[$classid];
}
if(empty($file_arrs)){
return '';
}else{
for ($rani=0; $rani<$num; $rani++){
$rand_id.= $file_arrs[mt_rand(0,count($file_arrs)-1)].",";
}
$rand_id = rtrim($rand_id,",");
$where = "id in ($rand_id)";
return $where;
}
}
function ZQcread(){//创建缓存文件
global $dbtbpre,$empire;
/* 检测json文件是否存在 */
$class_cache = ECMS_PATH."d/json/classId.json";
$all_cache = ECMS_PATH."d/json/allId.json";
//缓存整个news表的数据
// time()-@filemtime($file_cache)>86400
if(!file_exists($class_cache) || !file_exists($all_cache)){
//创建文件夹
@mkdir(ECMS_PATH."d/json/",0777);
//查询分类
$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))
{
$class_data[$hm_r['classid']][] = $hm_nr[id];
$all_data[] = $hm_nr[id];
}
}
file_put_contents($class_cache,json_encode($class_data));
file_put_contents($all_cache,json_encode($all_data));
}
}
客户反馈高效率调用会出现bug,经过查证发现,主要是num参数限制了,已改进
10.5 根据自己的需求修改,备份于此
function ZQrand($classid , $num = 60){
global $dbtbpre,$empire;
/* 检测json文件是否存在 */
$file_arrs = glob(ECMS_PATH."d/json/*.json");
$file_cache = $file_arrs[0];
//缓存整个news表的数据
if(!file_exists($file_cache)){
//创建文件夹
@mkdir(ECMS_PATH."d/json/",0777);
//查询分类
$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_nr[id];
}
file_put_contents(ECMS_PATH . "d/json/" . $hm_r['classid'] . ".json",json_encode($news_data));
unset($news_data);
}
//查询所有
$more = 0;
$hm_news2=$empire->query("SELECT id FROM `{$dbtbpre}ecms_news`");
while($hm_nr2=$empire->fetch($hm_news2))
{
$all_id[] = $hm_nr2[id];
if($more>=1000){
file_put_contents(ECMS_PATH . "d/json/allId{$hm_nr2[id]}.json",json_encode($all_id));
unset($all_id);
$more = 0;
}
$more++;
}
}
$num = (int) empty($num)?10:$num;
$classid = (int)$classid;
// 获取所有的文件
$hm_class=$empire->fetch1("select classid from {$dbtbpre}enewsclass where classid = {$classid} and bclassid != 0");
if(empty($hm_class[classid])){
// 随机调用
$file_arrs = glob(ECMS_PATH."d/json/allId*.json");
$counts = count(glob(ECMS_PATH."d/json/allId*.json")) - 1;
// print_r($file_arrs);
// echo $counts;
$file_arrs = $file_arrs[mt_rand(0,$counts)];
// echo $file_arrs;
$file_arrs = json_decode(file_get_contents($file_arrs),true);
}else{
//调用栏目
$file_arrs = json_decode(file_get_contents(ECMS_PATH."d/json/{$classid}.json"),true);
}
新闻资讯 更多
- 【帝国cms教程】帝国CMS模板变量$GLOBALS[navclassid]用法分析04-03
- 【帝国cms教程】鲜为人知帝国CMS内容页调用上一篇和下一篇的精华方法汇总04-03
- 【帝国cms教程】怎么快速找出帝国CMS数据库配置文件路径及迁移网站后修改技巧!04-03
- 【帝国cms教程】帝国CMS模板$GLOBALS[navclassid]用法详解04-03
- 【帝国cms教程】帝国cms 7.5版列表页分页样式修改笔记04-02
- 【帝国cms教程】解决帝国CMS搜索页面模板不支持灵动标签和万能标签的方法04-02
- 【帝国cms教程】帝国CMS只备份栏目和模板的方法04-02
- 【帝国cms教程】帝国CMS怎样删除清空数据库记录?04-02
热门文章
- 178Moban源码谈谈免费源码与收费源码的区别
- 2帝国CMS忘记后台登陆用户名、密码、认证码的解决方法
- 3帝国CMS(EmpireCMS) v7.5后台任意代码执行漏洞及具体修复方法
- 4帝国CMS和WordPress 哪个好?哪个适合建站?
- 5如何解决Discuz的密码错误次数过多请15分钟后登陆的问题
- 6帝国cms灵动标签取得内容和栏目链接地址
- 7emlog pro 注册码“开心”教程(如果有一天,emlog官方版 或者 emlog免费版 跑路了,那用户怎么办?)
- 8织梦CMS在nginx下设置伪静态方法(附nginx伪静态规则)
- 9帝国cms后台登录出现”您还未登录”怎么解决?
- 10帝国cms7.5忘记登录密码 和忘记登录认证码以及多次登录 失败被锁定解决办法