Sauced Apples
Sauced Apples

Reputation: 1173

User Level Conversion

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>

Upvotes: 0

Views: 41

Answers (1)

KAD
KAD

Reputation: 11102

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>"; 
      } 
?>

Upvotes: 1

Related Questions