【问题标题】:PHP smarty row hide easilyPHP smarty 行轻松隐藏
【发布时间】:2014-06-25 11:52:06
【问题描述】:

我有一些代码,如果它具有特定值,则应该显示一行。我是这个 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>

我似乎无法弄清楚这一点。有任何想法吗?

【问题讨论】:

  • $data 应始终在$data 的循环内定义。 strpos 如果找不到针,则返回整数或 false。只有当 strpos 结果>= 1(位置从 0 开始)时,您的 if 构造才会为真。取决于你的聪明版本,你应该更好地做{if $item.to|strpos:'given stringk' !== false}

标签: php html smarty tablerow


【解决方案1】:

正如@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>

输出是:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多