PHP accessing MySQL database intermediate Article smarty Technology

Source: Internet
Author: User
Tags php template

Before reading this article, we recommend that you refer to "accessing MySQL database with PHP".

Smarty is a template engine written in the PHP language and one of the most famous PHP template engines in the industry. It separates the logic code from the external content, and separates the PHP code from the HTML code. So that PHP programmers and front-end programmers of the website can achieve a good division of labor-PHP programmers change the logic content of the program does not affect the front-end staff's page design, the re-modification of the page style by the front-end personnel does not affect the program logic, which makes the projects that many people work with easy to manage and maintain. Because smarty has so many advantages, all major Chinese companies adopt this programming method in website programming. For the smarty manual, visit http://www.smarty.net/docs/en/index.tpl.

 

The following is a small example of the smarty program. The function is the same as that in the preliminary article-read data from the t_student In the MySQL test database and then display it. The program is divided into five files, respectively, smarty2.php?smarty2.html=smarty2_head.php=smarty2.jsand smarty2.css. In addition, the program must reference the library files of smarty and jquery.

1. smarty2_head.php File

<?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 File

<? 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); // number of records in the table $ 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");} // header $ tpl_db_coltitle = $ dbcolarray; // table content $ 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) // with $ row = Mysql_fetch_assoc ($ result) equivalent $ 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"> 

4. smarty2.js File

$ (Document ). ready (function () {// use CSS to control the color of the odd and even rows $ ("Table TR: odd" ).css ("background-color", "# e6e6fa "); $ ("Table TR: Even" ).css ("background-color", "# fff0fa ");});

5.smarty2.css File

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

The program running result is as follows:

 

The previous example shows the basic usage of smarty. Of course, smarty also provides more convenient interface functions. For example, you can use {html_table} to quickly generate a table. If you are interested, try it.

Now this program adds 《Add, delete, modify, and set the color of the odd and even rows in the jquery table.The addition, deletion, and modification of tables can basically be improved. For more information, see the next section "php advanced Ajax technology for MySQL database access".

 

 

Reprinted please indicate the source, original address: http://blog.csdn.net/morewindows/article/details/7094642

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.