1

In my MySQL table, there is a row labeled "state". There are 3 values possible for this. How could I utilise PHP to echo "Unbanned" as apposed to "0" in state? And "Banned" as apposed to "1" and "Temp-Banned" as apposed to "3"?

How would I use php to display "UnBanned" instead of 0 in the MySQL? I am using a table for this.

<?
          while($row = $result->fetch_assoc()){
          echo'<td>' . $row['player'] . '</td>';
          echo'<td>' . $row['admin'] . '</td>';
          echo'<td>' . $row['reason'] . '</td>';
          echo'<td>' . $row['created_at'] . '</td>';
          echo'<td>' . $row['expires_at'] . '</td>';
          echo'<td>' . $row['state'] . '</td>' . '</tr>';
          }
?>

All help is incredibly appreciated :)

1

3 Answers 3

3

You can do it either via PHP or MySQL. There are a lot of possibilities.

SELECT
    CASE
        WHEN state = 0 THEN 'UnBanned'
        WHEN state = 1 THEN 'Banned'
        ELSE 'Temp-Banned'
    END AS state

In PHP:

while ($row = $result->fetch_assoc()) {
    switch ($row['state']) {
        case 0: $row['state'] = "UnBanned";
            break;
        case 1: $row['state'] = "Banned";
            break;
        default: $row['state'] = "Temp-Banned";
    }
Sign up to request clarification or add additional context in comments.

2 Comments

I think OP should create a table of possible status'
@FreshPrinceOfSO That would work great since you could select the state's name by joining on the ID which would be the number
2
<?
while($row = $result->fetch_assoc()){
    echo'<td>' . $row['player'] . '</td>';
    echo'<td>' . $row['admin'] . '</td>';
    echo'<td>' . $row['reason'] . '</td>';
    echo'<td>' . $row['created_at'] . '</td>';
    echo'<td>' . $row['expires_at'] . '</td>';

 switch ($row['state']){
    case 0: echo '<td>Unbanned</td>';
        break;
    case 1: echo '<td>Banned</td>';
        break;
    case 3: echo '<td>Temp-Banned</td>';
        break;
      } 
}
?>

Alternatively, you could have a look up table for state where the records are:

STATE
STATEID STATE
0       Unbanned
1       Banned
3       Temp-Banned

in your players table you have a foreign key for state where it just holds the StateId.

Change your query to be:

SELECT * FROM Players JOIN STATE on STATE.STATEID=Players.state WHERE...

I like breaking stuff like that out, it makes it easy to create selectable lists later and it's much more extensible in the long run if other possible states are created...like if you want to break Temp-Banned into several groups, 30-60 day ban, full season ban, etc, you just change the records in the table rather than having to search for all of your queries and switch statements.

1 Comment

You forgot the case in your switch() :-P
0

You could use the CASE statement in MYSQL OR if you don't have many states use the following code:

<?
          $a = array('0'=>'Unbanned','1'=>'Banned','3'=>'Temp-Banned');
          while($row = $result->fetch_assoc()){
          echo'<td>' . $row['player'] . '</td>';
          echo'<td>' . $row['admin'] . '</td>';
          echo'<td>' . $row['reason'] . '</td>';
          echo'<td>' . $row['created_at'] . '</td>';
          echo'<td>' . $row['expires_at'] . '</td>';
          echo'<td>' . $a[$row['state']] . '</td>' . '</tr>';
          }
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.