2014-09-22 46 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