Arylis
Arylis

Reputation: 5

Changing digits in mysql to letters to echo in php

In regard to a previous question I just managed to get solved by a very generous person. I have but 1 more issue. I'm echo'ing the Online Users logged into my Website; however I also want to echo their Gender. Now the problem is, the Gender's are stored as 0 & 1 instead of male and female. I have tried but I can't seem to get it working... my code;

$result = mysql_query("SELECT DISTINCT name, gender FROM online INNER JOIN base ON  online.ID = base.id limit 8 "); 
switch($gender)
{
    case 0: $gen='Male'; break;
    case 1: $gen='Female'; break;
    default: $gen='Undecided'; break;
}
echo "<table class='table table-hover' width='300'><tr></tr>";
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['gender'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "</tr>";
}
echo "</table>";

This script does not work, but perhaps someone can shine some light upon a solution? I'd be very grateful!

Upvotes: 0

Views: 103

Answers (5)

Mr. Llama
Mr. Llama

Reputation: 20909

Instead of handling it from PHP, you could make your RDBMS handle it for you.

Use the following SQL instead (newlines added for formatting):

SELECT DISTINCT
    name,
    CASE gender
        WHEN 0 THEN 'Male'
        WHEN 1 THEN 'Female'
        ELSE 'Undecided'
    END CASE AS gender
FROM online
INNER JOIN base
ON online.ID = base.id
LIMIT 8

Upvotes: 0

AbraCadaver
AbraCadaver

Reputation: 79024

You could do it in the query:

SELECT DISTINCT name, IF(gender=0,'Male','Female') AS real_gender
FROM online INNER JOIN base ON online.ID = base.id limit 8

Then use $row['real_gender'] in the echo code.

If you ever need more cases use:

SELECT column_name, CASE WHEN column_name IS 0 THEN 'something'
                         WHEN column_name IS 1 THEN 'something else'
                         ELSE 'something diff' END AS some_name
                         FROM table_name

Upvotes: 1

Dana Hutchins
Dana Hutchins

Reputation: 44

You could use an array:

$gender[0] = "Male";
$gender[1] = "Female";

$result = mysql_query("SELECT DISTINCT name, gender FROM online INNER JOIN base ON  online.ID = base.id limit 8 "); 
echo "<table class='table table-hover' width='300'><tr></tr>";
while($row = mysql_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $gender[$row['gender']] . "</td>";
    echo "<td>" . $row['name'] . "</td>";
    echo "</tr>";
}
echo "</table>";

Upvotes: 1

Sasanka Panguluri
Sasanka Panguluri

Reputation: 3138

switch($gender) { case 0: $gen='Male'; break; case 1: $gen='Female'; break; default: $gen='Undecided'; break; }

where is $gender defined?

I think you need to put this switch in the while ( ) loop and use $switch($row['gender'])

Upvotes: 0

barell
barell

Reputation: 1489

$result = mysql_query("SELECT DISTINCT name, gender FROM online INNER JOIN base ON online.ID = base.id limit 8 "); 

function getGenderText($genderId) {
    $genders = array('Male', 'Female');
    return isset($genders[$genderId]) ? $genders[$genderId] : 'Undecided';
}

echo "<table class='table table-hover' width='300'><tr></tr>";
while($row = mysql_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . getGenderText($row['gender']) . "</td>";
    echo "<td>" . $row['name'] . "</td>";
    echo "</tr>";
}
echo "</table>";

Upvotes: 1

Related Questions