【问题标题】:Smarty assign value to an array variableSmarty 为数组变量赋值
【发布时间】:2014-09-22 14:25:35
【问题描述】:

我正在使用 SMARTY,我需要创建一个数组并将值分配给它的特定索引。

这是我的 php 代码:

   $tag = str_replace('-', ' ',$_GET['tag']);
    $tag = strip_tags(trim(mysql_real_escape_string(addslashes($tag)))); // the tags word variable
    $smarty->assign('tag',$tag);


    $tag_sql = "SELECT * FROM items WHERE item_published='0' AND item_tags LIKE '%$tag%' ";
$tag_query = mysql_query($tag_sql);
    while ($tag_row = mysql_fetch_assoc($tag_query)) {
   $items[] = $tag_row;
 }
    $smarty->assign('items',$items); // assign the  items loop to smarty

当我在 smarty 模板中使用此代码时

 {section name=x loop=$items } {$items[x].item_url} {/section}

html 输出是

http://google.com http://yahoo.com

我想做html输出

'http://google.com','http://yahoo.com'

【问题讨论】:

  • 不清楚你想要达到什么目的。编辑您的问题并在 PHP 中显示您的数据,现在真的很难说出您想要实现的目标。

标签: arrays smarty smarty3 smarty2


【解决方案1】:

你可以这样做:

{section name=x loop=$items } {append var="urls" value="'`$items[x].item_url`'"} {/section}

{","|implode:$urls}

输出是:

'http://google.com','http://yahoo.com'

对于 Smarty 2,您可以使用:

{section name=x loop=$items } '{$items[x].item_url}'{if not $smarty.section.x.last},{/if} {/section}

【讨论】:

  • 发生以下错误致命错误:Smarty 错误:[在 tag.html 第 44 行]:语法错误:C:\wamp 中无法识别的标签“附加”(Smarty_Compiler.class.php,第 590 行) \www\include\smarty\Smarty.class.php 在第 1095 行
  • @user3778067 你没有粘贴任何错误。您使用什么版本的 Smarty?
  • 将 smarty 更新为 Releases 版本,问题已解决,谢谢
  • @user3778067 我已经编辑了我的答案并添加了 Smarty 2 的解决方案
  • 你能帮我解决其他问题吗?
猜你喜欢
  • 2011-05-29
  • 2015-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-27
  • 2016-11-21
  • 2018-12-28
  • 1970-01-01
相关资源
最近更新 更多