Smarty循环实例详解
在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代码
- $suoyin1 = array ( '1000' , '1001' , '1002' );
- $smarty ->assign( 'suoyin1' , $suoyin1 );
XML/HTML代码
- {div name = n1 loop =$suoyin1} >
- id: {$suoyin1[n1]} >
- {/div} >
OUTPUT:
id:1000 id:1001 id:1002
2、如果多个数组包含相同数量的元素,可以将他们合并写出,这里说明了loop 只代
表循环次数。
PHP代码
- $id = array ( '1000' , '1001' , '1002' );
- $name = array ( 'John Smith' , 'Jack Jones' , 'Jane Munson' );
- $address = array ( '253 N 45th' , '417 Mulberry ln' , '5605 apple st' );
- $smarty ->assign( 'id' , $id );
- $smarty ->assign( 'name' , $name );
- $smarty ->assign( 'address' , $address );
XML/HTML代码
- {div name = h1 loop =$name} >
- id: {$id[h1]} >
- name: {$name[h1]} >
- address: {$address[h1]} >
- {/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代码
- $bst = array ( 'id' => '1000' , 'name' => 'John Smith' , 'address' => '253 N 45th' );
- $smarty ->assign( 'bst' , $bst );
XML/HTML代码
- id: {$bst.id} >
- name: {$bst.name} >
- address: {$bst.address} >
OUTPUT:
id:1000 name:John Smith address:253 N 45th
4、二维关联数组,用到基本循环。
PHP代码
- $erwei = array ( array ( 'id' => '1000' , 'name' => 'John Smith' , 'address' => '253 N 45th' ),
- array ( 'id' => '1001' , 'name' => 'Jack Jones' , 'address' => '417 Mulberry ln' ),
- array ( 'id' => '1002' , 'name' => 'Jane Munson' , 'address' => '5605 apple st' ));
- $smarty ->assign( 'erwei' , $erwei );
XML/HTML代码
- {div name = h2 loop =$erwei} >
- id: {$erwei[h2].id} >
- name: {$erwei[h2].name} >
- address: {$erwei[h2].address} >
- {/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代码
- {div name = customer loop =$custid} >
- id: {$custid[customer]} >
- {divelse} >
- there are no values in $custid.
- {/div} >
6、div 多层循环
PHP代码
- $id = array (1001,1002,1003);
- $smarty ->assign( 'custid' , $id );
- $fullnames = array ( 'John Smith' , 'Jack Jones' , 'Jane Munson' );
- $smarty ->assign( 'name' , $fullnames );
- $addr = array ( '253 N 45th' , '417 Mulberry ln' , '5605 apple st' );
- $smarty ->assign( 'address' , $addr );
- $types = array (
- array ( 'home phone' , 'cell phone' , 'e-mail' ),
- array ( 'home phone' , 'web' ),
- array ( 'cell phone' )
- );
- $smarty ->assign( 'contact_type' , $types );
- $info = array (
- array ( '555-555-5555' , '666-555-5555' , 'john@myexample.com' ),
- array ( '123-456-4' , 'www.example.com' ),
- array ( '0457878' )
- );
- $smarty ->assign( 'contact_info' , $info );
XML/HTML代码
- {div name = customer loop =$custid} >
- id: {$custid[customer]} >
- name: {$name[customer]} >
- address: {$address[customer]} >
- {div name = contact loop =$contact_type[customer]} >
- {$contact_type[customer][contact]} > : {$contact_info[customer][contact]} >
- {/div} >
- {/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代码
- $mu = array ( array ( 'id' => '1000' ,
- 'name' => 'John Smith' ,
- 'address' => '253 N 45th' ,
- 'contact' => array ( 'home' => '555-55-555' , 'bell' => '1111111' )
- ),
- array ( 'id' => '1001' ,
- 'name' => 'Jack Jones' ,
- 'address' => '417 Mulberry ln' ,
- 'contact' => array ( 'home' => '444-44-444' , 'bell' => '2222222' )
- ),
- array ( 'id' => '1002' ,
- 'name' => 'Jane Munson' ,
- 'address' => '5605 apple st' ,
- 'contact' => array ( 'home' => '333-33-333' , 'bell' => '4444444' )
- )
- );
- $smarty ->assign( 'mu' , $mu );
XML/HTML代码
- {div name = sec1 loop =$mu} >
- id: {$mu[sec1].id} >
- name: {$mu[sec1].name} >
- address: {$mu[sec1].address} >
- array: {$mu[sec1].contact} >
- home phone: {$mu[sec1].contact.home} >
- cell phone: {$mu[sec1].contact.bell} >
- ====================================================================
- {/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代码
- $forum = array ( array ( "category_id" => 1,
- "category_name" => "公告区" ,
- "topic" => array (
- array ( "topic_id" => 1, "topic_name" => "站务公告" )
- )
- ),
- array ( "category_id" => 2,
- "category_name" => "文学专区" ,
- "topic" => array (
- array ( "topic_id" => 2, "topic_name" => "好书介绍" ),
- array ( "topic_id" => 3, "topic_name" => "奇文共赏" )
- )
- ),
- array ( "category_id" => 3,
- "category_name" => "计算机专区" ,
- "topic" => array (
- array ( "topic_id" => 4, "topic_name" => "硬件外围" ),
- array ( "topic_id" => 5, "topic_name" => "软件讨论" )
- )
- )
- );
- $smarty ->assign( "forum" , $forum );
XML/HTML代码
- {div name = sec1 loop =$forum} >
- {$forum[sec1].category_name} >
- {div name = sec2 loop =$forum[sec1].topic} >
- {$forum[sec1].topic[sec2].topic_name} >
- {/div} >
- {/div} >
OUTPUT:
公告区
站务公告
文学专区
好书介绍
奇文共赏
计算机专区
硬件外围
软件讨论
9、div 和forech 循环的用法比较
PHP代码
- $array2 = array (
- array ( "index1" => "data1-1" , "index2" => "data1-2" , "index3" => "data1-3" ),
- array ( "index1" => "data2-1" , "index2" => "data2-2" , "index3" => "data2-3" ),
- array ( "index1" => "data3-1" , "index2" => "data3-2" , "index3" => "data3-3" ),
- array ( "index1" => "data4-1" , "index2" => "data4-2" , "index3" => "data4-3" ),
- array ( "index1" => "data5-1" , "index2" => "data5-2" , "index3" => "data5-3" )
- );
- $smarty ->assign( "array2" , $array2 );
XML/HTML代码
- foreach
- {foreach item = index2 from =$array2} >
- {foreach key = key2 item = item2 from =$index2} >
- {$key2} > : {$item2} >
- {/foreach} >
- {/foreach} >
- div
- {div name = sec2 loop =$array2} >
- index1: {$array2[sec2].index1} >
- index2: {$array2[sec2].index2} >
- index3: {$array2[sec2].index3} >
- {/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代码
- $conn =mysql_connect( 'localhost' , 'root' , 'pass' );
- mysql_select_db( 'dbname' , $conn );
- mysql_query( "SET NAMES 'GBK'" );
- $sql = "SELECT * FROM posts" ;
- $result =mysql_query( $sql );
- $posts =mysql_fetch_array( $result );
- $smarty ->assign( 'posts' , $posts );
XML/HTML代码
- {div name = h loop =$posts} >
- 标题: {$posts[h].ptitle} >
- 内容: {$posts[h].pcontent} >
- 发布时间: {$posts[h].ptime|date_format:"%Y-%m-%d %H:%M:%S"} >
- {/div} >
OUTPUT:
标题:标题一内容:内容一发布时间:2008-1-1 10:20:15
标题:标题二内容:内容二发布时间:2008-4-1 8:1:12
表category 文章分类表
cid 文章分类ID
cname 分类名
这里要循环出所有分类,以及分类下的所有帖子,也就是一个嵌套循环。
PHP
PHP代码
- $sql = "SELECT * FROM category" ;
- $result =mysql_query( $sql );
- $cat = array (); //第一层数组
- while ( $array1 =mysql_fetch_array( $result )){ //第一层数据
- $sql2 = "SELECT * FROM posts WHERE cid={$cat[cid]}" ;
- $rs =mysql_query( $sql2 );
- $posts = array (); //第二层数组
- while ( $array2 =mysql_fetch_array( $rs )){ //第二层数据
- array_push ( $posts , $array2 ); //压入数组posts 中
- }
- $array1 [ 'posts' ]= $posts ; // 把第二层数组指定为第一层数据中的一个成员
- array_push ( $cat , $array1 ); //把数据放入数组$cat 中
- }
- $smarty ->assign( 'cat' , $cat );
XML/HTML代码
- {div name = sec1 loop =$cat} >
- 分类: {$cat[sec1].cname} >
- {div name = sec2 loop =$cat[sec1].posts} >
- 标题: {$cat[sec1].posts[sec2].ptitle} >
- 内容: {$cat[sec1].posts[sec2].pcontent} >
- 发布时间: {$cat[sec1].posts[sec2].pcontent|date_format:"%Y-%m-%d %H:%M:%S"} >
- {/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

Heiße KI -Werkzeuge

Undresser.AI Undress
KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover
Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool
Ausziehbilder kostenlos

Clothoff.io
KI-Kleiderentferner

Video Face Swap
Tauschen Sie Gesichter in jedem Video mühelos mit unserem völlig kostenlosen KI-Gesichtstausch-Tool aus!

Heißer Artikel

Heiße Werkzeuge

Notepad++7.3.1
Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version
Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1
Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6
Visuelle Webentwicklungstools

SublimeText3 Mac-Version
Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Heiße Themen



PHP 8.4 bringt mehrere neue Funktionen, Sicherheitsverbesserungen und Leistungsverbesserungen mit einer beträchtlichen Menge an veralteten und entfernten Funktionen. In dieser Anleitung wird erklärt, wie Sie PHP 8.4 installieren oder auf PHP 8.4 auf Ubuntu, Debian oder deren Derivaten aktualisieren. Obwohl es möglich ist, PHP aus dem Quellcode zu kompilieren, ist die Installation aus einem APT-Repository wie unten erläutert oft schneller und sicherer, da diese Repositorys in Zukunft die neuesten Fehlerbehebungen und Sicherheitsupdates bereitstellen.

Wenn Sie ein erfahrener PHP-Entwickler sind, haben Sie möglicherweise das Gefühl, dass Sie dort waren und dies bereits getan haben. Sie haben eine beträchtliche Anzahl von Anwendungen entwickelt, Millionen von Codezeilen debuggt und eine Reihe von Skripten optimiert, um op zu erreichen

Visual Studio Code, auch bekannt als VS Code, ist ein kostenloser Quellcode-Editor – oder eine integrierte Entwicklungsumgebung (IDE) –, die für alle gängigen Betriebssysteme verfügbar ist. Mit einer großen Sammlung von Erweiterungen für viele Programmiersprachen kann VS Code c

JWT ist ein offener Standard, der auf JSON basiert und zur sicheren Übertragung von Informationen zwischen Parteien verwendet wird, hauptsächlich für die Identitätsauthentifizierung und den Informationsaustausch. 1. JWT besteht aus drei Teilen: Header, Nutzlast und Signatur. 2. Das Arbeitsprinzip von JWT enthält drei Schritte: Generierung von JWT, Überprüfung von JWT und Parsingnayload. 3. Bei Verwendung von JWT zur Authentifizierung in PHP kann JWT generiert und überprüft werden, und die Funktionen und Berechtigungsinformationen der Benutzer können in die erweiterte Verwendung aufgenommen werden. 4. Häufige Fehler sind Signaturüberprüfungsfehler, Token -Ablauf und übergroße Nutzlast. Zu Debugging -Fähigkeiten gehört die Verwendung von Debugging -Tools und Protokollierung. 5. Leistungsoptimierung und Best Practices umfassen die Verwendung geeigneter Signaturalgorithmen, das Einstellen von Gültigkeitsperioden angemessen.

Dieses Tutorial zeigt, wie XML -Dokumente mit PHP effizient verarbeitet werden. XML (Extensible Markup-Sprache) ist eine vielseitige textbasierte Markup-Sprache, die sowohl für die Lesbarkeit des Menschen als auch für die Analyse von Maschinen entwickelt wurde. Es wird üblicherweise für die Datenspeicherung ein verwendet und wird häufig verwendet

Eine Zeichenfolge ist eine Folge von Zeichen, einschließlich Buchstaben, Zahlen und Symbolen. In diesem Tutorial wird lernen, wie Sie die Anzahl der Vokale in einer bestimmten Zeichenfolge in PHP unter Verwendung verschiedener Methoden berechnen. Die Vokale auf Englisch sind a, e, i, o, u und sie können Großbuchstaben oder Kleinbuchstaben sein. Was ist ein Vokal? Vokale sind alphabetische Zeichen, die eine spezifische Aussprache darstellen. Es gibt fünf Vokale in Englisch, einschließlich Großbuchstaben und Kleinbuchstaben: a, e, ich, o, u Beispiel 1 Eingabe: String = "TutorialPoint" Ausgabe: 6 erklären Die Vokale in der String "TutorialPoint" sind u, o, i, a, o, ich. Insgesamt gibt es 6 Yuan

Statische Bindung (statisch: :) implementiert die späte statische Bindung (LSB) in PHP, sodass das Aufrufen von Klassen in statischen Kontexten anstatt Klassen zu definieren. 1) Der Analyseprozess wird zur Laufzeit durchgeführt.

Was sind die magischen Methoden von PHP? Zu den magischen Methoden von PHP gehören: 1. \ _ \ _ Konstrukt, verwendet, um Objekte zu initialisieren; 2. \ _ \ _ Destruct, verwendet zur Reinigung von Ressourcen; 3. \ _ \ _ Call, behandeln Sie nicht existierende Methodenaufrufe; 4. \ _ \ _ GET, Implementieren Sie den dynamischen Attributzugriff; 5. \ _ \ _ Setzen Sie dynamische Attributeinstellungen. Diese Methoden werden in bestimmten Situationen automatisch aufgerufen, wodurch die Code -Flexibilität und -Effizienz verbessert werden.
