[英]Why does my page error when I add an IF statement?
I am new to coding and new to Bootstrap.我是编码新手,也是 Bootstrap 新手。 I'm encountering a problem with the Bootstrap Navbar, all goes well until I try to use consecutive IF statements that check the Session and show or don't show specific menu items.
我遇到了 Bootstrap Navbar 的问题,一切顺利,直到我尝试使用连续的 IF 语句来检查 Session 并显示或不显示特定的菜单项。
I have a function drawTopMenu()
PHP function that I include on most pages.我在大多数页面上都有一个
function drawTopMenu()
PHP function。 This header is using YUI and I'm changing it to Bootstrap.这个 header 正在使用 YUI,我正在将其更改为 Bootstrap。 Here is the first part of the code that is not giving any issues.
这是代码的第一部分,没有给出任何问题。 This is a test file as I was having lots of trouble trying to figure out the cause.
这是一个测试文件,因为我在试图找出原因时遇到了很多麻烦。
I have tried to remove the IF statements and this solves the "HTTP 500 server error", but I do need to show specific items to specific users.我试图删除 IF 语句,这解决了“HTTP 500 服务器错误”,但我确实需要向特定用户显示特定项目。 I also tried reverting back to the YUI menu bar cade and that works fine with many more IF statements.
我还尝试恢复到 YUI 菜单栏 cade,它可以很好地处理更多 IF 语句。
EDIT:编辑:
IF {}
by "...", this did certainly not cause the issue.IF {}
中的代码替换为“...”,这当然不会导致问题。 It's the IF statements and as soon as I add the extra IF statement the code breaks.primaryGroupId = 2
, and the 3rd IF should be skipped.primaryGroupId = 2
和第三个 IF 也应该被跳过。 The code now looks like this "//BREAKS THE CODE" is the IF statement causing problems:代码现在看起来像这样“//BREAKS THE CODE”是导致问题的 IF 语句:
function drawTopMenu () {
$session_int = $_SESSION['primaryGroupId'];
//Bootsrap menubar
$html = <<<EOS
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="/home.php">
<img src="http://192.168.42.30:8080/lqcadjust/logo-grey.png" alt="Home" width="153" height="34"></a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
EOS;
//Approval List
if (($session_int == 1) || ($session_int == 2)) {
$html .= <<<EOS
<li class="nav-item"><a class="nav-link" href="/home/event/approval_status.php">Approval List</a></li>
EOS;
}
//File Maintenance
if (($session_int == 1) || ($session_int == 2)) {
...
}
if (($session_int == 2)){
...
}
//BREAKS THE CODE
if (($session_int == 3)){
...
}
//BREAKS THE CODE
}
I found a workaround by using switch
instead of if
, this is more elegant and works.我通过使用
switch
而不是if
找到了解决方法,这更优雅并且有效。 I do not know why this was happening however!我不知道为什么会这样!
EDIT: I was using IF statements to account for different user permissions.编辑:我使用 IF 语句来解释不同的用户权限。 For this I needed many IF statements covering each menu item, now I make 1 switch statement and it covers the entire menu item.
为此,我需要覆盖每个菜单项的许多 IF 语句,现在我制作了 1 个 switch 语句,它覆盖了整个菜单项。
FROM:从:
//File Maintenance item
IF ($session_int == 1){
...
}
IF ($session_int == 2){
...
}
IF ($session_int == 9){
...
}
IF ($session_int == 11){
...
}
TO (here is an explanation on how to use switch
, by w3) : TO (这里是关于如何使用
switch
的解释,由 w3 提供) :
//File Maintenance item
switch ($session_int){
case 1:
…
break;
case 2:
…
break;
case 9:
…
break;
case 11:
…
break;
}
I know that this is not a solution so I will not mark it as such, just an alternative approach.我知道这不是解决方案,所以我不会将其标记为这样,只是另一种方法。
If anyone has suggestions for a solution, please share.如果有人对解决方案有建议,请分享。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.