[转] SMARTY instance tutorial - Example

xiaoxiao2021-03-06  59

Smarty instance tutorial

--- Example

SMARTY instance tutorial ---- Example Article (1. Using PHP Built-in Mysql Function)

From this section, we will learn if SMARTY is applied to the actual example. For the demonstration, I will talk to the previous site as an instance. Let me explain, my own artistic work is not very strong, so The design of the design is not very good, everyone is dealing with it.

Before you see this article, you can first look at IndexBak.htm and NewsBak.htm, which is an example of our generation.

First, first explain the database we will use, let me first exist of the database source file:

============================================================================================================================================================================================================= ====================

Create Database News;

Uses;

/ ****************************

*

* Table name: TB_NEWS_CH

* Used: Domestic news

*

*************************** /

Create Table TB_News_CH

(

INEWSID INTERGER (11) Primary Key Auto_Increment,

Vcnewstitle Varchar (50) Not Null,

LTNewsContent Longtext Not Null

);

/ ****************************

*

* Table Name: TB_News_in

* Used: International News

*

*************************** /

Create Table TB_News_in

(

INEWSID INTERGER (11) Primary Key Auto_Increment,

Vcnewstitle Varchar (50) Not Null,

LTNewsContent Longtext Not Null

);

/ ****************************

*

* Table Name: TB_NEWS_MU

* Talking: Entertainment News

*

*************************** /

Create Table TB_News_MU

(

INEWSID INTERGER (11) Primary Key Auto_Increment,

Vcnewstitle Varchar (50) Not Null,

LTNewsContent Longtext Not Null

);

============================================================================================================================================================================================================= ==================== I simply explain the database.

first question:

Everyone can see that the field name of the three data sheets is the same, then why don't you merge them into a data sheet, the answer is simple: efficiency, when we develop, we may feel anything to improve the efficiency But everyone thinks, when this website is running for a while, its news data will become large, and considering that if the website may be separated, the various types may be separated to form similar China.xxx.com, International .xxx.com, music.xxx.com, separately separate each column to physically make a separate site. If the news is placed, if the news is placed, it will cause the database bottleneck, so for the current site, I It is reasonable to think that it is reasonable.

second question:

Some people may ask, what is the I, VC, and LT you add in the field? Here is named by the field type, which is also a good style, prefix the type prefix before the variable, use the user without seeing the field Definition can know what type is of the field. This is from Microsoft's Hungarian nomenclature. When designing the database, I first define each type into 1 - 3 letters, then add corresponding to each field. The code to represent its type. Like the top, I define each database field type as:

Integer i

VARCHAR VC

Longtext LT

Char C

....

When you are using, you can enter 5 data records in each data table to prepare us when you debug instances.

Second, the sample site directory structure:

PHP code: ----------------------------------------------- ---------------------------------

Web (Site root directory)

|

| ---- Comm (Smarty Related Document Directory)

| | |

| | ---- Plugins (Smarty Plugin Directory)

| | ----- Config_File.class.php (Smarty Profile)

| | ----- Smarty.class.php (Smarty Primary File)

