简体   繁体   English

为什么我的页面在添加 IF 语句时出错?

[英]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:编辑:

  • I have replaced the code from within the 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.这是 IF 语句,只要我添加额外的 IF 语句,代码就会中断。 Even if my primaryGroupId = 2 , and the 3rd IF should be skipped.即使我的primaryGroupId = 2和第三个 IF 也应该被跳过。
  • 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.我已将 Bootstrap 版本从 4.0.0 更改为 5.0.2,并相应地更改了代码,但 IF 语句的问题仍然存在。
  • Also, the server is using php 5.6.40, I do want to go to the latest version here too.另外,服务器使用的是 php 5.6.40,我也想把 go 升级到最新版本。

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.

相关问题 当我将googlesnippets ld + json添加到我的php页面时,为什么会出现错误? 503服务不可用 - Why does it give an error when I add googlesnippets ld + json to my php page? 503 Service Unavaible 为什么当我在第 2 页中添加值然后返回到原始页面时,我的会话会重置? - Why does my session resets when I add value in 2nd page then go back to original one? 每当我在我的 sql 语句中添加“AND”时,该语句都会出现错误 - Whenever I add an 'AND' to my sql statement, the statement gives an error 当我在相对路径中包含目录时,为什么我的PHP包含语句失败? - Why does my PHP include statement fail when I include the directory in the relative path? 当我的语句具有正确数量的参数和值时,为什么会出现SQL错误? - Why do i get an SQL error when i have the right number of parameters and values for my statement? 为什么在输入if语句时我的数组值不显示? - Why does my array value dissappear when entering an if statement? 为什么当我在网址中添加www.irctc.co.in然后在该网站上正常运行时将页面重定向到该网站时,我的页面无法正常工作 - Why my page not work when i add www.irctc.co.in in my url then page redirect on that site while its working good 当我添加博客条目时,为什么日期在我的页面和数据库中显示为全零-为什么? - How come when I add a blog entry the date appears in my page and database as all zeros - why is this? 当我只想要一个问题时,为什么我的页面会显示多个问题? - Why does my page display multiple questions when I only want one? 当我在首页显示wordpress帖子时,为什么我的静态php代码消失了? - Why does my static php code disappear when i show wordpress post on front page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM