簡體   English   中英

cakephp foreach循環條件

[英]cakephp foreach loop condition

我在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單元格。如果帳戶類型為'user',它將打印一個刪除按鈕,如果'admin,不允許刪除。現在事情是一個管理員是'super_user'布爾列指定的超級用戶,我試圖將其集成到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發生了什么

訪問登錄的用戶

在您的控制器中:

$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; ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM