Помогите написать функцию для смарти

Platon_82

Новичок
Помогите написать функцию для смарти

Есть у меня функция для вывода данных в селект по умолчанию:
PHP:
function selection()
  {
    $sql="select * from table order by id";		
    $res=db_select($sql);
    while ($item = mysql_fetch_array($res)) 
      {
	$txt .= '<option value="'.$item['id'].'">'.$item['text'].'</option>';	
      }
     return $txt;
}
$smarty->register_function("selection", "selection");
Вызываю ее в шаблоне так: {selection}

Теперь мне нужно выводить этот же селект, где известно выбранную запись(то что выбрал пользователь)
Пробовал так:

PHP:
function selection($id)
  {
    $sql="select * from table order by id";		
    $res=db_select($sql);
    while ($item = mysql_fetch_array($res)) 
       {
	if($id==$item['id'])
 	   $txt .= '<option value="'.$item['id'].'" selected=\"selected\">'.$item['text'].'</option>';
	else
	   $txt .= '<option value="'.$item['id'].'">'.$item['text'].'</option>';	
       }
    return $txt;
}
$smarty->register_function("selection", "selection");
Вызываю ее в шаблоне так: {selection id=12}, но грузится селект как в первой функции. Без выбранного елемента..(((
Как сделать правильно?
 

FractalizeR

Новичок
Зря вы документацию не читаете. А сразу на форум вопросы.

void smarty_function_name (array $params, object &$smarty)



Template Functions

void smarty_function_name (array $params, object &$smarty)


All attributes passed to template functions from the template are contained in the $params as an associative array.

The output (return value) of the function will be substituted in place of the function tag in the template (fetch() function, for example). Alternatively, the function can simply perform some other task without any output (assign() function).

If the function needs to assign some variables to the template or use some other Smarty-provided functionality, it can use the supplied $smarty object to do so.
 
Сверху