红穆笔记
首頁 程序筆記 帝國cms7.5內容隨機調取(高效率簡易版)
程序筆記 帝國cms

帝國cms7.5內容隨機調取(高效率簡易版)

帝国cms7.5内容随机调取(高效率简易版)

高效率函數

function user_randId($table='news'){
    global $dbtbpre,$empire;
    $filePath = ECMS_PATH . 'e/maxID'.md5($table).'.txt';  // 文件路徑
    if(file_exists($filePath)) {
      $fileCreateTime = filectime($filePath);  // 獲取文件創建時間(Unix 時間戳)
      $oneDayAgo = strtotime('-1 day');  // 當前時間減去一天的時間戳
    
      if ($fileCreateTime < $oneDayAgo) {
        unlink($filePath);
        $start = $empire->fetch1("select id from {$dbtbpre}ecms_{$table} where newstime <= ".time()." order by id ASC limit 1");
        $end = $empire->fetch1("select id from {$dbtbpre}ecms_{$table}  where newstime <= ".time()." order by id DESC limit 1");
        $index['min'] = $start['id'];
        $index['max'] = $end['id'];
        file_put_contents($filePath,json_encode($index));
        
      } else {
        $index = json_decode(file_get_contents($filePath),true);
      }
    } else {
      $start = $empire->fetch1("select id from {$dbtbpre}ecms_{$table}  where newstime <= ".time()." order by id ASC limit 1");
      $end = $empire->fetch1("select id from {$dbtbpre}ecms_{$table}  where newstime <= ".time()." order by id DESC limit 1");
      $index['min'] = $start['id'];
      $index['max'] = $end['id'];
      file_put_contents($filePath,json_encode($index));
    }
    $randnum=300; //隨機數量 
    $randids=''; 
    $randdh=''; 
    for($i=1;$i<=$randnum;$i++) 
    { 
    $randids.=$randdh.rand($index['min'],$index['max']); //1爲最小ID,100000爲最大ID 
    $randdh=',';
    } 
    return $randids;
}

支持不同數據表的查詢,默認爲news表,調用方式爲:

<? $randids = user_randId('news1');?>
[ecmsinfo]3,8,32,0,0,15,0,"id in ($randids)"[/ecmsinfo]

高效率代碼第二版:

第二版可謂是更加智能,採用了緩存的技術,巴適

🔒 隱藏內容:評論後查看

<ul>
<?php 
$filePath = ECMS_PATH . 'e/maxID.txt';  // 文件路徑

if(file_exists($filePath)) {
	$fileCreateTime = filectime($filePath);  // 獲取文件創建時間(Unix 時間戳)
	$oneDayAgo = strtotime('-1 day');  // 當前時間減去一天的時間戳

	if ($fileCreateTime < $oneDayAgo) {
		unlink($filePath);
		$start = $empire->fetch1("select id from {$dbtbpre}ecms_news where newstime <= ".time()." order by id ASC limit 1");
		$end = $empire->fetch1("select id from {$dbtbpre}ecms_news  where newstime <= ".time()." order by id DESC limit 1");
		$index['min'] = $start['id'];
		$index['max'] = $end['id'];
		file_put_contents($filePath,json_encode($index));
		
	} else {
		$index = json_decode(file_get_contents($filePath),true);
	}
} else {
	$start = $empire->fetch1("select id from {$dbtbpre}ecms_news  where newstime <= ".time()." order by id ASC limit 1");
	$end = $empire->fetch1("select id from {$dbtbpre}ecms_news  where newstime <= ".time()." order by id DESC limit 1");
	$index['min'] = $start['id'];
	$index['max'] = $end['id'];
	file_put_contents($filePath,json_encode($index));
}
$randnum=32; //隨機數量 
$randids=''; 
$randdh=''; 
for($i=1;$i<=$randnum;$i++) 
{ 
$randids.=$randdh.rand($index['min'],$index['max']); //1爲最小ID,100000爲最大ID 
$randdh=',';
} 
?>      

	[e:loop={0,10,3,0,"id in ($randids)"}] 
	<li>
		<a href="<?=$bqsr['titleurl']?>" title="<?=$bqr['title']?>" target="_blank"><?=$bqr['title']?></a>
	</li>
	[/e:loop]
	
</ul>

高效率調用第一版:

<?php 
$randnum=32; //隨機數量 
$randids=''; 
$randdh=''; 
for($i=1;$i<=$randnum;$i++) 
{ 
$randids.=$randdh.rand(1,100); //1爲最小ID,100000爲最大ID 
$randdh=',';
} 
?> [e:loop={1,32,0,0,"id in ($randids)"}]      
<li>
<a href="<?=$bqsr['titleurl']?>" title="<?=$bqr['title']?>"><?=$bqr['title']?></a>
</li>                  
[/e:loop]
微信赞赏

