本文實例講述了smarty循環嵌套用法。分享給大家參考,具體如下:
test3.php:
<?php require "main.php"; $forum = array( array("category_id" => 1, "category_name" => "公告区", "topic" => array( array("topic_id" => 1, "topic_name" => "站务公告") ) ), array("category_id" => 2, "category_name" => "文学专区", "topic" => array( array("topic_id" => 2, "topic_name" => "好书介绍"), array("topic_id" => 3, "topic_name" => "奇文共赏") ) ), array("category_id" => 3, "category_name" => "电脑专区", "topic" => array( array("topic_id" => 4, "topic_name" => "硬件周边"), array("topic_id" => 5, "topic_name" => "软件讨论") ) ) ); $tpl->assign("forum", $forum); $tpl->display("test3.htm"); ?>
樣版的寫法如下:
templates/test3.htm:
<html> <head> <title>循环嵌套测试</title> </head> <body> <table width="200" border="0" align="center" cellpadding="3" cellspacing="0"> <{section name=sec1 loop=$forum}> <tr> <td colspan="2"><{$forum[sec1].category_name}></td> </tr> <{section name=sec2 loop=$forum[sec1].topic}> <tr> <td width="25"> </td> <td width="164"><{$forum[sec1].topic[sec2].topic_name}></td> </tr> <{/section}> <{/section}> </table> </body> </html>
test2.php:
<?php require_once('./include/db_fns.php'); include_once("./Smarty/libs/Smarty.class.php"); //包含Smarty类文件 $smarty = new Smarty(); //建立Smarty实例对象$Smarty $smarty->template_dir = "./templates/dedecms";//设置模板目录 $smarty->compile_dir = "templates/templates_c"; //设置编译目录 $smarty->assign("template_url", "./"); $smarty->assign("$site_url", "http://www.php.net/"); $smarty->assign("$site_name", "文章管理系统"); $smarty->left_delimiter = "<{"; //设置左边界符 $smarty->right_delimiter = "}>"; //设置右边界符 $db_conn = db_connect(); $query = "SELECT cat_ID,cat_name FROM categories ORDER BY cat_ID DESC"; $result = mysql_query($query); $i = 5; while(($row = mysql_fetch_array($result)) && $i > 0) { $query2="SELECT ID, post_title, post_date FROM post WHERE post.post_category =$row[cat_ID] AND post_status <> 'unpbulish' ORDER BY post_date DESC"; $result2=mysql_query($query2); $i = 5; while(($row2 = mysql_fetch_array($result2)) && $i > 0) { $row2[post_date]=date('m-d',strtotime($row2[post_date])); $category = array("cat_ID"=>"$row[cat_ID]","cat_name"=>"$row[cat_name]", "post"=>array("ID"=>"$row2[ID]", "post_title"=>"$row2[post_title]" , "post_category"=>"$row2[post_category]" , "post_date"=>"$row2[post_date]")); $i--; } } $smarty->assign("forum", $category); $smarty->display("test2.htm"); ?>
test2.htm:
<html> <head> <title>嵌套循环测试</title> </head> <body> <table width="200" border="0" align="center" cellpadding="3" cellspacing="0"> <{section name=sec1 loop=$forum}> <tr> <td colspan="2"><{$forum[sec1].cat_id}></td> </tr> <{section name=sec2 loop=$forum[sec1].post}> <tr> <td width="25"> </td> <td width="164"><{$forum[sec1].post[sec2].post_title}></td> </tr> <{/section}> <{/section}> </table> </body> </html>
test4.php:
<?php require "main.php"; $my_array = array( array("value" => "0"), array("value" => "1"), array("value" => "2"), array("value" => "3"), array("value" => "4"), array("value" => "5"), array("value" => "6"), array("value" => "7"), array("value" => "8"), array("value" => "9")); $tpl->assign("my_array", $my_array); $tpl->display('test4.htm'); ?>
模版的寫法如下:
templates/test4.htm:
<html> <head> <title>横向重复表格测试</title> </head> <body> <table width="500" border="1" cellspacing="0" cellpadding="3"> <tr> <{section name=sec1 loop=$my_array}> <td><{$my_array[sec1].value}></td> <{if $smarty.section.sec1.rownum is div by 2}> </tr> <tr> <{/if}> <{/section}> </tr> </table> </body> </html>
重點在於$smarty.section.sec1.rownum 這個Smarty 變量,在section 循環中這個變數會取得從1 開始的索引值,所以當rownum 能被2 除盡時,就輸出
運算子有以下這些:
eq、ne、neq、gt、lt、lte、le、gte、ge、is even、is odd、is not even、is not odd、not、mod、div by、even by、odd by
範例:
<!--{if $bigsize ge '650'}--> <img src="/static/imghwm/default1.png" data-src="photo/<!--{$photo}-- alt="smarty循環巢狀用法範例分析_php實例" >" class="lazy" border="0" width="650" class="product_photo" /> <!--{else}--> <img src="/static/imghwm/default1.png" data-src="photo/<!--{$photo}-- alt="smarty循環巢狀用法範例分析_php實例" >" class="lazy" border="0" class="product_photo" /> <!--{/if}-->
以前不常用smarty,這兩天有個朋友的網站要改;順手用了一下,還是挺有意思的。
關於capture 的說明:
capture函數的作用是收集模板輸出的資料到一個變數裡,而不是把它們輸出到頁面.
任何在 {capture name="foo"}和{/capture}之間的資料都被收到了由函數的名稱屬性指定的變數裡($foo).
收集的資訊可以用在特殊變數$smarty裡.
例如capture.foo就收集了上述資料.如果函數沒有名字屬性,將使用"default".
每個{capture}都必須對應{/capture},也不能嵌套使用capture函數.
更多關於Smarty相關內容有興趣的讀者可查看本站專題:《smarty模板入門基礎教程》、《PHP模板技術總結》、《PHP基於pdo操作資料庫技巧總結》、《PHP運算與運算符用法總結》、《PHP網路程式設計技巧總結》、《PHP基本文法入門教學》、《php物件導向程式設計入門教學》、《php字串(string)用法總結》、《php+mysql資料庫操作入門教學》及《 php常見資料庫操作技巧總表》
希望本文所述對大家基於smarty模板的PHP程式設計有所幫助。

