简体   繁体   中英

Why does my page error when I add an IF statement?

I am new to coding and new to 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.

I have a function drawTopMenu() PHP function that I include on most pages. This header is using YUI and I'm changing it to 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. I also tried reverting back to the YUI menu bar cade and that works fine with many more IF statements.

EDIT:

  • I have replaced the code from within the IF {} by "...", this did certainly not cause the issue. It's the IF statements and as soon as I add the extra IF statement the code breaks. Even if my primaryGroupId = 2 , and the 3rd IF should be skipped.
  • I have changed the Bootstrap version from 4.0.0 to 5.0.2 and changed the code accordingly but the issues with the IF statement remain.
  • Also, the server is using php 5.6.40, I do want to go to the latest version here too.

The code now looks like this "//BREAKS THE CODE" is the IF statement causing problems:

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. I do not know why this was happening however!

EDIT: I was using IF statements to account for different user permissions. For this I needed many IF statements covering each menu item, now I make 1 switch statement and it covers the entire menu item.

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) :

//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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM