0

我正在使用 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'
4

1 回答 1

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}
于 2014-09-22T16:26:48.000 回答