A question about smarty

Source: Internet
Author: User
The new user asks a question about smarty, which is about to obtain the latest five pieces of information from the database. each piece of information has five fields (id, name, cont, job, date ), however, only the three fields (cont, name, date) are displayed in smarty to find out how to write them in tpl. why do I ask a new person about smarty?
This is probably the case.

Obtain the latest five pieces of information from the database. each piece of information has five fields (id, name, cont, job, date ), however, only the three fields (cont, name, date) are displayed through smarty, and how to write them in tpl is solved, why are the 5 messages that I cyclically output all the same (5th messages)

I wrote it like this.
* ******** Contro ******
For ($ I = 0; $ I <5; $ I ++ ){
$ Row = $ page-> res_array [$ I]; // All fields obtained here
$ Smarty-> assign ("arr", $ row );
}
* ********** Tpl *******
{Foreach from = $ arr item = item1} {$ arr. cont} {$ arr. name} at {$ arr. date} {/foreach}

------ Solution --------------------
* ******** Contro ******
$ Arr = array ();
For ($ I = 0; $ I <5; $ I ++ ){
$ Row = $ page-> res_array [$ I]; // All fields obtained here
$ Arr [] = $ row;
}
$ Smarty-> assign ("arr", $ arr );
* ********** Tpl *******
{Foreach from = $ arr item = item1} {$ arr. cont} {$ arr. name} at {$ arr. date} {/foreach}
------ Solution --------------------
Upstairs is also wrong
You only need to put $ smarty-> assign ("arr", $ row); this sentence can be placed outside the loop. do not $ arr [] = $ row;

{Foreach from = $ arr item = item1} {item1.cont} {item1.name} at {item1.date} {/foreach}

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.