简体   繁体   English

如何在smarty中使用{section}打印关联数组?

[英]How to use {section} in smarty to print an associative array?

I want to print an associative array using section but I'm not able to do so.Following is the PHP and Smarty code : 我想使用section打印一个关联数组,但是我不能这样做。以下是PHP和Smarty代码:

PHP Code : PHP代码:

  $smarty->assign('$all_latest_news',       $grid_data);

In the above code $grid_data is an associative array which is assigned to the smarty template. 在上面的代码中, $grid_data是分配给smarty模板的关联数组。 For your information the array looks like this after printing using print_r($grid_data) : 供您参考,使用print_r($grid_data)打印后,数组如下所示:

Array ( [0] => Array ( [news_id] => 1 [news_title] => News Channel URL [news_link] => http://indiatoday.intoday.in/ [news_added_date] => 2013-02-26 ) [1] => Array ( [news_id] => 2 [news_title] => News Paper Web Address [news_link] => http://www.bhaskar.com/ [news_added_date] => 2013-02-25 ) )

Now I want to print this array in smarty template the code snippet for this is as follows : 现在,我想在smarty模板中打印此数组,其代码片段如下:

<table width="100%" align="center" cellpadding="6" cellspacing="1" bgcolor="#FFFFFF">
                <tr align="center" id="tableHeader">
                    <td width="20%" align="left"><b>News Title</b></td>
                    <td width="55%" align="left"><b>News Link</b></td>
                    <td width="15%" align="center"><b>Option</b></td>
                </tr>
                {section name=news loop=$all_latest_news}
                <tr align="center" id="tabledata" bgcolor="{cycle values="#d0e8f4,#96c0d5"}">
                    <td align="left" valign="top">{$all_latest_news[news].news_title}</td>
                    <td align="left" valign="top">{$all_latest_news[news].news_link|truncate:350:" ...":true}</td>

                    <td align="center" valign="top"><a href="manage_latest_news.php?op=edit&news_id={$all_latest_news[news].news_id}">Edit</a>
                    &nbsp;&nbsp;<a href="manage_latest_news.php?op=delete&news_id={$all_latest_news[news].news_id}" onClick="return ConfirmDelete()">Delete</a></td>
                </tr>
                {sectionelse}
                <tr>
                    <td colspan="5" align="center"><b>No News Available</b></td>
                </tr>
                {/section}
</table>

Using this code I'm not able to print the array values to the respective columns. 使用此代码,我无法将数组值打印到相应的列。 The column headings getting printed properly. 列标题正确打印。 After that the {sectionelse} part is executed and the message "No News Available" gets printed. 之后,将执行{sectionelse}部分,并显示消息“ No News Available”。

Actually I want to print the values in array. 实际上我想在数组中打印值。 Can anyone help me out to resolve this issue? 谁能帮我解决这个问题? Thanks in advance. 提前致谢。

When you assign the values to variables in Smarty, you don't want to put '$' symbol. 当您将值分配给Smarty中的变量时,您不想放置'$'符号。 Change your code to, 将您的代码更改为

$smarty->assign('all_latest_news', $grid_data);

Change $smarty->assign('$all_latest_news', $grid_data); 更改$smarty->assign('$all_latest_news', $grid_data);

to : 至 :

$smarty->assign('all_latest_news', $grid_data);

Use this : 用这个 :

{foreach from=$all_latest_news item=news name=news}
   {$news.news_title}
{/foreach}

Ref: http://www.smarty.net/docsv2/en/language.function.foreach 参考: http : //www.smarty.net/docsv2/zh/language.function.foreach

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM