我的用户表包含字段性别和名字,如此..存储在性别类型数据库中的值为M(男性)为0,F(女性)为1。 我正在从数据库中检索细节。
使用以下查询并使用以下代码在表格中显示详细信息:
我的问题是,如果来自用户表格的值为“0”,如何在性别类型列中显示“M”。
对于女性来说,如果性别类型列(如果值来自用户表格中的性别为“1”),我想要显示“F”。
任何人都可以帮忙吗?
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 }?>
对此有任何帮助
答案 0 :(得分: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
如果你把它放在查询中它返回M或F
答案 1 :(得分:1)
在回显性别之前添加此行:
$row['Gender'] = $row['Gender'] == 0 ? 'M' : 'F';
或者在编辑查询时使用macwadu的优秀解决方案。
答案 2 :(得分:1)
或者你可以在php端进行:
<td><?php echo ($row['Gender'] ? 'F' : 'M') ?></td>