2014-09-22 36 views
0

我正在使用SMARTY,我需要创建一个数组并将值分配给它的特定索引。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' 
+0

目前还不清楚你想要达到的目标。编辑你的问题并在PHP中显示你的数据,现在很难说出你想要达到的目标。 – 2014-09-22 16:03:51

回答

0

你可以这样来做:

{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} 
+0

发生以下错误致命错误:Smarty错误:[in tag.html line 44]:语法错误:无法识别标记'append'(Smarty_Compiler.class.php,line 590)in C:\ wamp \ www \ include \ smarty \ 1095行的Smarty.class.php – user3778067 2014-09-22 16:38:48

+0

@ user3778067你没有粘贴任何错误。你使用什么版本的Smarty? – 2014-09-22 16:39:40

+0

@version 2.6.18 – user3778067 2014-09-22 16:41:16