Home php教程 php手册 Smarty循环实例详解

Smarty循环实例详解

Jun 06, 2016 pm 08:01 PM
php smarty Example application cycle Detailed explanation

在Smarty应用,常常会遇到循环问题,在这里做一个详细的说明。首先是参数说明: loop 属性: the loop variable only determines the number of times to loop loop 循环所赋的变量仅决定循环的次数,可以任意!!! div 属性: the name of the div can be

在Smarty应用,常常会遇到循环问题,在这里做一个详细的说明。首先是参数说明:
loop 属性:
the loop variable only determines the number of times to loop
loop 循环所赋的变量仅决定循环的次数,可以任意!!!
div 属性:
the name of the div can be anything you like,and it is used to reference
the data within the div
div 属性的值可以是任意的,而且在循环中只能使用该名字作为替换
数据,也就是[div]里的值。
下面给出示例来具体说明应用方法(PHP 代表PHP 部分代码,HTML 代表模板
内容,OUTPUT 代表输出内容):
1、一维索引数组循环。

PHP代码

  1. $suoyin1 = array ( '1000' , '1001' , '1002' );   
  2. $smarty ->assign( 'suoyin1' , $suoyin1 );   

 

XML/HTML代码

  1. {div  name = n1   loop =$suoyin1} >   
  2. id:  {$suoyin1[n1]} >   
  3. {/div} >   


OUTPUT:
id:1000 id:1001 id:1002
2、如果多个数组包含相同数量的元素,可以将他们合并写出,这里说明了loop 只代
表循环次数。

PHP代码

  1. $id = array ( '1000' , '1001' , '1002' );   
  2. $name = array ( 'John Smith' , 'Jack Jones' , 'Jane Munson' );   
  3. $address = array ( '253 N 45th' , '417 Mulberry ln' , '5605 apple st' );   
  4. $smarty ->assign( 'id' , $id );   
  5. $smarty ->assign( 'name' , $name );   
  6. $smarty ->assign( 'address' , $address );  


XML/HTML代码

  1. {div  name = h1   loop =$name} >   
  2. id: {$id[h1]} >   
  3. name: {$name[h1]} >   
  4. address: {$address[h1]} >   
  5. {/div} >   


OUTPUT:
id:1000 name:John Smith address:253 N 45th
id:1001 name:Jack Jones address:417 Mulberry ln
id:1002 name:Jane Munson address:5605 apple st
3、一维关联数组不要循环。

PHP代码

  1. $bst = array ( 'id' => '1000' , 'name' => 'John Smith' , 'address' => '253 N 45th' );   
  2. $smarty ->assign( 'bst' , $bst );  


XML/HTML代码

  1. id: {$bst.id} >   
  2. name: {$bst.name} >   
  3. address: {$bst.address} >   


OUTPUT:
id:1000 name:John Smith address:253 N 45th
4、二维关联数组,用到基本循环。

PHP代码

  1. $erwei = array ( array ( 'id' => '1000' , 'name' => 'John Smith' , 'address' => '253 N 45th' ),   
  2. array ( 'id' => '1001' , 'name' => 'Jack Jones' , 'address' => '417 Mulberry ln' ),   
  3. array ( 'id' => '1002' , 'name' => 'Jane Munson' , 'address' => '5605 apple st' ));   
  4. $smarty ->assign( 'erwei' , $erwei );  


XML/HTML代码

  1. {div  name = h2   loop =$erwei} >   
  2. id: {$erwei[h2].id} >   
  3. name: {$erwei[h2].name} >   
  4. address: {$erwei[h2].address} >   
  5. {/div} >   


OUTPUT:
id:1000 name:John Smith address:253 N 45th
id:1001 name:Jack Jones address:417 Mulberry ln
id:1002 name:Jane Munson address:5605 apple st
5、divelse 控制当没变量的时候显示,比如你打开的文章(产品)不存在或已删
除!

XML/HTML代码

  1. {div  name = customer   loop =$custid} >   
  2. id:  {$custid[customer]} >   
  3. {divelse} >   
  4. there are no values in $custid.   
  5. {/div} >   


6、div 多层循环

PHP代码

  1. $id  =  array (1001,1002,1003);   
  2. $smarty ->assign( 'custid' , $id );   
  3. $fullnames  =  array ( 'John Smith' , 'Jack Jones' , 'Jane Munson' );   
  4. $smarty ->assign( 'name' , $fullnames );   
  5. $addr  =  array ( '253 N 45th' '417 Mulberry ln' '5605 apple st' );   
  6. $smarty ->assign( 'address' , $addr );   
  7. $types  =  array (   
  8. array 'home phone' 'cell phone' 'e-mail' ),   
  9. array 'home phone' 'web' ),   
  10. array 'cell phone' )   
  11. );   
  12. $smarty ->assign( 'contact_type' $types );   
  13. $info  =  array (   
  14. array ( '555-555-5555' '666-555-5555' 'john@myexample.com' ),   
  15. array '123-456-4' 'www.example.com' ),   
  16. array '0457878' )   
  17. );   
  18. $smarty ->assign( 'contact_info' $info );   

 

