PHP訪問MySql資料庫 中級篇 Smarty技術

來源:互聯網
上載者:User

閱讀本文之前,推薦先參閱《PHP訪問MySql資料庫 初級篇》。

Smarty是一個使用PHP語言寫出來的模板引擎,是目前業界最著名的PHP模板引擎之一。它分離了邏輯代碼和外在的內容,將原本與HTML代碼混雜在一起PHP代碼進行了分離。從而使PHP程式員同網站的前端程式員可以達到良好的分工——PHP程式員改變程式的邏輯內容不會影響到前端人員的頁面設計,前端人員重新修改頁面的樣式也不會影響到程式的程式邏輯,這使得多人合作的項目變得尤為輕鬆和易於管理維護。正因為Smarty有這麼多的優點,所以國內各大公司在網站編程時均採用這種編程方法。Smarty的手冊可以訪問http://www.smarty.net/docs/en/index.tpl。

 

下面是Smarty程式的一個小範例,功能上與初級篇相同——從MySql的test資料庫中的t_student讀取資料然後顯示。程式共分為5個檔案,分別為smarty2.php、smarty2.html、smarty2_head.php、smarty2.js和smarty2.css。此外程式要引用Smarty和JQuery的庫檔案。

1.smarty2_head.php檔案

<?php// by MoreWindows( http://blog.csdn.net/MoreWindows )define(DB_HOST, 'localhost');define(DB_USER, 'root');define(DB_PASS, '111111');define(DB_DATABASENAME, 'test');define(DB_TABLENAME, 't_student');$dbcolarray = array('id', 'name', 'age');?>

2.smarty2.php檔案

<?php// by MoreWindows( http://blog.csdn.net/MoreWindows )header("Content-Type: text/html; charset=utf-8");require('../../smart_libs/Smarty.class.php');require_once('smarty2_head.php');date_default_timezone_set("PRC");//mysql_connect$conn = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("connect failed" . mysql_error());mysql_select_db(DB_DATABASENAME, $conn);//表中記錄條數$sql = sprintf("select count(*) from %s", DB_TABLENAME);$result = mysql_query($sql, $conn);if ($result){$dbcount = mysql_fetch_row($result);$tpl_db_count = $dbcount[0];}else{die("query failed");}//表頭$tpl_db_coltitle = $dbcolarray;//表中的內容$tpl_db_rows = array();$sql = sprintf("select %s from %s", implode(",",$dbcolarray), DB_TABLENAME);$result = mysql_query($sql, $conn);while ($row = mysql_fetch_array($result, MYSQL_ASSOC))//與$row=mysql_fetch_assoc($result)等價$tpl_db_rows[] = $row;mysql_free_result($result);mysql_close($conn);$tpl = new Smarty;$tpl->assign('db_count', $tpl_db_count);$tpl->assign('db_coltitle', $tpl_db_coltitle);$tpl->assign('db_rows', $tpl_db_rows);$tpl->display('smarty2.html');?>

3.smarty2.html檔案

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><link href="smarty2.css" rel="stylesheet" type="text/css" media="all" /><script type="text/javascript" src="../jquery-1.7.min.js"></script><script type="text/javascript" src="smarty2.js"></script><title>{$smarty.const.DB_TABLENAME}</title></head><body><h1>表中有{$db_count}條記錄</h1><table border="1" align="center" cellpadding="10" cellspacing="2" bordercolor="#ffaaoo">{foreach $db_coltitle as $col}    <th>{$col}</th>{/foreach}{foreach $db_rows as $dbrow}    <tr>    {foreach $dbrow as $k=>$val}        <td>{$val}</td>    {/foreach}    </tr>{/foreach}</table></body></html>

4.smarty2.js檔案

$(document).ready(function(){    //用CSS控制奇偶行的顏色    $("table tr:odd").css("background-color", "#e6e6fa");    $("table tr:even").css("background-color", "#fff0fa");});  

5.smarty2.css檔案

@charset "utf-8";h1{color:Red;text-align:center;}table th{  background-color:#7cfc00;  } 

程式運行結果如下:

 

上例展示了Smarty的基本用法,當然Smarty還提供了更加方便使用的介面函數,如對錶格,可以使用{html_table}來快速產生表格。有興趣的讀者可以試試。

現在這個程式再加上《jquery 表格的增加刪除和修改及設定奇偶行顏色》中對錶格的增加刪除和修改功能就基本上可以完善了,請參見下一篇《PHP訪問MySql資料庫 進階篇 AJAX技術》。

 

 

轉載請標明出處,原文地址:http://blog.csdn.net/morewindows/article/details/7094642

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.