cakephp foreach循环条件

时间:2014-11-14 16:06:45

标签: php cakephp

我在admin_index视图中有这段代码

<?php foreach ($users as $user): ?>
<tr>
    <?php if ( $user['User']['account_type']=='admin' ): ?>
    <td><?php echo h($user['User']['ID']); ?>&nbsp;</td>
    <td><?php echo h($user['User']['username']); ?>&nbsp;</td>
    <td><?php echo h($user['User']['account_type']); ?>&nbsp;</td>
    <td class="actions">
        <?php echo $this->Html->link(__('View'), array('action' => 'admin_view', $user['User']['ID'])); ?>
        <?php echo $this->Html->link(__('Edit'), array('action' => 'admin_edit', $user['User']['ID'])); ?>
    </td>
    <?php else: ?>
    <td><?php echo h($user['User']['ID']); ?>&nbsp;</td>
    <td><?php echo h($user['User']['username']); ?>&nbsp;</td>
    <td><?php echo h($user['User']['account_type']); ?>&nbsp;</td>
    <td class="actions">
        <?php echo $this->Html->link(__('View'), array('action' => 'admin_view', $user['User']['ID'])); ?>
        <?php echo $this->Html->link(__('Edit'), array('action' => 'admin_edit', $user['User']['ID'])); ?>
        <?php echo $this->Form->postLink(__('Delete'), array('action' => 'admin_delete', $user['User']['ID']), array(), __('Are you sure you want to delete # %s?', $user['User']['ID'])); ?>
    </td>
</tr>
<?php endif; ?>
<?php endforeach; ?>

我尝试做的是根据帐户类型* ergo获取行以不同的方式打印操作td单元格如果帐户类型是“用户”#39;它将打印一个删除按钮,如果&#admin;管理员,不允许删除..现在,一个管理员是超级用户&#39;指定的超级用户。布尔列,我尝试将其集成到if条件中,如果当前登录的管理员是指定的超级用户,他自己的帐户行将没有类似于上面的代码的删除按钮,但也能够删除其他管理员..如果当前登录的用户不是超级用户,将显示上述代码,而其他管理员无法查看超级用户个人资料

尝试使用

在if语句中调用auth和session
this>auth/session->user('ID')

并没有真正顺利

更新

    <?php foreach ($users as $user): ?>
<tr>
    <?php if ( $this->Session->read('User.super_user')=== 1 ): ?>
        <?php if ( $this->Session->read('User.ID')===$user['User']['ID']): ?>
            <td><?php echo h($user['User']['ID']); ?>&nbsp;</td>
            <td><?php echo h($user['User']['username']); ?>&nbsp;</td>
            <td><?php echo h($user['User']['account_type']); ?>&nbsp;</td>
            <td class="actions">
                <?php echo $this->Html->link(__('View'), array('action' => 'admin_view', $user['User']['ID'])); ?>
                <?php echo $this->Html->link(__('Edit'), array('action' => 'admin_edit', $user['User']['ID'])); ?>
            </td>
        <?php else: ?>
            <td><?php echo h($user['User']['ID']); ?>&nbsp;</td>
            <td><?php echo h($user['User']['username']); ?>&nbsp;</td>
            <td><?php echo h($user['User']['account_type']); ?>&nbsp;</td>
            <td class="actions">
                <?php echo $this->Html->link(__('View'), array('action' => 'admin_view', $user['User']['ID'])); ?>
                <?php echo $this->Html->link(__('Edit'), array('action' => 'admin_edit', $user['User']['ID'])); ?>
                <?php echo $this->Form->postLink(__('Delete'), array('action' => 'admin_delete', $user['User']['ID']), array(), __('Are you sure you want to delete # %s?', $user['User']['ID'])); ?>
            </td>
        <?php endif; ?>
    <?php else: ?>

        <?php if ($this->Session->read('User.ID')=== $user['User']['ID']): ?>
            <td><?php echo h($user['User']['ID']); ?>&nbsp;</td>
            <td><?php echo h($user['User']['username']); ?>&nbsp;</td>
            <td><?php echo h($user['User']['account_type']); ?>&nbsp;</td>
            <td class="actions">
                <?php echo $this->Html->link(__('View'), array('action' => 'admin_view', $user['User']['ID'])); ?>
                <?php echo $this->Html->link(__('Edit'), array('action' => 'admin_edit', $user['User']['ID'])); ?>
            </td>
        <?php elseif ($user['User']['super_user'] ===1): ?>
            <td><?php echo h($user['User']['ID']); ?>&nbsp;</td>
            <td><?php echo h($user['User']['username']); ?>&nbsp;</td>
            <td><?php echo h($user['User']['account_type']); ?>&nbsp;</td>
            <td class="actions">
                <?php echo "no altering allowed";?>
            </td>
        <?php else: ?>
            <td><?php echo h($user['User']['ID']); ?>&nbsp;</td>
            <td><?php echo h($user['User']['username']); ?>&nbsp;</td>
            <td><?php echo h($user['User']['account_type']); ?>&nbsp;</td>
            <td class="actions">
                <?php echo $this->Html->link(__('View'), array('action' => 'admin_view', $user['User']['ID'])); ?>
                <?php echo $this->Html->link(__('Edit'), array('action' => 'admin_edit', $user['User']['ID'])); ?>
                <?php echo $this->Form->postLink(__('Delete'), array('action' => 'admin_delete', $user['User']['ID']), array(), __('Are you sure you want to delete # %s?', $user['User']['ID'])); ?>
            </td>
        <?php endif; ?>
<?php endif; ?>
    </tr>

我的问题现在来自我的第一层if语句。它会自动忽略我检查会话的当前super_user是否设置为1的条件。它总是与else语句一起... dunno正在进行什么

1 个答案:

答案 0 :(得分:0)

Accessing the logged user

在您的控制器中:

$iAmsuperAdmin = (bool)$this->Auth->user('super_user');
$myId = (int)$this->Auth->user('ID');
$this->set('iAmsuperAdmin', $iAmsuperAdmin);
$this->set('myID', $myID);

在视图中:

<?php foreach ($users as $user): ?>
    <?php 
    $canDelete = false;

    // admin users should be able to delete
    if ($user['User']['account_type'] == 'admin') {
        $canDelete = true;
    }

    // if I am the super-admin, I should not be able to delete myself
    if ($user['User']['account_type'] == 'admin' && $iAmSuperAdmin === true && $myID == $user['User']['ID']) {
        $canDelete = false;
    }
    ?>
    <tr>
    <?php  ?>
        <td><?php echo h($user['User']['ID']); ?>&nbsp;</td>
        <td><?php echo h($user['User']['username']); ?>&nbsp;</td>
        <td><?php echo h($user['User']['account_type']); ?>&nbsp;</td>
        <td class="actions">
            <?php echo $this->Html->link(__('View'), array('action' => 'admin_view', $user['User']['ID'])); ?>
            <?php echo $this->Html->link(__('Edit'), array('action' => 'admin_edit', $user['User']['ID'])); ?>
            <?php if ($canDelete === true) { echo $this->Form->postLink(__('Delete'), array('action' => 'admin_delete', $user['User']['ID']), array(), __('Are you sure you want to delete # %s?', $user['User']['ID'])); } ?>
        </td>
        </tr>
<?php endforeach; ?>
相关问题