XML/HTML代码

  1. {div  name = customer   loop =$custid} >   
  2. id:  {$custid[customer]} >   
  3. name:  {$name[customer]} >   
  4. address:  {$address[customer]} >   
  5. {div  name = contact   loop =$contact_type[customer]} >   
  6. {$contact_type[customer][contact]} > {$contact_info[customer][contact]} >   
  7. {/div} >   
  8. {/div} >   

OUTPUT:
id: 1001
name: John Smith
address: 253 N 45th
home phone: 555-555-5555
cell phone: 666-555-5555
e-mail:5656@126.com
id: 1002
name: 232
address: 417 Mulberry ln
home phone: 123-456-4
web:www.example.com
3
id: 1003
name: Jane Munson
address: 5605 apple st
cell phone: 0457878
7、多层嵌套循环:这里联系方式还嵌套了一个一维数组,有cell phone、home phone
和email。注意嵌套的一维数组不要循环!!内嵌套写法$mu[sec1].contact.home。

PHP代码

  1. $mu = array ( array ( 'id' => '1000' ,   
  2. 'name' => 'John Smith' ,   
  3. 'address' => '253 N 45th' ,   
  4. 'contact' => array ( 'home' => '555-55-555' , 'bell' => '1111111' )   
  5. ),   
  6. array ( 'id' => '1001' ,   
  7. 'name' => 'Jack Jones' ,   
  8. 'address' => '417 Mulberry ln' ,   
  9. 'contact' => array ( 'home' => '444-44-444' , 'bell' => '2222222' )   
  10. ),   
  11. array ( 'id' => '1002' ,   
  12. 'name' => 'Jane Munson' ,   
  13. 'address' => '5605 apple st' ,   
  14. 'contact' => array ( 'home' => '333-33-333' , 'bell' => '4444444' )   
  15. )   
  16. );   
  17. $smarty ->assign( 'mu' , $mu );   

 

XML/HTML代码

  1. {div  name = sec1   loop =$mu} >   
  2. id: {$mu[sec1].id} >   
  3. name: {$mu[sec1].name} >   
  4. address: {$mu[sec1].address} >   
  5. array: {$mu[sec1].contact} >   
  6. home phone: {$mu[sec1].contact.home} >   
  7. cell phone: {$mu[sec1].contact.bell} >   
  8. ====================================================================   
  9. {/div} >   

OUTPUT:
id:1000
name: John Smith
address:253 N 45th
array:Array
home phone:555-55-555
cell phone:1111111 =================================================id:1001
name:Jack Jones
address:417 Mulberry ln
array:Array
home phone:444-44-444
cell phone:2222222 ================================================id:1002
name:Jane Munson
address:5605 apple st
array:Array
home phone:333-33-333
cell phone:4444444
8 、多层嵌套循环。内部嵌套二维数组, 加个循环!! 内嵌套写法
$forum[sec1].topic[sec2].topic_name。

PHP代码

  1. $forum  =  array ( array ( "category_id"  => 1,   
  2. "category_name"  =>  "公告区" ,   
  3. "topic"  =>  array (   
  4. array ( "topic_id"  => 1,  "topic_name"  =>  "站务公告" )   
  5. )   
  6. ),   
  7. array ( "category_id"  => 2,   
  8. "category_name"  =>  "文学专区" ,   
  9. "topic"  =>  array (   
  10. array ( "topic_id"  => 2,  "topic_name"  =>  "好书介绍" ),   
  11. array ( "topic_id"  => 3,  "topic_name"  =>  "奇文共赏" )   
  12. )   
  13. ),   
  14. array ( "category_id"  => 3,   
  15. "category_name"  =>  "计算机专区" ,   
  16. "topic"  =>  array (   
  17. array ( "topic_id"  => 4,  "topic_name"  =>  "硬件外围" ),   
  18. array ( "topic_id"  => 5,  "topic_name"  =>  "软件讨论" )   
  19. )   
  20. )   
  21. );   
  22. $smarty ->assign( "forum" $forum );   

 

XML/HTML代码

  1. {div  name = sec1   loop =$forum} >   
  2. {$forum[sec1].category_name} >   
  3. {div  name = sec2   loop =$forum[sec1].topic} >   
  4. {$forum[sec1].topic[sec2].topic_name} >   
  5. {/div} >   
  6. {/div} >   


OUTPUT:
公告区
站务公告
文学专区
好书介绍
奇文共赏
计算机专区
硬件外围
软件讨论
9、div 和forech 循环的用法比较

