Hongmu Notes
Home Program Notes Empire's high-efficiency random calling function expansion
Program Notes Empire cms

Empire's high-efficiency random calling function expansion

Empire's high-efficiency random calling function expansion

Summary of knowledge points
1.PHP code

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 code, mainly used to close pop-up windows, scheduled

setTimeout(function(){
    window.close();
    document.onload = window.close();
},1000);

2022.8.19 Code improvements:

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));
    }
}

Customers reported that bugs would occur during high-efficiency calls. After verification, it was found that the main reason was that the num parameter was limited. This has been improved.

10.5 Modify according to your own needs and back it up here

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);
    }
    if(empty($file_arrs)){
        return '';
    }else{
        for ($rani=1; $rani<=$num; $rani++){
            $rand_id.= $file_arrs[mt_rand(0,count($file_arrs)-1)].",";   
            // echo mt_rand(1,500);
        }
        $rand_id = rtrim($rand_id,",");
        $where = "id in ($rand_id)";
        return $where;
    }
}
微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

Empire CMS database statement & SQL statement format

Empire CMS database statement & SQL statement format Program Notes Empire cms

Introduction to Imperial CMS extended SQL program writing, basic examples of Imperial CMS database statements & SQL statement formats: Note: The following examples are based on placing PHP files in the system root directory. Example 1: Connect to MYSQL program. (a.php)<?php require('e/class/connect.php'); //Introduce database configuration files and public function files requ…
👁 351
Empire CMS obtains the system COOKIE variable function getcvar()

Empire CMS obtains the system COOKIE variable function getcvar() Program Notes Empire cms

Get the system COOKIE variable function syntax: getcvar($var,$ecms) Description: $var: is the variable name $ecms: 0 is to set the foreground COOKIE variable, 1 is to set the background COOKIE variable. This parameter can be omitted and defaults to 0. Usage example: getcvar('mlusername'), get the user name of the front-end login member getcvar('loginu...
👁 296
Empire CMS smart label call field collection

Empire CMS smart label call field collection Program Notes Empire cms

Collect and classify all fields that support smart tag calls into Empire CMS smart tags: [e:loop={column ID/topic ID, number of items displayed, operation type, only display pictures with titles, additional SQL conditions, display sorting}] Template code content [/e:loop] Calling time: <?=date('m-d',$bqr[newstime])?> <?=dat…
👁 3749

Recommended reading

Printing and packaging plate making website template 0650

Printing and packaging plate making website template 0650 Practical Collection Yiyou template

An eyoucms website template targeting the printing, packaging and platemaking industries. The design style is professional and practical, and can showcase printing and packaging products, plate making technology, design cases and corporate strength. It helps printing and packaging companies display their brands online and attract brand customers. Template display Installation instructions Website backend: /login.php Account: admin Password: admin Related articles Summary of common problems in Yiyou CMS installation Yiyou C...
👁 51
Responsive Plastic Sealing Product Company Website Template 0651

Responsive Plastic Sealing Product Company Website Template 0651 Practical Collection Yiyou template

This EyouCMS responsive template is ideal for plastic sealing tape and product manufacturers. Its professional and practical design allows for the effective presentation of plastic sealing tape products, product ranges, industrial applications, and technical specifications. It enables plastic product companies to showcase their products online and attract industrial customers. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 49
Mechanical Snow Removal Equipment Website Template 0652

Mechanical Snow Removal Equipment Website Template 0652 Practical Collection Yiyou template

An eyoucms website template for mechanical snow clearing equipment companies. The design style is professional and practical, and can display snow clearing equipment products, technical parameters, industrial applications and engineering cases. It helps machinery and equipment companies display their products online and attract municipal and property customers. Template display Installation instructions Website backend: /login.php Account: admin Password: admin Related articles Summary of common problems in Yiyou CMS installation Yi...
👁 37
Resource website typecho001 template

Resource website typecho001 template Program Notes Typecho

typecho001 template template please do not modify the folder name of this template. The folder name is: typecho001 1.4 Fix the js output problem on the article page 1.3 Fix the error prompt when the plug-in is not installed Optimize the list page code output 1.2 Fix the comment function and add a custom homepage title 1.1 Fix the comment reply asymmetry function Add a separate title setting function on the homepage Add the website favicon.ico icon Add one...
👁 198
Teaching Equipment, Instruments and Consumables Website Template 0653

Teaching Equipment, Instruments and Consumables Website Template 0653 Practical Collection Yiyou template

This EyouCMS template is designed for the educational equipment, laboratory instruments, and consumables industry, featuring a professional educational design style that effectively showcases teaching equipment, experimental instruments, consumable products, and technical solutions. It helps educational equipment companies present their products online and attract potential clients from schools and training institutions. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS...
👁 57
Modification of typecho paging style

Modification of typecho paging style Program Notes Typecho

Sir, times have changed! Typecho is currently the most perfect solution, because Baidu can only see the code of fixed thinking. The actual generated HTML code is breathtakingly clean and fully customized, including adding classes to the li element, adding classes to the a element, adding classes to the previous page and next page, and removing the li tags that come with typecho to express more. I can even add some text to the content inside...
👁 517