PHP smarty行很容易隐藏

PHP smarty行很容易隐藏,php,html,smarty,tablerow,Php,Html,Smarty,Tablerow,我有一段代码,它应该显示一行,如果它有一个特定的值。我是这个HTML/PHP/SMARTY世界的新手,不知道如何用简单的方法解决这个问题。 我的代码如下所示: <tr> other data to be defined before the code under this. {foreach from=$data item=item2 } {if isset ($data)} {if (strpos($item.to, 'given stringk')) == true} hide

我有一段代码,它应该显示一行,如果它有一个特定的值。我是这个HTML/PHP/SMARTY世界的新手,不知道如何用简单的方法解决这个问题。 我的代码如下所示:

<tr>
other data to be defined before the code under this.
{foreach from=$data item=item2 }
{if isset ($data)}
{if (strpos($item.to, 'given stringk')) == true}
hide row
{else}
hide row
{/if}
{else}
do not hide row
</tr>

在此代码之前定义的其他数据。
{foreach from=$data item=item2}
{if isset($data)}
{if(strpos($item.to,'given stringk'))==true}
隐藏行
{else}
隐藏行
{/if}
{else}
不要隐藏行

我似乎无法理解这一点。有什么想法吗

正如@sofl所提到的,您应该使用
!==false
而不是
==true
,因为如果字符串以开头。整个代码应该可以正常工作

我已经准备好了示例脚本,效果很好:

PHP文件:

$x = array (
   array ('to' => 'given string'),
   array ('to' => 'givenNOstring'),
   array ('to' => 'givenNOstring2'),
   array ('to' => 'given string2'),

);

$smarty->assign('data', $x);
Smarty模板文件:

<tr>
other data to be defined before the code under this.<br />
{foreach from=$data item=item2 }
{if (strpos($item2.to, 'given string')) !== false}
hide row {$item2.to}<br />
{else}
do not hide row {$item2.to}<br />
{/if}
{/foreach}
</tr>

正如预期的那样,它工作正常

$data
应始终在
$data
的循环中定义
strpos
如果未找到指针,则返回整数或false。只有当strps结果>=1(位置从0开始)时,if构造才会为true。根据您的smarty版本,您最好执行
{if$item.to | strpos:'given stringk'!==false}
other data to be defined before the code under this.
hide row given string
do not hide row givenNOstring
do not hide row givenNOstring2
hide row given string2