duanfan8699 2015-07-03 21:26
浏览 29
已采纳

用户级别转换

I currently am trying to get my user levels from the database, which is fine and working. I am posting them on registration or user edits as tinyints.

I can get these to show, IE User Level: 0 but I want to have it so 0 is changed to Regular User on a userlist page.

This is the code, that is not working. It is showing a blank page.

I have created a function to echo if a certain value is found.

<?php
ini_set('display_errors', '1');
error_reporting(E_ALL);

include_once '../includes/conn.php';

if(!$user->is_loggedin()){
    $user->redirect('../users/login.php');
}

$stmt = $conn->prepare("SELECT username, level FROM users");
$stmt->execute();

$userRow=$stmt->fetchAll(PDO::FETCH_ASSOC);

function level($userRow){
    if($userRow['level'] == '1'){
        echo "Regular User";
    }
    elseif($userRow['level'] == '2'){
        echo "Moderator";
    }
    elseif($userRow['level'] == '3'){ 
        echo "Administrator"; 
    }else{ 
        echo "Undefined"; 
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>EpicOwl UK | CMS User List</title>
    <meta charset="utf-8">
    <link rel="shortcut icon" href="../images/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" type="text/css" href="../css/main.css">
</head>
<body>
<div id="header">
    <a href="index.php"><img id="logo" src="../images/logo.png" /></a>
    <div id="navigation">
        <ul>
            <a href="../index.php"><li>Home</li></a>
            <a href="./profile.php"><li>Profile</li></a>
            <a href="../admin/index.php"><li>Admin Panel</li></a>
            <a href="./logout.php?logout=true"><li>Logout</li></a>
        </ul>
    </div>
</div>
<div id="content">
<h2>Members List</h2>
<table>
    <tr>
        <th align="left">Username</th>
        <th align="left">User Level</th>
    </tr>
        <?php 
        foreach($userRow as $list){
            echo"<tr><td>".($list['username'])."</td><td>".level();."</td></tr>"; 
        }
        ?>
</table>
</div>
<div id="footer">
    <p class="copyright">&copy; EpicOwl UK. All Rights Reserved.</p>
</div>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • duanmi3476 2015-07-03 21:38
    关注

    There is a problem with you PHP calling inside the foreach

    you are putting semi-colon after calling level() function which terminates the line, then you are concatenating a string which is also wrong.

    Another problem is that level expects a parameter and you are not sending it.

    The code can be fixed as below:

    Edit Try this

    make your function return a value and not echo, Echo writes directly to the standard output, the echo in level funtion is being printed before the echo in the foreach :

        function level($userRow){
            $returnString = '';
            if($userRow['level'] == '1'){
                $returnString = "Regular User";
            }
            elseif($userRow['level'] == '2'){
                $returnString = "Moderator";
            }
            elseif($userRow['level'] == '3'){ 
                $returnString = "Administrator"; 
            }else{ 
                $returnString = "Undefined"; 
            }
    
            return $returnString;
        }
    

    then call the foreach as follows :

    <?php 
          foreach($userRow as $list)
          {
                echo "<tr><td>" . $list['username'] ."</td><td>" 
                   . level($list) . "</td></tr>"; 
          } 
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度