PHP Access MySQL Database Intermediate article smarty Technology _php Tutorial

Source: Internet
Author: User
Tags php template jquery library
Before reading this article, it is recommended that you first refer to the PHP access MySQL database primary article.

Smarty is a template engine written in PHP, and is one of the most famous PHP template engines in the industry today. It separates the logic code from the external content, separating the PHP code that was originally mixed with the HTML code. So that the PHP programmer and the front-end programmer of the site can achieve a good division of--php programmers to change the logic of the program will not affect the front-end staff of the page design, the front-end staff to re-modify the style of the page will not affect the program logic, which makes multi-person cooperation projects become particularly easy and easy to manage maintenance. Because Smarty has so many advantages, so the major domestic companies in the website programming using this programming method. Smarty's Manual provides access to the HTTP://WWW.SMARTY.NET/DOCS/EN/INDEX.TPL.

Here is a small example of the Smarty program, functionally the same as the primary-reading the data from t_student in the MySQL test database and then displaying it. The program is divided into 5 documents, namely smarty2.php, smarty2.html, smarty2_head.php, Smarty2.js and Smarty2.css. In addition, the program will reference the Smarty and jquery library files.

1. smarty2_head.php file

by Morewindows (http://www.BkJia.com)


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 ');
?>
by Morewindows (http://www.BkJia.com)

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

by Morewindows (http://www.BkJia.com)


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 record bars in 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");
}

Table header
$tpl _db_coltitle = $dbcolarray;

Contents of the Table
$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))//equivalent to $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 ');
?>
by Morewindows (http://www.BkJia.com)
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 record bars in 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");
}

Table header
$tpl _db_coltitle = $dbcolarray;

Contents of the Table
$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))//equivalent to $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 file







{$smarty. Const.db_tablename}


{$db _count} records are in the table













{foreach $db _coltitle as $col} {/foreach} {foreach $db _rows as $dbrow} {foreach $dbrow as $k = + $val} {/foreach} {/foreach}
{$col}
{$val}









<title>{$smarty. Const.db_tablename}</title>


{$db _count} records are in the table













{foreach $db _coltitle as $col} {/foreach} {foreach $db _rows as $dbrow} {foreach $dbrow as $k = + $val} {/foreach} {/foreach}
{$col}
{$val}



4. Smarty2.js file

$ (document). Ready (function ()
{
Control the color of odd and even rows with CSS
$ ("Table Tr:odd"). CSS ("Background-color", "#e6e6fa");
$ ("Table Tr:even"). CSS ("Background-color", "#fff0fa");
});
$ (document). Ready (function ()
{
Control the color of odd and even rows with CSS
$ ("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;
}
@charset "Utf-8";
H1
{
color:red;
Text-align:center;
}
Table th
{
Background-color: #7cfc00;
}
The results of the program run as follows:



The example above shows the basic usage of smarty, of course Smarty also provides more convenient interface functions, such as tables, you can use {html_table} to quickly generate tables. Interested readers can try.

Now this program, together with the addition of "jquery table add delete and modify and set the odd and even row color" in the addition of the table to remove and modify the function is basically perfect, please see the next article "PHP Access MySQL database advanced Ajax technology."

Excerpted from Morewindows

http://www.bkjia.com/PHPjc/478476.html www.bkjia.com true http://www.bkjia.com/PHPjc/478476.html techarticle before reading this article, it is recommended that you first refer to the PHP access MySQL database primary article. Smarty is a template engine written in PHP, and is one of the most famous PHP template engines in the industry ...

  • 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.