问个 smarty section 用法

leec1981 2008-11-13 02:29:40
for($i=0;$i<count($array);$i=$i+2)
{
echo $array[$i];
echo $array[$i+1];
}
这种效果

$array = array(array('id'=>1,'name'='name1'),array('id'=>2,'name'='name2'),array('id'=>3,'name'='name3'));
用section 怎么处理?
{section name=aa loop=$array step=2}
$array[aa].id
$array[aa].name

$array[$smarty.section.aa.index].id
$array[$smarty.section.aa.index].name

下面这样的不行
$array[$smarty.section.aa.index+1].id
$array[$smarty.section.aa.index+1].name

{/section}



简单的说,就是想在模板中得到第二个的值
...全文
402 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
程序猿之殇 2008-11-13
  • 打赏
  • 举报
回复
大致看了你的问题,
index_next虽然会输出下一条数据,但并不能影响循环,所以你这种思路可能不行.
leec1981 2008-11-13
  • 打赏
  • 举报
回复
变换了一种处理方式,将category和products存放到同一个数组中,通过section嵌套基本上解决问题。。

谢谢大家的帮助。。。
leec1981 2008-11-13
  • 打赏
  • 举报
回复
{foreach item=category from=$categories} 
<tr>
<td bgcolor="#COCOCO" colspan="2"> <strong>{$category.categoryj} </strong> </td>
</tr>
<tr>
<td colspan="2">
<table width="100%" border="1" cellspacing="0" cellpading="5">
{section name=product loop=$products}
{if $products[product].categoryid eq $category.categoryid}
<tr height="70">
<td rowspan="2" width="15%"> <a href="product.html">Thumbnail1 </a> </td>
<td colspan="2" width="35%">
{$products[$smarty.section.product.index].productname} <br>
{$products[product].description|nl2br}
</td>
{if $products[$smarty.section.product.index_next].categoryid eq $category.categoryid}
<td rowspan="2"> <a href="product.html">Thumbnail2 </a> </td>
<td colspan="2">
{$products[$smarty.section.product.index_next].productname} <br>
{$products[$smarty.section.product.index_next].description|nl2br}
</td>
{else}
<td rowspan="2">  </td>
<td colspan="2">  </td>
{/if}
</tr>
<tr>
<td align="center">Price: {$products[product].price} </td>
<td align="center" nowrap="nowrap"> <a href="product.html">Buy </a> </td>
{if $products[$smarty.section.product.index_next].categoryid eq $category.categoryid}
<td> <div align="center">Price: 4,000 </div> </td>
<td> <div align="center"> <a href="product.html">Buy </a> </div> </td>
{else}
<td> <div align="center">  </div> </td>
<td> <div align="center">  </div> </td>
{/if}
</tr>
{/if}
{/section}
</table>
</td>
</tr>
{/foreach}
程序猿之殇 2008-11-13
  • 打赏
  • 举报
回复
乱哄哄的代码,为啥不能弄个格式呢
leec1981 2008-11-13
  • 打赏
  • 举报
回复
贴出代码大家可能更明白我的意思

{foreach item=category from=$categories}
<tr>
<td bgcolor="#COCOCO" colspan="2"><strong>{$category.categoryj} </strong></td>
</tr>
<tr>
<td colspan="2">
<table width="100%" border="1" cellspacing="0" cellpading="5">
{section name=product loop=$products}
{if $products[product].categoryid eq $category.categoryid}
<tr height="70">
<td rowspan="2" width="15%"><a href="product.html">Thumbnail1</a></td>
<td colspan="2" width="35%">
{$products[$smarty.section.product.index].productname}<br>
{$products[product].description|nl2br}
</td>
{if $products[$smarty.section.product.index_next].categoryid eq $category.categoryid}
<td rowspan="2"><a href="product.html">Thumbnail2</a></td>
<td colspan="2">
{$products[$smarty.section.product.index_next].productname}<br>
{$products[$smarty.section.product.index_next].description|nl2br}
</td>
{else}
<td rowspan="2"> </td>
<td colspan="2"> </td>
{/if}
</tr>
<tr>
<td align="center">Price: {$products[product].price}</td>
<td align="center" nowrap="nowrap"><a href="product.html">Buy</a></td>
{if $products[$smarty.section.product.index_next].categoryid eq $category.categoryid}
<td><div align="center">Price: 4,000 </div></td>
<td><div align="center"><a href="product.html">Buy</a></div></td>
{else}
<td><div align="center"> </div></td>
<td><div align="center"> </div></td>
{/if}
</tr>
{/if}
{/section}
</table>
</td>
</tr>
{/foreach}

实际上我是想根据不同的分类,显示产品,每行两个。上面代码还有问题,这个$smarty.section.product.index_next用的不对
chenping1984 2008-11-13
  • 打赏
  • 举报
回复
{foreach name=p item=item key=key from=$array}
{if $smarty.p.iteration/2==0}
id:{$key}
name:{$item}
{/if}
{/foreach}
chenping1984 2008-11-13
  • 打赏
  • 举报
回复
你的是二维数组。
实际上这样表示数组就可以了。
$array = array(1=>'name1',2=>'name2',3=>'name3');
用 foreach
{foreach name=p item=item key=key from=$array}
{if $smarty.p.iteration/2==0}
id:$key
name:$item
{/if}
{/foreach}
程序猿之殇 2008-11-13
  • 打赏
  • 举报
