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.