国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

smarty簡單應用實例

2024-05-04 23:40:02
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了smarty簡單應用實例,簡單介紹了smarty中數據庫查詢、數組遍歷及調用模板顯示數據的相關技巧,簡單易懂,具有一定參考借鑒價值,需要的朋友可以參考下

本文講述了smarty簡單應用實例。分享給大家供大家參考,具體如下:

 

 
  1. <?php 
  2. require 'smarty/libs/Smarty.class.php'
  3. $smarty = new Smarty; 
  4. $smarty->template_dir="smarty/templates/templates"
  5. $smarty->compile_dir="smarty/templates/templates_c"
  6. $smarty->config_dir="smarty/templates/config"
  7. $smarty->cache_dir="smarty/templates/cache";  
  8. $hello = "Hello World!"
  9. $smarty->compile_check = true
  10. //$smarty->debugging = true; 
  11. $smarty->debugging = false
  12. $smarty->caching=true
  13. $conn=mysql_connect("localhost""root","root"); //打開MySQL服務器連接 
  14. mysql_select_db("lava_guess"); //鏈接數據庫 
  15. mysql_query("set names utf8"); //解決中文亂碼問題 
  16. $sql_list="Select * from t_sys_msg Order by id desc limit 0,10"
  17. $result_list=mysql_query($sql_list); //執行sql語句,返回結果 
  18. //把記錄集轉換為數組 
  19. while($rs_list=mysql_fetch_array($result_list))  
  20. $msg_array[]=$rs_list; 
  21. $array[] = array("newsID"=>1, "newsTitle"=>"第1條新聞");  
  22. $array[] = array("newsID"=>2, "newsTitle"=>"第2條新聞");  
  23. $array[] = array("newsID"=>3, "newsTitle"=>"第3條新聞");  
  24. $array[] = array("newsID"=>4, "newsTitle"=>"第4條新聞");  
  25. $array[] = array("newsID"=>5, "newsTitle"=>"第5條新聞");  
  26. $array[] = array("newsID"=>6, "newsTitle"=>"第6條新聞"); 
  27. $smarty->assign("newsArray", $array); 
  28. $smarty->assign("hello",$hello); 
  29. //賦值 
  30. $smarty->assign("msglist",$msg_array); 
  31. $smarty->display('index.tpl'); 
  32. ?> 

模板文件:index.tpl

 

 
  1. <html> 
  2. <head> 
  3. <metahttp-equiv="Content-Type" content="text/html;charset=utf-8"
  4. <title>Smarty</title></head> 
  5. <body> 
  6. <hr> 
  7. {$hello} 
  8. <hr> 
  9. {section name=loop loop=$msglist} 
  10. id: {$msglist[loop].id}--{$msglist[loop].title}----{$msglist[loop].receiver}<br> 
  11. {/section} 
  12. <hr> 
  13. {section name=loop loop=$newsArray} 
  14. 新聞編號:{$newsArray[loop].newsID}<br> 
  15. 新聞標題:{$newsArray[loop].newsTitle}<br><hr> 
  16. {sectionelse} 
  17. 對不起,沒有任何新聞輸入! 
  18. {/section} 

希望本文所述對大家基于smarty模板的php程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 泽库县| 郁南县| 乌鲁木齐县| 高台县| 普兰县| 仲巴县| 永宁县| 松阳县| 炉霍县| 万全县| 乌兰浩特市| 松桃| 万安县| 滨州市| 阿拉善左旗| 淄博市| 平和县| 平谷区| 满洲里市| 永福县| 白城市| 株洲县| 泗洪县| 陆丰市| 中阳县| 兴国县| 武汉市| 都江堰市| 蒙城县| 怀远县| 周至县| 深泽县| 固安县| 广灵县| 儋州市| 霍城县| 沾化县| 惠安县| 平度市| 清水河县| 大连市|