0

I have user table having fields gender and first name like this.. The value stored in the database for gender type is 0 for M(Male) and 1 for F(Female).
I am retrieving the details from database.

By using the following query and displaying the details in table using below code:

My problem is how do I display the 'M' in gender type column if the value is coming from user table for gender is '0'.
And same for Female is I want to display the 'F' if the gender type column if the value is coming from user table for gender is '1'.

Can anyone help on this one?

      modified code :


               GOT an error :Parse error: syntax error, unexpected $end 


               <?php
              $rows=array();
            $query = "SELECT CONCAT(usrFirstname,'',usrSurname) As FullName,usrNickname AS    Nickname,";
            $query.= "usrEmail As EmailAddress,usrGender AS Gender,DATE_FORMAT(usrDOB,'%d%m%y') As DOB,usrBelt AS BeltId";
   $query.= " FROM user";
   $result = mysql_query($query);
 echo mysql_error();
      if($result)
       {
        while($row=mysql_fetch_assoc($result))
  {
       $rows[] = $row;
     }
   }
      <?php 
       if ($row['Gender'] == '0'){
        $Gender = 'M';
           } else {
         $Gender = 'F';

        ?>

     <link href="../../css/styles.css" rel="stylesheet" type="text/css" />
      <div class="subheader" style="margin:16px 0 0;width:980px font-style:bold"><div        class="subheaderl"></div><div class="subheaderr"></div>Users registered at your     facility</div>
       <div class="div" style="overflow-y:scroll;height:500px">
          <table name="t" id ="t" height= "140" width="800">





     <tr style="text-align:left; line-height=10px; word-spacing:0px">
         <thead style="font-weight:bold; font-size:12px;">  
          <th>Full Name</th>
          <th>NickName</th>
             <th>Email Address</th>
              <th>Gender</th>
           <th>DOB</th>
             <th>Belt ID</th>      
       </thead>  
         <?php foreach ($rows as $row){?>
            <tr style="font-size:small">
               <td><?php echo $row['FullName']?></td>
              <td><?php echo $row['Nickname']?></td>
    <td><?php echo $row['EmailAddress']?></td>
    <td><?php echo $Gender?></td>       
    <td><?php echo $row['DOB']?></td>
    <td><?php echo $row['BeltId']?></td>
  </tr>
 <?php }?>

would any one help on this

2
  • 1) Please don't shout 2) This is an exact duplicate of the question you posted moments ago. Edit the existing question to add more details, don't post a new one 3) The answer to this one is the same I already posted in the previous question. Commented Aug 12, 2011 at 9:31
  • You forget the closing brackets } after $Gender = 'F'. Commented Aug 12, 2011 at 9:52

3 Answers 3

2

SELECT CONCAT(usrFirstname,'',usrSurname) As FullName, usrNickname AS Nickname, usrEmail As EmailAddress, CASE WHEN usrGender = 0 THEN 'M' ELSE 'F' END AS Gender, DATE_FORMAT(usrDOB,'%d%m%y') As DOB,usrBelt AS BeltId FROM user

if you put this in the query it return M or F

Sign up to request clarification or add additional context in comments.

1 Comment

No problem, that's why we're here :)
1

Add this line, right before you echo out the gender:

$row['Gender'] = $row['Gender'] == 0 ? 'M' : 'F';

Or use macwadu's excellent solution, on editing the query.

Comments

1

or you can do it on php-side:

<td><?php echo ($row['Gender'] ? 'F' : 'M') ?></td>

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.