微信

支付宝赞赏

支付寶

✍️ 作者: 紅穆

網站管理員 · 感謝閱讀,更多精彩內容敬請關注

作者主頁 查看主頁 →

相關文章

帝国CMS数据库语句&amp;SQL语句格式

帝國CMS數據庫語句&amp;SQL語句格式 程序筆記 帝國cms

帝國CMS擴展SQL程序編寫介紹,帝國CMS數據庫語句&SQL語句格式基本範例:說明:下面例子都是以PHP文件放在系統根目錄爲基準。例1:連接MYSQL程序。(a.php)&lt;?php require('e/class/connect.php'); //引入數據庫配置文件和公共函數文件 requ…
👁 351
帝国CMS获取系统COOKIE变量函数getcvar()

帝國CMS獲取系統COOKIE變量函數getcvar() 程序筆記 帝國cms

取得系統COOKIE變量函數語法:getcvar($var,$ecms)說明:$var:爲變量名$ecms:0爲設置前臺COOKIE變量,1爲設置後臺COOKIE變量。此參數可省略,默認爲0。使用範例:getcvar('mlusername'),取得前臺登陸會員的用戶名getcvar('loginu…
👁 295
帝国CMS灵动标签调用字段大全

帝國CMS靈動標籤調用字段大全 程序筆記 帝國cms

將所有支持靈動標籤調用的字段收集歸類帝國CMS靈動標籤: [e:loop={欄目ID/專題ID,顯示條數,操作類型,只顯示有標題圖片,附加SQL條件,顯示排序}] 模板代碼內容 [/e:loop]調用時間:&lt;?=date('m-d',$bqr[newstime])?&gt; &lt;?=dat…
👁 3749

推薦閱讀

资源网站typecho001模板

資源網站typecho001模板 程序筆記 Typecho

typecho001模板板本請勿修改本模板文件夾名稱 文件夾名稱爲:typecho001 1.4修復文章頁面js輸出問題1.3修復未安裝插件時的報錯提示優化列表頁代碼輸出1.2修復評論功能增加自定義首頁標題1.1修復評論回覆不對稱功能添加首頁單獨的標題設置功能增加網站favicon.ico圖標增加一…
👁 198
typecho分页样式的修改

typecho分頁樣式的修改 程序筆記 Typecho

大人,時代變了!目前typecho最完美的解決方案了,因爲百度只能看到固定思維的代碼。實際生成的html代碼,乾淨到讓人窒息完全的自定義,包括給li元素加上class,給a元素加上class,給上一頁下一頁加上class,去除typecho自帶用於表示更多的li標籤。甚至裏面的內容我也能添加一些文字…
👁 517
typecho文章页评论样式修改

typecho文章頁評論樣式修改 程序筆記 Typecho

需求使用 typecho 進行模板開發的時候,默認評論樣式很難看。因此需要評論需要重新輸出或進行樣式設定。默認的評論模板文件路徑是:comments.php主要使用的相關變量&lt;?php $comments-&gt;gravatar('40', ''); ?&gt; //頭像,有兩個參數,大小、…
👁 388
Typecho 对数据库的增删改查操作方法 API

Typecho 對數據庫的增刪改查操作方法 API 程序筆記 Typecho

Typecho數據庫提供了非常易用的api,和原生SQL寫法沒有太大的差別,同時也很好地處理了SQL常見的安全問題,如sql注入。本文從實用角度出發,介紹了typecho操作數據庫的常用場景以及相關api用法。表創建和刪除在Typecho插件開發過程中,往往需要創建自己的表。上文提到Typecho_…
👁 552
PHP ob函数记录

PHP ob函數記錄 語言筆記 PHP

下面3個函數的用法 ob_get_contents(); ob_end_clean(); ob_start(); 可以藉助這幾個函數實現緩衝本地文件,以及執行本地腳本代碼。 使用ob_start()把輸出的代碼存到緩衝區,頁面不會顯示出來; 然後用ob_get_contents得到緩衝區的數據。 o…
👁 141
Typecho调用热门评论文章和调用最新文章的方法总结

Typecho調用熱門評論文章和調用最新文章的方法總結 程序筆記 Typecho

Typecho文章調用Typecho程序在設計主題的時候,側欄有些時候需要調用熱評文章或者最新文章。我們可以在指定的位置通過腳本直接調用。在這篇文章整理這個調用文章的方法,以後在有需要的模板中可以直接調用使用。其實設計主題就那麼回事,靜態模板搞定後,就是直接調用。最新文章調用 &lt;?php $t…
👁 295