PHP代码

  1. $array2  =  array (   
  2. array ( "index1"  =>  "data1-1" "index2"  =>  "data1-2" "index3"  =>  "data1-3" ),   
  3. array ( "index1"  =>  "data2-1" "index2"  =>  "data2-2" "index3"  =>  "data2-3" ),   
  4. array ( "index1"  =>  "data3-1" "index2"  =>  "data3-2" "index3"  =>  "data3-3" ),   
  5. array ( "index1"  =>  "data4-1" "index2"  =>  "data4-2" "index3"  =>  "data4-3" ),   
  6. array ( "index1"  =>  "data5-1" "index2"  =>  "data5-2" "index3"  =>  "data5-3" )   
  7. );   
  8. $smarty ->assign( "array2" $array2 );   

 

XML/HTML代码

  1. foreach   
  2. {foreach  item = index2   from =$array2} >   
  3. {foreach  key = key2   item = item2   from =$index2} >   
  4. {$key2} > {$item2} >   
  5. {/foreach} >   
  6. {/foreach} >   
  7. div   
  8. {div  name = sec2   loop =$array2} >   
  9. index1:  {$array2[sec2].index1} >   
  10. index2:  {$array2[sec2].index2} >   
  11. index3:  {$array2[sec2].index3} >   
  12. {/div} >   

OUTPUT:
index1: data1-1 index2: data1-2 index3: data1-3 index1: data2-1 index2: data2-2 index3: data2-3
index1: data3-1 index2: data3-2 index3: data3-3 index1: data4-1 index2: data4-2 index3: data4-3
index1: data5-1 index2: data5-2 index3: data5-3
10、显示数据库的资料
表posts 文章列表
字段
pid INT(10) AUTO_INCREMENT 文章ID
cid INT(10) 分类ID
ptitle varchar(100) 文章标题
pcontent text 文章内容
ptime INT(11) 文章发表日期
和简单关联二维数组一样。

PHP代码

  1. $conn =mysql_connect( 'localhost' , 'root' , 'pass' );   
  2. mysql_select_db( 'dbname' , $conn );   
  3. mysql_query( "SET NAMES 'GBK'" );   
  4. $sql = "SELECT * FROM posts" ;   
  5. $result =mysql_query( $sql );   
  6. $posts =mysql_fetch_array( $result );   
  7. $smarty ->assign( 'posts' , $posts );   

 

XML/HTML代码

  1. {div  name = h   loop =$posts} >   
  2. 标题:  {$posts[h].ptitle} >   
  3. 内容:  {$posts[h].pcontent} >   
  4. 发布时间:  {$posts[h].ptime|date_format:"%Y-%m-%d %H:%M:%S"} >   
  5. {/div} >   


OUTPUT:
标题:标题一内容:内容一发布时间:2008-1-1 10:20:15
标题:标题二内容:内容二发布时间:2008-4-1 8:1:12
表category 文章分类表
cid 文章分类ID
cname 分类名
这里要循环出所有分类,以及分类下的所有帖子,也就是一个嵌套循环。
PHP

PHP代码

  1. $sql = "SELECT * FROM category" ;   
  2. $result =mysql_query( $sql );   
  3. $cat = array ();  //第一层数组   
  4. while ( $array1 =mysql_fetch_array( $result )){  //第一层数据   
  5. $sql2 = "SELECT * FROM posts WHERE cid={$cat[cid]}" ;   
  6. $rs =mysql_query( $sql2 );   
  7. $posts = array ();  //第二层数组   
  8. while ( $array2 =mysql_fetch_array( $rs )){  //第二层数据   
  9. array_push ( $posts , $array2 );  //压入数组posts 中   
  10. }   
  11. $array1 [ 'posts' ]= $posts // 把第二层数组指定为第一层数据中的一个成员   
  12. array_push ( $cat , $array1 );  //把数据放入数组$cat 中   
  13. }   
  14. $smarty ->assign( 'cat' , $cat );   

 

XML/HTML代码

  1. {div  name = sec1   loop =$cat} >   
  2. 分类: {$cat[sec1].cname} >   
  3. {div  name = sec2   loop =$cat[sec1].posts} >   
  4. 标题:  {$cat[sec1].posts[sec2].ptitle} >   
  5. 内容:  {$cat[sec1].posts[sec2].pcontent} >   
  6. 发布时间:  {$cat[sec1].posts[sec2].pcontent|date_format:"%Y-%m-%d %H:%M:%S"} >   
  7. {/div} >   

OUTPUT:
分类:分类1
标题:标题一内容:内容一发布时间:2008-1-1 10:20:15
标题:标题二内容:内容二发布时间:2008-4-1 8:1:12
分类:分类2
标题:标题三内容:内容三发布时间:2008-1-1 10:20:15
标题:标题四内容:内容四发布时间:2008-4-1 8:1:12

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles