0

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!

Arylis
  • 5
  • 1
  • 4
  • Try converting $gender to int before testing. – wonce Jun 02 '14 at 20:02
  • If I were you I would use if/else instead of switch/case. Also you've got a lot of html in there it might be better to write the html outside of php tags – Dan Jun 02 '14 at 20:02
  • 1
    you can make a function with the switch part and return the $gen, and in the display call that function with the DB gender value. – Abhik Chakraborty Jun 02 '14 at 20:03
  • Why not use an `array` look-up table for this? It's also worth noting that the deprecated `mysql_query` interface shouldn't be used in new applications, it's being removed from PHP in the near future. – tadman Jun 02 '14 at 21:25

5 Answers5

1
$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>";
barell
  • 1,470
  • 13
  • 19
1

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
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
1

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>";
0

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'])

Sasanka Panguluri
  • 3,058
  • 4
  • 32
  • 54
0

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
Mr. Llama
  • 20,202
  • 2
  • 62
  • 115
  • This seems a lot easier and smaller to use than what I have down at the moment, will try it out even though I have it working! – Arylis Jun 02 '14 at 20:19