douqiao5543 2017-10-23 23:24
浏览 54
已采纳

php如果语句作为函数内部的返回值

I have a function to see what a user's role is. It works great on it's own in the html document. But I have several functions that connect to a database and print information. I want certain parts of the function to be available to all users and some only to admin. Right now it is printing <?php ?> in the source file.

function isUserInRole($userRole){
    $retVal = false;

    if ($userRole == $_SESSION['role']) {
        $retVal = true;
    }

    return $retVal;
}

This works fine in the html document:

<?php if(isUserInRole('Admin')){ ?>
    <?php print "<a href='edit/staffDetailsForm.php?ID=$staffId'><button>Edit &gt;</button></a>" ?>
<?php } ?>

Here is a function that is not working:

function getLabelCodes ($staffId, $staffName, $compId){
    $retVal = "";

    include "inc/DBconnect.php";

    $query = "SELECT l.listName, l.listCode FROM labels AS l INNER JOIN agtLabels AS al ON l.listCode = al.listCode WHERE al.staffId = $staffId AND al.compId = $compId";

    if ($result = $mysql->query($query)) {
        while ($aRow = $result->fetch_assoc()) {

            $listCode = $aRow['listCode'];

            $retVal = $retVal . "<li class='remove'><form action='delete/removeStaffLabel.php' method='post' onsubmit='return confirm('Do you really want to remove" . $staffName . "from " . $listCode . "?')'>";
            $retVal = $retVal . $aRow['listName'] .

                " <input type='hidden' name='staffId' value='" . $staffId . "' />
                    <input type='hidden' name='compId' value='" . $compId . "' />
                <input type='hidden' name='listCode' value='". $listCode . "' />
                <input class='remove' type='submit' value='Remove from "  . $aRow['listName'] . "' />
                </form>
                </li>";
        }
    } return "<?php if(isUserInRole('Admin')){ ?><h3>Label Lists:</h3><a href='edit/staffLabels.php?staffId=" . $staffId . "&compId=" . $compId . "' /><button>Add to Label list</button></a><ul>" . $retVal  . "</ul><?php } ?>";

    $mysqli->close();

}

I have tried placing the<?php if(isUserInRole('Admin')){ ?> bit in the return (as it is now) as well as part of $retVal. I have to find a way to fix it within the function because I have many other functions that are similar and I have no real way of breaking them up further.

The source-code prints </contact><comments></comments><?php if(isUserInRole('Admin')) { ?><h3>Label Lists:</h3>. How do I get isUserInRole() to perform it's action prior to the page load being complete?

Any help would be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • dpl74687 2017-10-23 23:35
    关注

    You can do it inside your function:

    if(isUserInRole('Admin')){
        return "<h3>Label Lists:</h3><a href='edit/staffLabels.php?staffId=" . $staffId . "&compId=" . $compId . "' /><button>Add to Label list</button></a><ul>" . $retVal  . "</ul>"
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?