最新公告
  • 欢迎您光临 我爱模板网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境! 立即加入钻石VIP
  • 自用帝国CMS高效随机调用

    正文概述 管理员   2025-09-04   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);
        }
        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;
        }
    }

    前端调用代码

    <?php
    $ranid  = ZQrand($navinfor[classid],10);
    ?>
    [e:loop={0,10,3,0,"$ranid"}]  
    <?=$bqr[title]?>
     [/e:loop]


    我爱模板网 » 自用帝国CMS高效随机调用

    发表评论

    如需帝国cms功能定制以及二次开发请联系我们

    联系作者
    script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?587cc1e5c052b5b0ce99533beff13c96"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })();

    请选择支付方式

    ×
    支付宝支付
    余额支付
    ×
    微信扫码支付 0 元