Php smarty template foreach loop block and cache technology

Source: Internet
Author: User
Tags smarty template

Function get_product_tree ()
{
$ SQL = "select * from ecs_category ";
$ Rs = $ GLOBALS ['DB']-> getOne ($ SQL );
Foreach ($ rs AS $ row)
  {
$ Rows ['cat _ id'] = $ row ['cat _ id'];
$ Rows ['cat _ name'] = $ row ['cat _ name'];
  
  }
Return $ rows;
  }

The above is my code. My template page is
<! -- {Foreach from = $ product item = child} -->
<Li> $ child. cat_name} </li>
<! -- {/Foreach} -->

How can this problem be solved ??? Question added:
<! -- {Foreach from = $ product item = child} -->
<Li >{$ child. cat_name} </li>
<! -- {/Foreach} -->


<! -- {Foreach from = $ product item = child} -->
<Li> $ child. cat_name} </li>
<! -- {/Foreach} -->
-------------------------------------------

<Li> is a {Mark missing near a tag?

 
Report
Supplemented by the respondent <! -- {Foreach from = $ product item = child} -->
<Li> <! -- {$ Child. cat_name} --> </li>

 

Working Principle of PHP Cache technology: first look at the data cache function provided by adodb: 1 <? Php 2 include ('adodb. inc. php '); # load code common to ADOdb 3 $ ADODB_CACHE_DIR ='/usr/ADODB_cache '; 4 $ conn = & ADONewConnection ('mysql '); # create a connection 5 $ conn-> PConnect ('http://www.aboutstudy.net ', 'userid', '', 'agora'); # connect to MySQL, agora db 6 $ SQL = 'SELECT mermername, CustomerID from MERS mers '; 7 $ rs = $ conn-> CacheExecute (15, $ SQL); 8?> As shown above, each time data is queried, the corresponding results are serialized and saved to the file. In the future, the same query statement can be obtained from the cache file instead of directly querying the database. Let's take a look at the page cache function provided by Smarty: 1 <? Php 2 require ('smarty. class. Php'); 3 $ Smarty = new smarty; 4 $ Smarty-> caching = true; 5if (! $ Smarty-> is_cached ('index. tpl ') {6 // No cache available, do variable assignments here. 7 $ contents = get_database_contents (); 8 $ smarty-> assign ($ contents); 9} 10 $ smarty-> display ('index. tpl '); 11?> 12 as shown above, each time you access the page, you will first check whether the corresponding cache exists. If it does not exist, connect to the database and obtain the data. After assigning values to template variables, the page is displayed, the cache file is generated at the same time, so that the cache file will play a role the next time you access it, instead of executing the if block data query statement. Of course, there are many things to consider in actual use, such as setting the validity period and cache group. For details, refer to the related sections on cache (caching) in the Smarty manual. The two popular PHP component caching methods have different focuses. For the Adodb cache, it caches data and for the Smarty cache, it caches pages. There are many other components that provide the caching function (for example, PEAR: Cache_Lite). The specific analysis of the specific solution used in actual programming may also be used in combination.

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.