回复
{section name=aa loop=$array start=1 max=1}
{$array[aa].id}
{$array[aa].name}
{/section}
Table of Contents [内容列表] Preface [序] I. Getting Started [开始] 1. What is Smarty? [什么是Smaty?] 2. Installation [安装] Requirements [安装] Basic Installation [基本安装] Extended Setup [扩展设置] II. Smarty For Template Designers [模板设计者篇] 3. Basic Syntax [基本语法] Comments [注释] Functions [函数] Attributes [属性] Embedding Vars in Double Quotes [双引号里值的嵌入] Math [数学运算] 4. Variables [变量] Variables assigned from PHP [从PHP分配的变量] Variables loaded from config files [从配置文件读取的变量] {$smarty} reserved variable [{$smarty}保留变量] 5. Variable Modifiers [变量调节器] capitalize [首字符大写] count_characters [字符计数] cat [连接字符串] count_paragraphs [计算段数] count_sentences [计算句数] count_words [计算词数] date_format [格式化日期] default [默认值] escape [编码] indent [缩进] lower [小写] nl2br [换行符替换成
] regex_replace [正则替换] replace [替换] spacify [插空] string_format [字符串格式化] strip [去除(多余空格)] strip_tags [去除html标签] truncate [截取] upper [大写] wordwrap [行宽约束] 6. Combining Modifiers [组合修改器] 7. Built-in Functions [内建函数] capture config_load foreach,foreachelse include include_php insert if,elseif,else ldelim,rdelim literal php section,sectionelse strip 8. Custom Functions [自定义函数] assign counter cycle debug eval fetch html_checkboxes html_image html_options html_radios html_select_date html_select_time html_table math mailto popup_init popup textformat 9. Config Files [配置文件] 10. Debugging Console [调试控制台] III. Smarty For Programmers [程序员篇] 11. Constants [常量] SMARTY_DIR [Smarty目录] 12. Variables [变量] $template_dir [模板目录变量] $compile_dir [编译目录变量] $config_dir [配置目录变量] $plugins_dir [插件目录变量] $debugging [调试变量] $debug_tpl [调试模板] $debugging_ctrl [调试控制变量] $global_assign [全局配置变量] $undefined [未定义变量] $autoload_filters [自动加载过滤器变量] $compile_check [编译检查变量] $force_compile [强迫编译变量] $caching [缓存变量] $cache_dir [缓存目录变量] $cache_lifetime [缓存生存时间变量] $cache_handler_func [缓存处理函数变量] $cache_modified_check [缓存修改检查变量] $config_overwrite [配置覆盖变量] $config_booleanize [配置布尔化变量] $config_read_hidden [配置读取隐藏变量] $config_fix_newlines [配置固定换行符变量] $default_template_handler_func [默认模板处理函数变量] $php_handling [php处理变量] $security [安全变量] $secure_dir [安全目录变量] $security_settings [安全配置变量] $trusted_dir [信任目录变量] $left_delimiter [左结束符变量] $right_delimiter [右结束符变量] $compiler_class [编译类变量] $request_vars_order [变量顺序变量] $request_use_auto_globals [自动全局变量] $compile_id [编译id变量] $use_sub_dirs [子目录变量] $default_modifiers [默认修正器变量] $default_resource_type [默认源类型变量] 13. Methods [方法] append [添加] append_by_ref [引用添加] assign [赋值] assign_by_ref [引用赋值] clear_all_assign [清除所有赋值] clear_all_cache [清除所有缓存] clear_assign [清除赋值] clear_cache [清除缓存] clear_compiled_tpl [清除已编译模板] clear_config [清除配置] config_load [加载配置] display [显示] fetch [取得输出的内容] get_config_vars [取配置变量的值] get_registered_object [取得已注册的对象] get_template_vars [取得模板变量的值] is_cached [是否已被缓存] load_filter [加载过滤器] register_block [注册一个块] register_compiler_function [注册编译函数] register_function [注册函数] register_modifier [注册修饰器] register_object [注册对象] register_outputfilter [注册输出过滤器] register_postfilter [注册提交过滤器] register_prefilter [注册预过滤器] register_resource [注册资源] trigger_error [触发错误] template_exists [模板是否存在] unregister_block [注销一个块] unregister_compiler_function [注销编译函数] unregister_function [注销函数] unregister_modifier [注销修饰器] unregister_object [注销对象] unregister_outputfilter [注销输出过滤器] unregister_postfilter [注销提交过滤器] unregister_prefilter [注销预过滤器] unregister_resource [注销资源] 14. Caching [缓存] Setting Up Caching [建立缓存] Multiple Caches Per Page [每页多个缓存] Cache Groups [缓存集合] Controlling Cacheability of Plugins' Output [控制插件输出的缓冲能力] 15. Advanced Features [高级特点] Objects [对象] Prefilters [预过滤器] Postfilters [后过滤器] Output Filters [输出滤镜] Cache Handler Function [缓冲处理函数] Resources [资源] 16. Extending Smarty With Plugins [以插件扩展Smarty] How Plugins Work [插件如何工作] Naming Conventions [命名约定] Writing Plugins [编写插件] Template Functions [模板函数] Modifiers [修正器] Block Functions [块函数] Compiler Functions [编译函数] Prefilters/Postfilters [预滤器/后滤器] Output Filters [输出过滤器] Resources [资源] Inserts [插入] IV. Appendixes [附录] 17. Troubleshooting [疑难解答] Smarty/PHP errors [Smarty/PHP 错误] 18. Tips & Tricks [使用技巧和经验] Blank Variable Handling [空白变量处理] Default Variable Handling [默认变量处理] Passing variable title to header template [传递变量标题给头模板] Dates [日期] WAP/WML Componentized Templates [组合的模板] Obfuscating E-mail Addresses [拒绝电子邮件地址] 19. Resources [相关资源] 20. BUGS [漏洞] 21. LIST 翻译人员列表

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