IDontKnow
IDontKnow

Reputation: 355

changing the value from database on the webpage

I'm about to "list active users from database."

I have username, email, password and status columns.

If status value is "1" user can login. It means s/he is active.

If status value is "0" user can't login. Because s/he is inactive.

What I want is when I list users, I want to show "Active" instead of "1" and "Deactive" instead of "0"

Here is my code:

<?php
$query = $connection->query('SELECT * FROM users');
while($r = $query->fetch()) {
echo "<tr>
        <td class=\"danger\">$r[id]</td>
        <td class=\"success\">$r[username]</td>
        <td class=\"warning\">$r[email]</td>
        <td class=\"info\">$r[status]</td>  
    </tr>";
    }
?>

With this $r[status] is status of user either 1 or 0. I want to display only on the webpage (Active/Deactive) but not changing column values in the database.

Upvotes: 3

Views: 84

Answers (3)

Gideon Appoh
Gideon Appoh

Reputation: 663

You can use:

<?php
$query = $connection->query('SELECT * FROM users');
while($r = $query->fetch()) {
echo "<tr>
        <td class=\"danger\">$r[id]</td>
        <td class=\"success\">$r[username]</td>
        <td class=\"warning\">$r[email]</td>
        <td class=\"info\">" . ($r['status'] == 1 ? 'Active' : 'Inactive') . "</td>  
    </tr>";
    }
?>

Upvotes: 3

Lukasz
Lukasz

Reputation: 409

You can add this code before echo.

if ($r['status'] > '0') { 
  $r['status'] = "Active"; 
}else{
  $r['status'] = "Deactive"; 
}

It means if the value is bigger than 0, status will be Active, else status will be deactive.

Hope it works for you.

Upvotes: 4

Hassaan
Hassaan

Reputation: 7672

You can do it using IF/ELSE conditions.

Try

<?php

$query = $connection->query('SELECT * FROM users');
while($r = $query->fetch())
{
    if($r['status'] == 1) $r['status'] = 'yes';
    if($r['status'] == 0) $r['status'] = 'no';

    echo "<tr>
            <td class=\"danger\">$r[id]</td>
            <td class=\"success\">$r[username]</td>
            <td class=\"warning\">$r[email]</td>
            <td class=\"info\">$r[status]</td>  
        </tr>";
}

?>

Upvotes: 1

Related Questions