If ... Else语句在CakePHP中显示错误逻辑

时间:2011-10-20 09:01:17

标签: php cakephp cakephp-1.3

我有以下语句来显示我的所有用户的激情,它显示所有那些只有激情,而那些没有激情,它没有显示'空'文本。请协助。 $ cell是一个表,我用它来附加我的所有数据。

foreach ($user['Passion'] as $passion) {
    if (empty($passion['PassionsUser']['passion_tag'])) {
        $cell .= 'empty';
    }
        if ($passion['PassionsUser']['type'] == 'personal') {

            $cell .= '[' . $html->link(ucwords($passion['PassionsUser']['passion_tag']), array('controller' => 'passions', 'action' => 'view', $passion['PassionsUser']['passion_tag']), array('escape' => false, 'class' => 'normalTip', 'title' => ucwords($passion['PassionsUser']['type'] . ' passion'))) . ']';
        } elseif ($passion['PassionsUser']['type'] == 'professional') {

            $cell .= '[' . $html->link(ucwords($passion['PassionsUser']['passion_tag']), array('controller' => 'passions', 'action' => 'view', $passion['PassionsUser']['passion_tag']), array('escape' => false, 'class' => 'normalTip', 'title' => ucwords($passion['PassionsUser']['type'] . ' passion'))) . ']';
        }
}
$cells[] = $cell;

1 个答案:

答案 0 :(得分:0)

通过previous question进行猜测,问题是如果用户没有任何激情,$user[ 'Passion' ]将为空,因此循环根本不会运行。将代码更改为:

if( empty( $user[ 'Passion' ] ) {
    $cell = 'empty';
}
else foreach( $user[ 'Passion' ] as $passion ) {
    ....
}