知識點總結
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);
}
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;
}
}