使用in_array和smarty section循环

时间:2015-01-25 09:04:23

标签: php smarty

这是我的智能模板文件:

{section loop=$Author name=Author}
<option value="{$Author[Author].Id}" 
{if $Author[Author].Id|in_array:$ListAths.AuthorId})}
selected
{/if}
>
{$Author[Author].AuthorName}</option>
{/section}

我的两个变量的PHP结构(作者和ListAths)看起来像是这样的,用sql和smarty命令替换正确的变量:

$stmt = $connect->prepare(/*command here*/);
$stmt->execute($array);
$result = $stmt->fetchall();
$smarty->assign('ListAths', $result);
$stmt2 = $connect->prepare(/*command here*/);
$stmt2->execute($array);
$result2 = $stmt2->fetchall();
$smarty->assign('Author', $result2);

我得到的错误:

Warning: in_array() expects parameter 2 to be array, null given in /mydirectory/Smarty/sysplugins/smarty_internal_templatebase.php(171) : eval()'d code on line 71

$ListAths.AuthorId无法识别为数组!我该如何解决这个问题?

它以这种方式工作:

{section loop=$Author name=Author}
<option value="{$Author[Author].Id}" 
{section loop=$ListAths name=ListAths}
{if $Author[Author].Id == $ListAths[ListAths].AuthorId}
selected
{/if}
{/section}
>
{$Author[Author].AuthorName}</option>
{/section}

但我仍然想使用in_array

来做

1 个答案:

答案 0 :(得分:0)

我不能评论你问哪个聪明的版本...假设聪明3你可以在你聪明的模板中使用任何PHP函数(以及对象)......

{section loop=$Author name=Author}
<option value="{$Author[Author].Id}" 
{if in_array($Author[Author].Id, $ListAths.AuthorId)}
selected
{/if}
>
{$Author[Author].AuthorName}</option>
{/section}