arrayssmartysmarty3smarty2

Smarty assign value to an array variable


I am using SMARTY and I need to create an array and assign value to a particular index of it.

this is my php code:

   $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

when i use this code in smarty template

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

html output is

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

I want to be html output

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

Solution

  • You can do it this way:

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

    Output for that is:

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

    For Smarty 2 you can use:

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