MySQL obtains the number of read rows and smarty traverses the array.

Source: Internet
Author: User
Tags smarty template

Today, when processing my website, I encountered the problem of smarty traversing two-dimensional arrays. $ row ['id'] was rejected. It seems that the syntax of smarty is not exactly the same as that of PHP. I finally solved the problem by checking the information on the Internet. Today I will share two more points, as shown below.
I. MySQL executes one operation to obtain the total number of data rows and data read.

 
$ Query = "select SQL _calc_found_rows * from title where ID> 1000 limit 10;"; $ result = $ db-> query ($ query ); $ line = $ db-> fetch_row ($ db-> query ("select found_rows ();"));

The aboveCodeIt is a function encapsulated in my own project to obtain the number of Recommendation sites. You will see SQL _calc_found_rows, which is a feature of MySQL. If you query it in an SQL statement, MySQL will cache this item during execution, then you can use the select found_rows function to obtain the variable value of the number of rows obtained from the last query in the cache. This means that you have used only one database query operation to obtain data and count (*), which is worth promoting.

Ii. traverse the array in smarty

 
<? PHP $ items_list = array (23 => array ('no' => 2456, 'label' => 'salad'), 96 => array ('no' => 4889, 'label' => 'cream'); $ smarty-> assign ('items ', $ items_list);?>

The above Code creates a false array and assigns it to our template so that the smarty template can be filled in. For the above array, we have to remember how to parse it in the smarty template by querying the SQL database to return the most common form of array. The following code is used:

 
<Ul> {foreach from = $ items key = myid item = I} <li> <a href = "item. php? Id = {$ myid} ">{$ I. No }:{$ I. Label} </Li >{/ foreach} </ul>

The preceding keys and items are the parameters of smarty foreach traversal. One key representing the key-value pair is rowname, And the other value is the value we want. When using foreach for traversal, the second bit of a two-dimensional array gets its value. It is not worthwhile to use $ I ['id'], a traditional PHP, to obtain the array. It can be used in smarty. to access the values of two-dimensional arrays, this is cool, as if smarty made the array into an anonymous class for us. The specific implementation did not look at it, so we have time to look at it.

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.