Dan Hammond Dan Hammond - 22 days ago 5
MySQL Question

Replace mysql data when printing to text

Currently when someone loggs out of a game it goes into the database with there game mode

modes:
0 = easy
1 = noive
2 = legendary


on my highscores for modes im trying to get it to replace
"0" to "easy" "1" to say novice and ofc "2" to say legendary

enter image description here

My code:

$skill_xp = strtolower($skill.'_xp');
$min = $page == 1 ? 0 : ($page * 25) - 25;
$res = $con->query("SELECT * FROM hs_users ORDER BY $skill_xp DESC LIMIT $min, 25");
if ($res->num_rows > 0) {
$rank = ($page * 25) - 24;
while ($row = $res->fetch_assoc()) {
$level = $skill_xp == 'overall_xp' ? getTotalLevel($row, $skills) : getLevelForXp($row[$skill_xp], $skill);
echo '
<tr>
<td>'.$rank.'</td>
<td><a href="?player='.$row['username'].'">'.$row['username'].'</a></td>
<td><a href="?player='.$row['difficulty'].'">'.$row['difficulty'].'</a></td>
<td style="text-align:right;">'.getLevel($row).'</td>
<td style="text-align:right;">'.number_format($level).'</td>
<td style="text-align:right;">'.number_format($row[$skill_xp]).'</td>
</tr>';
$rank++;
}
}

Answer

You can use switch for this.

Create a function like so:

function translateSkill( $skill ){
switch( $skill ){
    case 0:
    return "Easy";
    break;
    case 1: 
    return "Novice";
    break;
    case 2:
    return "Legen ... wait for it ... DARY!";
    break;
}
}

and in your <td> instead of:

<td><a href="?player='.$row['difficulty'].'">'.$row['difficulty'].'</a></td>

you use:

<td><a href="?player='.$row['difficulty'].'">'.translateSkill( $row['difficulty'] ).'</a></td>