PHP:如果满足某些条件,则显示<td>元素

时间:2019-12-15 18:22:22

标签: php if-statement html-table

基本上,只有管理员登录后,我才需要使带有删除和编辑按钮的列可见,我知道如何确定管理员是否登录。有没有办法呼应需要显示的标记?预先感谢!

<table>
        <tr>
            <th>ID</th>
            <th>Full Name</th>
            <th>Email Address</th>
            <th>Address</th>
            <th>City</th>
            <th>State</th>
            <th>Zip Code</th>
            <th>Status</th>
            <th>Edit?</th>
            <th>Delete?</th>
        </tr>        
        <?php foreach ($users as $user) : ?>
        <tr>
            <td><?php echo $user->getUser_id();?></td>
            <td><?php echo $user->getFirstName().' '.$user->getLastName();?></td>
            <td><?php echo $user->getEmailAddress(); ?></td>
            <td><?php echo $user->getAddress();?></td>
            <td><?php echo $user->getCity(); ?></td>
            <td><?php echo $user->getState(); ?></td>
            <td><?php echo $user->getZipCode(); ?></td>
            <td><?php echo $user->getActiveDescription();?></td>



these two <td> should only be shown if x is true:

            <td>
             <form action ="" method="post">
                <input type="hidden" name="controllerRequest"
                       value="show_user_edit">
                <input type="hidden" name="user_id"
                       value="<?php echo $user->getUser_id(); ?>"/>
                <input type="submit" value="Edit"></form>
            </td>
            <td>
                <form action="" method="post">
                    <input type="hidden" name="controllerRequest"
                           value="delete_user">
                    <input type="hidden" name="user_id"
                           value="<?php echo $user->getUser_id(); ?>"/>
                    <input type="submit" value="Delete" id="red">
                </form>
            </td>"
                ;
            }
        }
        ?>

        </tr>
        <?php endforeach; ?>    
    </table>

1 个答案:

答案 0 :(得分:-1)

您需要使用<?php if(x): ?><?php endif ?>,请参阅:https://www.php.net/manual/en/control-structures.if.php#112231