| | ----- Smarty_Compiler.class.php (Smarty Compile "

|

| ---- cache (Smarty Cache Directory, * NIX to ensure read and write permissions)

|

| ---- Templates (Site Template File Save Directory)

| | |

| | ---- Header.tpl (Page Tablet Template File)

| | ---- Index.tpl (Site Home Template File)

| | ---- Foot.TPL (Page Foot Template File)

| | ---- News.tpl (News page template file)

|

|

| ---- Templates_c (save the catalog after the template file, * NIX guarantees read and write permissions)

|

| ---- CSS (Site CSS Directory)

|

| ---- Image (Site Picture Directory)

|

| ---- Media (Site Flash Animation Storage Directory)

|

| ---- IndexBak.htm (Home Original Render]

|

| ---- NewsBak, HTM (News page Original effect)

|

| ---- Index.php (Smarty Home Program File)

|

| ---- News.php (Smarty News Display File)

|

| ---- routine description .txt (directory instructions) |

| ---- Database Establishment File .txt (Create Document for Database)

For details, please download the example after the example.

-------------------------------------------------- ------------------------------

Third, instance fragment in the template:

INDEX.TPL:

Index.tpl is the template class of the homepage. After opening its source file, we can first see such a sentence:

=============================================================================================================================================================================================================

1. <{* below this sentence is page header *}>

2. <{include file = "Header.tpl"}>

=============================================================================================================================================================================================================

The first sentence knows, is a template notes

The second sentence indicates that there is another file in the current location, what file, Header.tpl, this header.tpl is the standard page file of the page, and it is separately used for this part independently, so that Reuse in other pages, habits to write the page files in a table, but because my instance is already made, some are not in line with the norm.

Let's see the last sentence:

================================================================================★

3. <{include file = "foot.tpl"}>

================================================================================★

Needless to say everyone understand, it is to include a footer file.

Take a look at the template code of the domestic news section:

=========================================================================================== < Table width = "100%" border = "0" cellpadding = "0" cellspacing = "0" bgcolor = "# b9e9ff">

<{section name = loop loop = $ news_ch}>

  • <{$ news_ch [loop] .newstitle}>

    <{/ section}>

    ================================================================================★

    Everyone is the key to <{section}> and <{/ section}>, which is the part other than understanding the meaning of code. can you understand? Here, you will review the previously told content: Define a section cycle template, name is loop, to looping $ news_ch This array, with will generate a link for the current news, The address is news.php? Type = 1 & id = xxx, the XXX here is an inewsid extracted from the database, which means to display the news that the number ID is displayed in the news display page. $ News_ch [loop] .newstitle This representation understands? Look at the <{section}> syntax in the "Smarty Instance Teaching-Program Design" section in the previous section.

    Maybe everyone is a bit unfamiliar with <{}> here, the <{}> is a template statement, and we use {} in the first 2 sections, but because this is a specific application, no {}, use The <{}> of everyone habit, of course, this is set in .php file.

    Look at the code of international news and entertainment news:

    international News:

    =========================================================

    <{section name = loop loop = $ news_in}>

  • <{$ news_in [loop] .newstitle}>

    <{/ section}>

    =================================================

    Entertainment News:

    =================================================

    <{section name = loop loop = $ news_mu}>

  • <{$ news_mu [loop] .newstitle}>

    <{/ section}>

    =================================================

    Is there any difference? Loop part is different, loop refers to an array to be loop. Here, you should pay attention to the same value as the same value. If the SMARTY will analyze the template, it will Two steps have the same name simultaneously, generate two or the same records.

    Seeing this, someone will have questions: I have already had a loop block, how can I only display the number of records we want on the current location? This problem is simple, we are determined by controlling the number of SMARTY's section cycle blocks to control the arrays to replace this loop block in .PHP files, we don't have to consider when designing templates.

    Second, let's take a look at News.tpl:

    1. Look at this sentence:

    <{$ newstitle}> ---- Tianjiao Information Network </ Title></p> <p>The title of the news page to display is displayed as the news title "---- Tianjiao Information Network"</p> <p>2. News headline:</p> <p><div align = "center" class = "newstitle"> <{$ newstitle}> </ div></p> <p>Here is a template variable $ newstitle, meaning. PHP replaces $ newstitle to the title of current news in the database.</p> <p>3. News content:</p> <p><p> <{$ newscontext}> </ p></p> <p>This sentence is also easy to display news content in the current location.</p> <p>Of course, it is just simple to list the news, you can also list the article, publish the time, author, and related news when practical applications, and discuss it here.</p> <p>Third, the program part:</p> <p>1. Let's take a look at the source file of index.php, then let's take another step by analyze its role:</p> <p>============================================================================================================================================================================================================= ===========</p> <p>Index.php</p> <p>============================================================================================================================================================================================================= =========== <? PHP</p> <p>1. /*********************************************</p> <p>*</p> <p>* File name: index.php</p> <p>* Decision: Display instance program</p> <p>*</p> <p>* Servers: Master</p> <p>* Email:</p> <p>Teacherli@163.com</p> <p>*</p> <p>*************************************************** /</p> <p>2. INCLUDE_ONCE ("./ Comm / Smarty.class.php"); // contains the Smarty class file</p> <p>3. Define ("num", 5); // Define the number of news entries each displayed</p> <p>4. $ smarty = new smarty (); // Create a smarty instance object $ smarty</p> <p>$ Smarty-> Templates_Dir = "./Templates"; // Settings Template Directory</p> <p>$ smarty-> compile_dir = "./Templates_c"; // Set the compilation directory</p> <p>$ smarty-> cache_dir = "./cache"; // Set the cache directory</p> <p>$ smarty-> cache_lifetime = 60 * 60 * 24; // Set the cache time</p> <p>$ Smarty-> Caching = false; // This is set to false when debugging, please use true when publishing</p> <p>$ Smarty-> LEFT_DELIMITER = "<{"; // Set the left boundary</p> <p>$ Smarty-> Right_Delimiter = "}>"; // Set the right border</p> <p>5. $ db = mysql_connect ("localhost", "root", "");</p> <p>MySQL_SELECT_DB ("News", $ db);</p> <p>// This will be dealt with the domestic news section</p> <p>6. $ strQuery = "select inewsid, inewstitle from tb_news_ch order by inewsid desc";</p> <p>$ result = mysql_query ($ strquery);</p> <p>$ I = NUM;</p> <p>7. While ($ ROW = mysql_fetch_array ($ result)) && $ i> 0)</p> <p>{</p> <p>$ array [] = array ("newsid", Substr ($ row ["inewsid"], 0, 40),</p> <p>"Newstitle", Substr ($ ROW ["VcneWstitle"], 0, 40));</p> <p>$ I -;</p> <p>}</p> <p>8. $ SMARTY-> Assign ("news_ch", $ array);</p> <p>9. Unset ($ array);</p> <p>MySQL_Free_Result ();</p> <p>10. // Treat the International News section</p> <p>$ strQuery = "select inewsid, inewstitle from tb_news_in order by inewsid desc"; $ results = mysql_query ($ strquery);</p> <p>$ I = NUM;</p> <p>While ($ ROW = mysql_fetch_array ($ result)) && $ i> 0)</p> <p>{</p> <p>$ array [] = array ("newsid", Substr ($ row ["inewsid"], 0, 40),</p> <p>"Newstitle", Substr ($ ROW ["VcneWstitle"], 0, 40));</p> <p>$ I -;</p> <p>}</p> <p>$ Smarty-> Assign ("News_IN", $ Array);</p> <p>UNSET ($ array);</p> <p>MySQL_Free_Result ();</p> <p>11. // This section will be dealt with the entertainment news</p> <p>$ STRQUERY = "SELECT INEWSID, INEWSTITLE from TB_NEWS_MU ORDER BY INEWSID DESC";</p> <p>$ result = mysql_query ($ strquery);</p> <p>$ I = NUM;</p> <p>While ($ ROW = mysql_fetch_array ($ result)) && $ i> 0)</p> <p>{</p> <p>$ array [] = array ("newsid", Substr ($ row ["inewsid"], 0, 40),</p> <p>"Newstitle", Substr ($ ROW ["VcneWstitle"], 0, 40));</p> <p>$ I -;</p> <p>}</p> <p>$ Smarty-> Assign ("News_MU", $ Array;</p> <p>UNSET ($ array);</p> <p>MySQL_Free_Result ();</p> <p>MySQL_Close ($ dB);</p> <p>// Compile and display the INDEX.TPL template under ./Templates</p> <p>12. $ Smarty-> Display ("INDEX.TPL");</p> <p>?></p> <p>In order to facilitate explanation, I labeled every place to be explained, and it will be said to have the role of each part:</p> <p>1. Program annotation, style problem, I have multiple times in the program to make readers to develop this habit.</p> <p>2. The file is included. This sentence means containing the class file of Smarty to the current program file.</p> <p>3. Constant definition: Define NUM to 5, the number of news display</p> <p>4. Set the smarty parameter: About the parameters inside, you can refer to the previous section, here only want to specify $ smarty-> cache properties, during our program debugging process, set it to false, officially released When you design it to true, you can implement our program cache effect.</p> <p>5. Database connection: I don't have to say this part? Standard PHP statement.</p> <p>6. During the domestic news search SQL statement: Standard SQL statement, order sequencing in order numbers.</p> <p>7. While ():</p> <p>This sentence controls the number of news rows to be displayed. First, give $ I Num, NUM for us a constant defined outside, use $ I> 0 conditions at the start judgment of While (), while () each time The value of $ i should be reduced when the cycle is cycled, so that when it completes 5 cycles, the while will end.</p> <p>Here we mainly take a look at $ array [] This array:</p> <p>$ rail [] is theoretically a two-dimensional array, that is, an element in an array is another array. In the above example, when the while () statement ends, $ array is an array containing 5 array elements, Each element is an array of characters indexes. And these character indexes are the property values ​​of the $ news [loop] defined in the template. Everyone must pay attention to the attribute value of the template cycle block. Otherwise, it will not be displayed!</p> <p>When building this two-dimensional array, we use a substr () method for character interception. This method is here for reference only. In actual use because Chinese is a full-horn character, it may cause garbled when using it, in the later example in the examples Another netizen will use the CSUBSTR () function to implement the character interception function. 8. Resolution of the section cycle block in the template's loop = news_ch.</p> <p>Everyone should pay attention to using $ smarty-> assign ("news_ch", $ array), ASSIGN's first parameter is the value of LOOP in section, and the second parameter is the array established above.</p> <p>9. Log out $ ARRAY: $ array [] After using it, you will not be emptied when you use the assignment statement to assign $ array, but the new array is increased as an element of it, in order not to produce side effects, It is necessary to use unset () here.</p> <p>10. International News Processing Module:</p> <p>Like domestic news processing modules, you can compare the template and this paragraph, find what is related to what is related.</p> <p>11. Entertainment News Process Module: I have the same, everyone finds the relationship. I believe that everyone will have harvested.</p> <p>12. Process and display index.tpl.</p> <p>2. Let's take a look at the source file of News.php:</p> <p>============================================================================================================================================================================================================= ======</p> <p>News.php</p> <p>============================================================================================================================================================================================================= ======</p> <p><? PHP</p> <p>/ ***************************************************</p> <p>*</p> <p>* File name: news.php</p> <p>* Job: News display</p> <p>*</p> <p>* Servers: Master</p> <p>* Email:</p> <p>Teacherli@163.com</p> <p>*</p> <p>*************************************************** /</p> <p>INCLUDE_ONCE ("./ Comm / Smarty.class.php"); // contains the smarty file</p> <p>Define ("NUM", 5); // Defines the number of news bars each displayed</p> <p>$ smarty = new smarty (); // Establish a smarty instance number $ smarty</p> <p>$ Smarty-> Templates_Dir = "./Templates"; // Settings Template Directory</p> <p>$ smarty-> compile_dir = "./Templates_c"; // Set the compilation directory</p> <p>$ smarty-> cache_dir = "./cache"; // Set the cache directory</p> <p>$ smarty-> cache_lifetime = 60 * 60 * 24; // Set the cache time</p> <p>$ Smarty-> Caching = false; // This is set to false when debugging, please use True $ Smarty-> Left_DELIMITER = "<{"; // set the left boundary character when publishing.</p> <p>$ Smarty-> Right_Delimiter = "}>"; // Set the right border</p> <p>$ db = mysql_connect ("localhost", "root", "") or Die ("Database Connection Error!");</p> <p>MySQL_SELECT_DB ("News", $ db);</p> <p>$ Newsid = $ _get ["id"]; // Get news numbers</p> <p>$ Newstype = $ _get ["type"]; // to display the type of news</p> <p>Switch ($ newstype)</p> <p>{</p> <p>Case 1:</p> <p>$ DBNAME = "TB_News_CH";</p> <p>Break;</p> <p>Case 2:</p> <p>$ dbname = "tb_news_in";</p> <p>Break;</p> <p>Case 3:</p> <p>$ dbname = "tb_news_mu";</p> <p>Break;</p> <p>}</p> <p>$ strQuery = "Select vcnewstitle, ltnewscontent from". $ dbname;</p> <p>$ results = mysql_query ($ strquery) or Die ("Database Query!");</p> <p>IF ($ row = mysql_fetch_array ($ result))</p> <p>{</p> <p>$ Smarty-> Assign ("NewStitle", $ ROW ["Vcnewstitle"]);</p> <p>$ Smarty-> Assign ("NewsContent", $ ROW ["LTNewsContent"]);</p> <p>MySQL_Free_Result ($ Result);</p> <p>$ Smarty-> Display ("news.tpl");</p> <p>}</p> <p>MySQL_Close ($ dB);</p> <p>?></p> <p>This is only 3 lines about Smarty's program statement:</p> <p>============================================================================================================================================================================================================= ================</p> <p>$ Smarty-> Assign ("NewStitle", $ ROW ["Vcnewstitle"]);</p> <p>$ Smarty-> Assign ("NewsContent", $ ROW ["LTNewsContent"]);</p> <p>MySQL_Free_Result ($ Result);</p> <p>============================================================================================================================================================================================================= ================ It is very simple to replace newstitle in the news page to find the content you are looking for from the database.</p> <p>Ok, the primary part of this SMARTY routine learning is here, I believe that everyone will have a basic understanding of Smarty after learning, and the next section also uses this example to talk about how to use phplib. The DB class is to implement control of the template.</p> <p>============================================================================================================================================================================================================= ========================================</p> <p>SMARTY instance tutorial example articles (II, using phplib's DB class)</p> <p>A few days ago, this example has been written today, why do you want to take the db class in phplib? Many people are learning PHP, the template that may first come first is PHPLIB. The reason is very simple: Many PHP learning materials are introduced to phplib. When PHPLIB is very hot, one is that it is not implemented in PHP3. SESSION features, until now there are still many people using phplib template technology because it is very simple. At the same time, the database operation class on phplib also has a small file, and the loading speed is fast.</p> <p>. I like its syntax, I feel very close to mySQL statement in PHP. Ok, there is not much nonsense, come see our procedure. The program also said in the site of the instance. About the establishment of the database and the establishment of the template, please refer to the introduction of the next section, mainly introducing the newly added to the program. Let's see the catalog table first:</p> <p> Web (Site root directory)</p> <p>|</p> <p>| ---- Comm (Smarty Related Document Directory)</p> <p>| | |</p> <p>| | ---- Plugins (Smarty Plugin Directory)</p> <p>| | ----- Config_File.class.php (Smarty Profile)</p> <p>| | ----- Smarty.class.php (Smarty Primary File)</p> <p>| | ----- Smarty_Compiler.class.php (Smarty Compile "</p> <p>| | ----- DB_MYSQL.Inc.php (db class in phplib)</p> <p>| | ----- csub.inc.php (a function of intercepting Chinese)</p> <p>|</p> <p>| ---- Cache (Smarty Cache Directory, * NIX to ensure read and write permissions) |</p> <p>| ---- Templates (Site Template File Save Directory)</p> <p>| | |</p> <p>| | ---- Header.tpl (Page Tablet Template File)</p> <p>| | ---- Index.tpl (Site Home Template File)</p> <p>| | ---- Foot.TPL (Page Foot Template File)</p> <p>| | ---- News.tpl (News page template file)</p> <p>|</p> <p>|</p> <p>| ---- Templates_c (save the catalog after the template file, * NIX guarantees read and write permissions)</p> <p>|</p> <p>| ---- CSS (Site CSS Directory)</p> <p>|</p> <p>| ---- Image (Site Picture Directory)</p> <p>|</p> <p>| ---- Media (Site Flash Animation Storage Directory)</p> <p>|</p> <p>| ---- IndexBak.htm (Home Original Render]</p> <p>|</p> <p>| ---- NewsBak, HTM (News page Original effect)</p> <p>|</p> <p>| ---- Index.php (Smarty Home Program File)</p> <p>|</p> <p>| ---- News.php (Smarty News Display File)</p> <p>|</p> <p>| ---- routine description .txt (this document)</p> <p>Here mainly in the / commit / directory, one is db_mysql.inc.php, which is called DB_MYSQL.INC in phplib, and the Inc file is displayed directly from the browser by default. In the device, so for security we change its extension to db_mysql.inc.php; the second file is a function that contains intercept Chinese characters.</p> <p>CSUBSTR (), when we use it, we display its source code.</p> <p>Here first, the main member variables of DB_SQL defined in db_mysql.inc.php will give you a brief introduction to everyone:</p> <p>First, member variables:</p> <p>1. $ Host: Host Name</p> <p>2. $ Database: Database Name</p> <p>3. $ user: Username</p> <p>4. $ Password: Password</p> <p>Second, member functions:</p> <p>1. Connect ($ Database = ", $ Host =" ", $ user =", $ password = ""): Establish a connection, return to the connection ID</p> <p>2. Query ($ query_string): Inquiry, return to the query ID</p> <p>3. Free (): Release the current query ID resource</p> <p>4. Next_Record (): Return to the next return set</p> <p>5. Num_Rows (): The number of data lines in the current query number</p> <p>6. f ($ name): The value of the current field</p> <p>Other functions are basically not used in our procedures, so they don't introduce, interested readers can view relevant information themselves.</p> <p>PHP code: ----------------------------------------------- ---------------------------------</p> <p>INDEX.TPL in Templates and News.tpl with the previous section, different only in Index.php and News.tpl, first take a look at INDEX.PHP:</p> <p>================================</p> <p>Index.php</p> <p>================================</p> <p><? PHP</p> <p>/ ******************************************************</p> <p>* File name: index.php</p> <p>* Decision: Display instance program</p> <p>*</p> <p>* Servers: Master</p> <p>* Email:</p> <p>Teacherli@163.com</p> <p>*</p> <p>*************************************************** /</p> <p>INCLUDE_ONCE ("./ Comm / Smarty.class.php"); // contains the smarty file</p> <p>1. / / =======================================</p> <p>INCLUDE_ONCE ("./ Comm / DB_MYSQL.Inc.php"); / / contains database operation classes</p> <p>INCLUDE_ONCE ("./ Comm / Csubstr.inc"); // Contains Chinese Intercept class</p> <p>/ / ===============================================================================================================================================================================</p> <p>Define ("NUM", 5); // Defines the number of news bars each displayed</p> <p>$ smarty = new smarty (); // Establish a smarty instance number $ smarty</p> <p>$ Smarty-> Templates_Dir = "./Templates"; // Settings Template Directory</p> <p>$ smarty-> compile_dir = "./ Templates_c"; // Set the compilation directory</p> <p>$ smarty-> cache_dir = "./cache"; // Set the cache directory</p> <p>$ Smarty-> Caching = false; // This is set to false when debugging, please use true when publishing</p> <p>$ Smarty-> LEFT_DELIMITER = "<{"; // Set the left boundary</p> <p>$ Smarty-> Right_Delimiter = "}>"; // Set the right border</p> <p>2. $ db = new db_sql (); / / instantiate a DB class</p> <p>$ dB-> host = "localhost"; // database host name</p> <p>$ db-> database = "news"; // database name</p> <p>$ dB-> user = "root"; // user name</p> <p>$ db-> password = ""; // password</p> <p>$ db-> connet (); // Database connection</p> <p>// This will be dealt with the domestic news section</p> <p>$ strquery = "select inewsid, inewstitle from tb_news_ch order by inewsid desc";</p> <p>3. $ db-> query ($ strquery);</p> <p>$ I = NUM;</p> <p>4. While ($ db-> next_record () && $ i> 0)</p> <p>{</p> <p>5. $ array [] = array ("newsid", csubstr ($ db-> f ("inewsid"), 0, 20), "newstitle", csubstr ($ db-> f ("vcnewstitle", 0, 20));</p> <p>$ I -;</p> <p>}</p> <p>$ Smarty-> Assign ("News_CH", $ Array);</p> <p>UNSET ($ array);</p> <p>6. $ db-> free ();</p> <p>// Treat the international news section</p> <p>$ strQuery = "select inewsid, inewstitle from tb_news_in order by inewsid desc";</p> <p>$ dB-> query ($ strquery);</p> <p>$ I = NUM;</p> <p>While ($ db-> next_record () && $ i> 0)</p> <p>{</p> <p>$ array [] = array ("newsid", csubstr ($ db-> f ("inewsid"), 0, 20),</p> <p>"Newstitle", CSUBSTR ($ db-> f ("vcnewstitle"), 0, 20));</p> <p>$ I -;</p> <p>}</p> <p>$ Smarty-> Assign ("News_IN", $ Array);</p> <p>UNSET ($ array);</p> <p>$ dB-> free ();</p> <p>// Here you will process the entertainment news part</p> <p>$ STRQUERY = "SELECT INEWSID, INEWSTITLE from TB_NEWS_MU ORDER BY INEWSID DESC";</p> <p>$ dB-> query ($ strquery);</p> <p>$ I = NUM;</p> <p>While ($ db-> next_record () && $ i> 0)</p> <p>{</p> <p>$ array [] = array ("newsid", csubstr ($ db-> f ("inewsid"), 0, 20),</p> <p>"Newstitle", CSUBSTR ($ db-> f ("vcnewstitle"), 0, 20));</p> <p>$ I -;</p> <p>}</p> <p>$ Smarty-> Assign ("News_MU", $ Array;</p> <p>UNSET ($ array);</p> <p>$ dB-> free ();</p> <p>// Compile and display the INDEX.TPL template under ./Templates</p> <p>$ Smarty-> Display ("INDEX.TPL");</p> <p>?></p> <p>Old rules, I added a label for key places to see these places:</p> <p>1. Here is to include the db_sql class file, the following line is to include a Chinese intercept class to the current page</p> <p>2. Instantiate a class and set the Host, Database, User, Password property of the class, last Connect database</p> <p>3. Call $ DB-> Query ($ STRQUERY) for a data query</p> <p>4. $ db-> next_record () Read the next line of the database query return value (Note: Call Query () Returns the first record of the first record of the data set)</p> <p>5. csubstr () is a Chinese intercept class we use, the source file of the class is as follows:</p> <p>============================================================================================================================================================================================================= === csubstr.inc.php</p> <p>============================================================================================================================================================================================================= ===</p> <p>6. $ db-> free (): Release the current query back resources</p> <p>Ok, use the class for database operation, you can improve the program's readability, and it is also convenient for the operation of the database.</p> <p>Next, let's take a look at News.php:</p> <p>================================================================================★</p> <p>News.php</p> <p>================================================================================★</p> <p><? PHP</p> <p>/ ***************************************************</p> <p>*</p> <p>* File name: news.php</p> <p>* Job: News display</p> <p>*</p> <p>* Servers: Master</p> <p>* Email:</p> <p>Teacherli@163.com</p> <p>*</p> <p>*************************************************** /</p> <p>INCLUDE_ONCE ("./ Comm / Smarty.class.php"); // contains the smarty file</p> <p>/ / ===============================================================================================================================================================================</p> <p>INCLUDE_ONCE ("./ Comm / DB_MYSQL.Inc.php"); / / contains database operation classes</p> <p>INCLUDE_ONCE ("./ Comm / Csubstr.inc"); // contains Chinese intercept classes / / ============================= ==========</p> <p>Define ("NUM", 5); // Defines the number of news bars each displayed</p> <p>$ smarty = new smarty (); // Establish a smarty instance number $ smarty</p> <p>$ Smarty-> Templates_Dir = "./Templates"; // Settings Template Directory</p> <p>$ smarty-> compile_dir = "./ Templates_c"; // Set the compilation directory</p> <p>$ smarty-> cache_dir = "./cache"; // Set the cache directory</p> <p>$ smarty-> cache_lifetime = 60 * 60 * 24; // Set the cache time</p> <p>$ Smarty-> Caching = false; // This is set to false when debugging, please use true when publishing</p> <p>$ Smarty-> LEFT_DELIMITER = "<{"; // Set the left boundary</p> <p>$ Smarty-> Right_Delimiter = "}>"; // Set the right border</p> <p>$ dB = new db_sql (); // instantiate a DB class</p> <p>$ dB-> host = "localhost"; // database host name</p> <p>$ db-> database = "news"; // database name</p> <p>$ dB-> user = "root"; // user name</p> <p>$ db-> password = ""; // password</p> <p>$ db-> connet (); // Database connection</p> <p>$ Newsid = $ _get ["id"]; // Get news numbers</p> <p>$ Newstype = $ _get ["type"]; // to display the type of news</p> <p>Switch ($ newstype)</p> <p>{</p> <p>Case 1:</p> <p>$ DBNAME = "TB_News_CH";</p> <p>Break;</p> <p>Case 2:</p> <p>$ dbname = "tb_news_in";</p> <p>Break;</p> <p>Case 3:</p> <p>$ dbname = "tb_news_mu";</p> <p>Break;</p> <p>}</p> <p>$ strQuery = "Select vcnewstitle, ltnewscontent from". $ dbname;</p> <p>1. $ db-> query ($ strquery);</p> <p>2. IF ($ db-> next_record ())</p> <p>{</p> <p>$ Smarty-> Assign ("NewStitle", $ db-> f ("vcnewstitle"));</p> <p>$ Smarty-> Assign ("NewsContent", $ db-> f ("ltnewscontent");</p> <p>$ dB-> free ();</p> <p>$ Smarty-> Display ("news.tpl");</p> <p>Else</p> <p>{</p> <p>Echo "No News Content";</p> <p>}</p> <p>?></p> <p>Everyone can take a look at 1, 2, is it simpler than the original mysql function directly?</p> <p>If you are excess, don't say much, if you have seen the previous example seriously, then this article should be very well understood.</p> <p>======================</p> <p>postscript:</p> <p>The template part I basically spended some of the examples in the manual, and the actual essence is part of the program design part. If you want to learn it, I have to see two cases, I put two routines and previous mentioned The sample program is placed in three packaging files, tested by "Shenxian Sister", everyone can debug while watching.</p> <p>Here is 3 examples, I pack them together.</p> <p>SMARTY program example .rar</p> <p>============================================================================================================================================================================================================= ========================================</p> <p>SMARTY instance teaching instance articles (three, using the AdoDB connection database)</p> <p>The first two months have been very busy because of the work of the work, so don't complete this tutorial in time, just don't have to work overtime today, take an empty completed it! When you start the new tutorial, I</p> <p>First modify some of the mistakes in the tutorial you wrote, I would like to thank Nesta2001zhang brothers, he found some mistakes in the article, otherwise it was really</p> <p>"The wrong child" (said it is really embarrassing, my first draft is released afterwards, and I found a lot of questions, and then there was a mistake in the document after returning to revisions, really should not</p> <p>This ...)</p> <p>In the previous tutorial:</p> <p>============================================================================================================================================================================================================= =======</p> <p>While ($ db-> next_record () && $ i> 0)</p> <p>{</p> <p>$ array [] = array ("newsid", csubstr ($ db-> f ("inewsid"), 0, 20),</p> <p>"Newstitle", CSUBSTR ($ db-> f ("vcnewstitle"), 0, 20));</p> <p>$ I -;</p> <p>}</p> <p>============================================================================================================================================================================================================= ======= should be changed to:</p> <p>============================================================================================================================================================================================================= =======</p> <p>While ($ db-> next_record () && $ i> 0)</p> <p>{</p> <p>$ array [] = array ("newsid" => $ db-> f ("inewsid"),</p> <p>"Newstitle" => csubstr ($ db-> f ("vcnewstitle"), 0, 20));</p> <p>$ I -;</p> <p>}</p> <p>============================================================================================================================================================================================================= =======</p> <p>Why don't you change this? Because the second method is clearer, it is more clear. It is actually the effect of the first way and the second method is not different, and I have been debugged with the few programs.</p> <p>There is no problem.</p> <p>Ok, then we will talk about AdoDB today, say adoDB, you may have an ASP know the ADO components of the Windows platform, but our adodb is not Microsoft's database.</p> <p>As a component, but a set of database operations class libraries written in the PHP language, let's take a look at what is the advantage of it.</p> <p>1. Database execution code written in a standard SQL statement does not have to change the source when performing database transplantation, that is, it supports multiple databases, including Access.</p> <p>2. Provide similar syntax features similar to Microsoft AdoDB. This is a great gospel for people from ASP to PHP, and many of its operations are similar to ADODB in Windows.</p> <p>3. You can generate the two-dimensional array of Smarty cycles, which simplifies smarty development. This is the wait for me to give you a demonstration.</p> <p>4. Support the database's cache query, the most likely to improve the speed of the query database.</p> <p>5. Other practical features.</p> <p>Although there are many advantages, because this library is very large, it is 107K, so if you consider the efficiency, you must think about it. But to be honest, its</p> <p>The function is still very powerful, there are a lot of practical features, using it, it can be very convenient to implement the features we want. So everyone does not have special requirements for those bosses</p> <p>Use</p> <p>First, how to get adoDB? What is its operating environment?</p> <p>From http://sourceforge.net/project/show ... 簆 HP4.0.5 or more. Second, how to install ADODB?</p> <p>Unzip the underlying compressed file, pay attention: Everyone downloads back format is adoDb.tar.gz, this is Linux's compression format, you can use WinRAR to enter in Windows.</p> <p>Under the completion of the decompression, copy the directory to the AdoDB directory of the specified directory, copy it to / comm / adoDB / in the example.</p> <p>Third, how to call AdoDB?</p> <p>Use incrude_once ("./comm/adodb/adodb.inc.inc.php"); don't say it? Contains the main file of AdoDB.</p> <p>Fourth, how to use AdoDB?</p> <p>1. Perform initialization:</p> <p>AdoDB uses $ conn = adonewconnection (); this statement is initialized, and there are two ways to initialize AdoDB:</p> <p>The first way is: Traditional ways. I temporarily called it for this name. It is used to create a new connection method like a standard connection in PHP:</p> <p>$ conn = new adonewconnection ($ dbdriver);</p> <p>$ conn-> Connect ($ Host, $ User, $ Passwd, $ dB);</p> <p>Simple? If you use the DB class in phplib, you should be familiar with it.</p> <p>The second way: uses the DSN mode, which is written to a statement to initialize the connection statement of the database. The writing of the DSN is: $ dsn =</p> <p>"Dbtype: // user: passwd @ host / dbname"; where dbtype indicates database type, user names, passwd is password, Host is server name, dbname is the database name</p> <p>Like this, I use Oracle Database, User Name: Oracleuser, Password to OraclePasswd, Database Server to Localhost, Database to ORADB DSN Words:</p> <p>$ DSN = "Oracle: // OracleUserraclepasswd @ localhost / oradb";</p> <p>$ conn = new adonewconnection ($ DSN);</p> <p>This approach may be more interested in programmers coming from ASP.</p> <p>These two ways can be used, to see personal habits.</p> <p>2. Related concepts:</p> <p>There are two basic classes using AdoDB, one is the Adoconnection class, the other is the AdoCordset class, people who have used ASP see that these two classes will understand its meaning,</p> <p>AdoConnection refers to the class of database connections, and the adorecordset refers to the data set by AdoConnection to perform query statements. The relevant information can be queried by AdoDB</p> <p>Class manual.</p> <p>3. Basic functions:</p> <p>About the relevant methods of the Adoconnection class are:</p> <p>1.Connect: Database connection method, we introduced it. There is also a PConnect for mysql, just like the usage in the PHP language.</p> <p>2.Execute: The result of the execution query statement is returned to an Adorecordset class.</p> <p>3.Getone ($ SQL): Returns the first field of the first line</p> <p>4.Getall ($ SQL): Returns all data. This function is a big use, remember that I will need to write on the page when I write about the news list in the previous tutorial.</p> <p>News list makes a two-dimensional array? Is this statement:</p> <p>============================================================================================================================================================================================================= ========================================== WHILE ($ db-> next_record ())</p> <p>{</p> <p>$ array [] = array ("newsid" => $ db-> f ("inewsid"),</p> <p>"Newstitle" => csubstr ($ db-> f ("vcnewstitle"), 0, 20));</p> <p>}</p> <p>============================================================================================================================================================================================================= ====================================</p> <p>What does this line mean? It is the news example that will be displayed.</p> <p>$ array [0] = array ("newsid" => 1, "newstitle" => "The first article here");</p> <p>$ array [1] = array ("newsid" => 2, "newstitle" => "Article 2 of the news");</p> <p>...</p> <p>In this form, but if we don't need to control the title, we are blessed in AdoDB, we can write this:</p> <p>============================================================================================================================================================================================================= ========================================= $ strQuery = "SELECT INEWS, VCNEWSTITIL AU"; "</p> <p>$ array = & $ conn-> getAll ($ strquery); // Note this statement</p> <p>$ Smarty-> Assign ("News_CH", $ Array);</p> <p>UNSET ($ array);</p> <p>============================================================================================================================================================================================================= =======================================================================================================================================================</p> <p>Of course, $ conn here should be initialized, I don't know if you understand? It turns out that the two-dimensional data created by hand is using getAll directly! ! ! This is also</p> <p>What some people will say Adodb Smarty is one of the causes of invincible combination ...</p> <p>4.SelectLimit ($ SQL, $ NumRows = -1, $ OFFSET = -1, $ INPUTARRR = FALSE): Return to a dataset, everyone is not difficult to see from the statement it is a limited query language</p> <p>Sentence, with LIMIT in the mysql statement, there is a simple example, come to a simple example:</p> <p>$ r = $ conn-> selectlimit ("Select inewsid, vcnewstitle from tb_news_ch", 5, 1);</p> <p>do you understand? Saved in $ RS is 5 records from the first record in the database. We know that in the Oracle database does not support using Limit in the SQL statement, but if we make</p> <p>With AdoDB, then this problem is easy to solve!</p> <p>5.Close (): Turn off the database, although PHP is automatically turned off at the end of the page, but for the full point of the program, the database is turned off on the page.</p> <p>About AdorecordSet.Adorecordset is the result of $ conn-> execute ($ SQL), its basic functions are as follows:</p> <p>1. Fields ($ colname): Returns the value of the field.</p> <p>2. RecordCount (): The number included. This record determines the total number of records of the data set .3. GetMenu ($ name, [$ default_str = '), [$ blank1stItem = true], [$ multiple_select = false], [$ size = 0], [$ moreattr = '']) Very good one</p> <p>Function, use it to return a name = $ name drop-down menu (or multiple box) !!! Of course, it is a html string, this is an exciting good thing, $ name refers to</p> <p>Option's Name property, $ default_str is a string selected by default, $ blank1stitem points out whether the first item is empty, $ multiple_select points to whether it is a multi-selection box, and we get this</p> <p>After the strings, you can use $ Smarty -> ("Templatevar", "GetMenustr") to enter a drop-down list at the TemplateVar (or more box)</p> <p>4. MoveNext (): To see a code:</p> <p>============================================================================================================================================================================================================= =======</p> <p>$ r = & $ conn-> exceuters;</p> <p>IF ($ r)</p> <p>{</p> <p>While ($ RS-> EOF)</p> <p>{</p> <p>$ array [] = array ("newsid" => $ rs-> fields ["inewsid"],</p> <p>"Newstitle" => csubstr ($ RS-> Fields ["vcnewstitle"]), 0, 20);</p> <p>$ r-> movelnext ();</p> <p>}</p> <p>}</p> <p>============================================================================================================================================================================================================= =======</p> <p>Understand? Very like MS AdoDB!</p> <p>5. MoveFirst (), Movese (), Move ($ to): The same, see what the function is named, you can know what it means.</p> <p>6. fetchrow (): Return to a row, see the code:</p> <p>============================================================================================================================================================================================================= =======</p> <p>$ r = & $ conn-> exceute ($ sql); if ($ r)</p> <p>{</p> <p>While ($ row = $ r-> fetchrow ())</p> <p>{</p> <p>$ array [] = array ("newsid" => $ row ["inewsid"],</p> <p>"Newstitle" => csubstr ($ row ["vcnewstitle"], 0, 20);</p> <p>}</p> <p>}</p> <p>============================================================================================================================================================================================================= =======</p> <p>It achieves the same functionality as 4, but it seems to be a habit of PHP, and 4 habits look more like MS AdoDB.</p> <p>7.GetArray ($ NUM): Returns the $ NUM line data in the data set, combines it into two-dimensional arrays. This method we are in an example index.php.</p> <p>8. Close (): With mySQL_FREE_RESULT ($ RS); clearing the content is occupied.</p> <p>Ok, the initial function is introduced here, we use it! In fact, AdoDB has many practical technology, including formatting date, format query statement, output form, more advanced</p> <p>Point cache query, with query, etc., you can view the manual yourself.</p> <p>Below we are beginning to learn our procedures, the same still the web program, I will reorganize it, and in order to improve the efficiency to add SMARTY.</p> <p>MySMARTY.CLASS.PHP is a packaged class, which inherits from smarty, so only new class mysmarty is called in all program files, first to see the directory structure:</p> <p> Web (Site root directory)</p> <p>|</p> <p>| ---- Comm (Smarty Related Document Directory)</p> <p>| | |</p> <p>| | ---- Smarty (Smarty Original Directory)</p> <p>| | ---- AdoDB (AdoDB Original Directory)</p> <p>| | ----- mysmarty.class.php (SMARTY file after the extended)</p> <p>| | ----- Csubstr.inc (intercepting Chinese characters)</p> <p>|</p> <p>| ---- cache (Smarty Cache Directory, * NIX to ensure read and write permissions)</p> <p>|</p> <p>| ---- Templates (Site Template File Save Directory)</p> <p>| | |</p> <p>| | ---- Header.tpl (Page Tablet Template File)</p> <p>| | ---- Index.tpl (Site Home Template File)</p> <p>| | ---- Foot.TPL (Page Foot Template File)</p> <p>| | ---- News.tpl (News page template file)</p> <p>|</p> <p>|</p> <p>| ---- Templates_c (save the catalog after the template file, * NIX guarantees read and write permissions)</p> <p>|</p> <p>| ---- CSS (Site CSS Directory)</p> <p>|</p> <p>| ---- Image (Site Picture Directory)</p> <p>|</p> <p>| ---- Media (Site Flash Animation Storage Directory)</p> <p>|</p> <p>| ---- IndexBak.htm (Home Original Render]</p> <p>|</p> <p>| ---- NewsBak, HTM (News page Original effect)</p> <p>|</p> <p>| ---- Index.php (Smarty Home Program File)</p> <p>|</p> <p>| ---- News.php (Smarty News Display File)</p> <p>|</p> <p>| ---- NewsList.php (Show news list)</p> <p>|</p> <p>| ---- routine description .txt (this document)</p> <p>Compared to the top two tutorials, there is no change in the Comm-directory, and the other file structures do not change. The entire site is relatively two tutorials, and the changed place has only a Comm-directory and index.php and news.php, Added a list of newsletters, you can click "Domestic News", "International News", "Entertainment News", and "Entertainment News" to view the respective "domestic news", "International News", "Entertainment News" in the "Domestic News", "Entertainment News" after INDEX.PHP execution.</p> <p>News list, let's take a look at INDEX.PHP:</p> <p>============================================================================================================================================================================================================= ====</p> <p>Index.php</p> <p>============================================================================================================================================================================================================= ====</p> <p><? PHP</p> <p>/ ***************************************************</p> <p>*</p> <p>* File name: index.php</p> <p>* Decision: Display instance program</p> <p>*</p> <p>* Servers: Master</p> <p>* Email: teacherli@163.com</p> <p>*</p> <p>*************************************************** /</p> <p>INCLUDE_ONCE ("./ Comm / MySmarty.class.php"); // contains Smarty's extended class file</p> <p>INCLUDE_ONCE ("./ Comm / AdoDB / AdoDb.inc.PHP"); / / contains the AdodB main execution file</p> <p>INCLUDE_ONCE ("./ Comm / Csubstr.inc"); // Contains Chinese Intercept class</p> <p>Define ("news_num", 5); // Defines the number of news lists</p> <p>$ smarty = new mysmarty (); // Establish a smarty instance object $ smarty</p> <p>1. $ conn = adonewconnection ("mysql"); // Initialize AdoDB</p> <p>2. $ conn-> Connect ("LocalHost", "root", "" ""); / / Connect the database</p> <p>// This will be dealt with the domestic news section</p> <p>3. $ strQuery = "SELECT INEWSID AS NEWSID, VCNEWSTILE AS NEWSID, VCNEWSTILE AS NEWSTILE,"</p> <p>4. $ r = & $ conn-> execute ($ strquery);</p> <p>5. $ Smarty-> Assign ("news_ch", $ r -> getArray (news_num));</p> <p>6. Unset ($ RS);</p> <p>// Treat the international news section</p> <p>$ strQuery = "select inewsid as newsid, vcnewstitle as newstitle from tb_news_in order by inewsid desc"; $ = & $ conn-> execute ($ strquery);</p> <p>$ Smarty-> Assign ("News_IN", $ RS-> GetArray (news_num));</p> <p>UNSET ($ RS);</p> <p>// Here you will process the entertainment news part</p> <p>$ STRQUERY = "SELECT INEWSID AS NEWSID, VCNEWSTILE AS NewStitle from TB_News_Mu Order By inewsid";</p> <p>$ r = & $ conn-> execute ($ strquery);</p> <p>$ Smarty-> Assign ("News_MU", $ RS-> GetArray (news_num));</p> <p>UNSET ($ RS);</p> <p>7. $ conn-> close ();</p> <p>// Compile and display the INDEX.TPL template under ./Templates</p> <p>$ Smarty-> Display ("INDEX.TPL");</p> <p>?></p> <p>============================================================================================================================================================================================================= ===========================</p> <p>Similarly, I added a number in key places, let's explain their meaning:</p> <p>1. Create a connection object $ conn, everyone should pay attention to it is that its initial is not in the form of $ conn = new adonepnection ($ dbtype), that is,</p> <p>AdonewConnection is not a class, you can't initialize it with New. Look at its source code you will understand, this is just a function.</p> <p>2. Don't say this? Open a new database, the host is: localhost, the username root, the password is ""</p> <p>3. A query statement, pay attention, here you want to use the field of the query using the AS keyword to re-identify, named the name of the template variable you set in the template.</p> <p>4. Use Execute to perform this query, and return a Recordset dataset</p> <p>5. Here is a method: $ RS-> GetArray ($ NUM) This is introduced on the top, it is to return $ NUM line from the data set of $ RS, resulting in a two-dimensional number identified by smarty</p> <p>According to this, ADODB automatically constructs such a structure, and in our previous example, it is used to build such an array using a loop.</p> <p>6. I don't have to say this this sentence?</p> <p>7. Turn off the relevant resources in memory.</p> <p>Everyone can see what the While statement does not appear in the entire program, the overall structure of the program is very clear, this is why AdoDB Smarty is the reasons for gold combination. But</p> <p>Said back, simple and simple questions, I don't know if you think about it, there is no control on the length of the news title shown, that is, if a news title is exceeded by the line</p> <p>Wai, it is automatically folded to the next line, then the entire layout will mess, said that everyone has made this situation to decide whether to use this, you can also use a mediation in the previous section.</p> <p>As, use a circular statement to reconstruct this two-dimensional array, so that it meets your use, how to do yourself, refer to PHPLIB, I introduced the next section ... Let's take a look at the news page Bar</p> <p>============================================================================================================================================================================================================= ===========</p> <p>News.php</p> <p>============================================================================================================================================================================================================= ===========</p> <p><? PHP</p> <p>/ ***************************************************</p> <p>*</p> <p>* File name: news.php</p> <p>* Job: News display</p> <p>*</p> <p>* Servers: Master</p> <p>* Email: teacherli@163.com</p> <p>*</p> <p>*************************************************** /</p> <p>INCLUDE_ONCE ("./ Comm / MySmarty.class.php"); // contains Smarty's extended class file</p> <p>INCLUDE_ONCE ("./ Comm / AdoDB / AdoDb.inc.PHP"); / / contains the AdodB main execution file</p> <p>$ smarty = new mysmarty (); // Establish a smarty instance object $ smarty</p> <p>$ conn = adonewconnection ("mysql"); // Initialize AdoDB</p> <p>$ conn-> connect ("localhost", "root", "" ""); // Connect the database</p> <p>$ Newsid = $ _get ["id"]; // Get news numbers</p> <p>$ Newstype = $ _get ["type"]; // to display the type of news</p> <p>Switch ($ newstype)</p> <p>{</p> <p>Case 1:</p> <p>$ DBNAME = "TB_News_CH";</p> <p>Break;</p> <p>Case 2:</p> <p>$ dbname = "tb_news_in";</p> <p>Break;</p> <p>Case 3:</p> <p>$ dbname = "tb_news_mu";</p> <p>Break;</p> <p>}</p> <p>$ strquery = "Select vcnewstitle as newstitle, ltnewscontent as newscontent from". $ dbname;</p> <p>1. $ ROW = & $ conn-> getrow ($ strquery); // Return a one-dimensional array, the subscript is a template variable name</p> <p>$ Smarty-> Display ($ ROW);</p> <p>UNSET ($ row); $ conn-> close ();</p> <p>?></p> <p>============================================================================================================================================================================================================= ===========</p> <p>Explain the key place, in fact, there is only one place value in News.php.</p> <p>1. $ conn-> getrow ($ strquery): This sentence returns a one-dimensional array, the return form is:</p> <p>$ array = ("newstitle" => "xxxx", "newscontent" => "YYYYY ...")</p> <p>I understand what if smarty will do if SmartY is used after using $ smarty ($ array)? It is equivalent to:</p> <p>$ Smarty-> Assign ("NewStitle", "XXXX");</p> <p>$ Smarty-> Assign ("NewsContent", "YYYYY ...");</p> <p>Simple, it is really simple</p> <p>Let's take a look at the newslist:</p> <p>============================================================================================================================================================================================================= ==============</p> <p>Newslist.php</p> <p>============================================================================================================================================================================================================= ==============</p> <p><? PHP</p> <p>/ ***************************************************</p> <p>*</p> <p>* File name: newslist.php</p> <p>* Job: News list display</p> <p>*</p> <p>* Servers: Master</p> <p>* Email: teacherli@163.com</p> <p>*</p> <p>*************************************************** /</p> <p>INCLUDE_ONCE ("./ Comm / MySmarty.class.php"); // contains Smarty's extended class file</p> <p>INCLUDE_ONCE ("./ Comm / AdoDB / AdoDb.inc.inc.php"); // contains Adodb main execution file $ smarty = new mysmarty (); // Establish a smarty instance object $ smarty</p> <p>$ conn = adonewconnection ("mysql"); // Initialize AdoDB</p> <p>$ conn-> connect ("localhost", "root", "" ""); // Connect the database</p> <p>$ Newsid = $ _get ["id"]; // Get news numbers</p> <p>$ Newstype = $ _get ["type"]; // to display the type of news</p> <p>Switch ($ newstype)</p> <p>{</p> <p>Case 1:</p> <p>$ TBNAME = "TB_News_CH";</p> <p>Break;</p> <p>Case 2:</p> <p>$ TBNAME = "TB_News_IN";</p> <p>Break;</p> <p>Case 3:</p> <p>$ TBNAME = "TB_News_MU";</p> <p>Break;</p> <p>}</p> <p>$ strQuery = "Select inewsid as newsid, vcnewstitle assen newstitle from". $ tbname;</p> <p>1. $ r = & $ conn-> getall ($ strquery);</p> <p>2. $ Smarty-> Assign ("NewSType", $ newstype); // This sentence is the link service in the news list</p> <p>3. $ SMARTY-> Assign ("Newslist", $ RS);</p> <p>UNSET ($ RS);</p> <p>$ conn-> close ();</p> <p>$ Smarty-> Display ("newslist.tpl");</p> <p>?></p> <p>============================================================================================================================================================================================================= ==============</p> <p>Take it separately:</p> <p>1. Getall ($ strquery): This function is a good stuff, its role is to combine all the data combinations from $ STRQUERY to be a two-dimensional array that can be identified by Smarty.</p> <p>Remember: It returns a two-dimensional array instead of a recordset, you can use it directly in the program.</p> <p>2. Here is to give the news header to make a link to get parameter type = xx</p> <p>postscript:</p> <p>Everyone has several places to pay attention to using AdoDB:</p> <p>1. Initialization: Initialization does not use new because it is not an object</p> <p>2. Method: Basically, each method is a name that is mixed with uppercase letters. This seems to be different from * NIX habits, and it is different from the overall style of PHP.</p> <p>Note that the case is written here.</p> <p>Ok, this SMARTY's series of tutorials is basically completed here. Even if the primary tutorial is to throw the jade, I hope more masters will write more experience. Everyone</p> <p>Common improvement! Because the company is not allowed to open QQ, if everyone wants to communicate with me, please add my MSN: teacherli@ceua.org, welcome everyone to discuss!</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-84137.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="84137" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.035</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'B8QTIJvNYPZxVFNUl0Hmn_2FwolZdVQZeQOfRnqiHGU6gOz_2FmHbV4m_2Bqu9HGBUnH_2FewMvSr_2FKia3j0VTYjS4Kv9A_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>