絕對會話超時從會話創建時開始計時,閒置會話超時則從用戶無操作時開始計時。絕對會話超時適用於需要嚴格控制會話生命週期的場景,如金融應用;閒置會話超時適合希望用戶長時間保持會話活躍的應用,如社交媒體。

服務器會話失效可以通過以下步驟解決:1.檢查服務器配置,確保會話設置正確。 2.驗證客戶端cookies,確認瀏覽器支持並正確發送。 3.檢查會話存儲服務,如Redis,確保其正常運行。 4.審查應用代碼,確保會話邏輯正確。通過這些步驟,可以有效診斷和修復會話問題,提升用戶體驗。

session_start()iscucialinphpformanagingusersessions.1)ItInitiateSanewsessionifnoneexists,2)resumesanexistingsessions,and3)setsasesessionCookieforContinuityActinuityAccontinuityAcconActInityAcconActInityAcconAccRequests,EnablingApplicationsApplicationsLikeUseAppericationLikeUseAthenticationalticationaltication and PersersonalizedContentent。

設置httponly標誌對會話cookie至關重要,因為它能有效防止XSS攻擊,保護用戶會話信息。具體來說,1)httponly標誌阻止JavaScript訪問cookie,2)在PHP和Flask中可以通過setcookie和make_response設置該標誌,3)儘管不能防範所有攻擊,但應作為整體安全策略的一部分。

phpsessions solvathepromblymaintainingStateAcrossMultipleHttpRequestsbyStoringDataTaNthEserVerAndAssociatingItwithaIniquesestionId.1)他們儲存了AtoredAtaserver side,通常是Infilesordatabases,InseasessessionIdStoreDistordStoredStoredStoredStoredStoredStoredStoreDoreToreTeReTrestaa.2)

tostartaphpsession,usesesses_start()attheScript'Sbeginning.1)placeitbeforeanyOutputtosetThesessionCookie.2)useSessionsforuserDatalikeloginstatusorshoppingcarts.3)regenerateSessiveIdStopreventFentfixationAttacks.s.4)考慮使用AttActAcks.s.s.4)

會話再生是指在用戶進行敏感操作時生成新會話ID並使舊ID失效,以防會話固定攻擊。實現步驟包括:1.檢測敏感操作,2.生成新會話ID,3.銷毀舊會話ID,4.更新用戶端會話信息。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SublimeText3漢化版
中文版,非常好用

記事本++7.3.1
好用且免費的程式碼編輯器

Dreamweaver Mac版
視覺化網頁開發工具