根据性别在两列中显示来自MySQL数据库的结果

时间:2018-07-01 22:04:08

标签: php mysql

我想在两列中根据性别列出MySQL数据库中参与者的姓名。我无法弄清楚性别变化时如何开始新的一列,所以目前我只有一列,其中显示了所有的名字和姓氏,并以“ Male”和“ Female”为标题来分隔名称。性别变化后,如何获得新的栏目开始?我想这样做,以便当用户打印页面时可以全部保留在一页上。

这是我用于表格的代码:

<table width="700" border="0" cellspacing="1" cellpadding="0" align="center">
<tr>
<td>
<table width="700" border="0" cellspacing="0" cellpadding="3">

<thead>
<tr>
<td><strong></strong></td>
<td align="center"><strong></strong></td>
</tr>
</thead>

<?php
while($row = mysql_fetch_assoc($result)){
if($comp != $row['Gender']) echo "<tr><td colspan=\"6\"><b>".$row['Gender']."</b></td></tr>\r\n";

 
	echo "<tr><td>";	
	echo $row['rownumber'];
	echo "</td><td>";
	echo "$first2";
	echo " ";
	echo "$last2";
	echo "</td></tr>";
	
	
 
$comp = $row['Gender'];
}
}
echo "</table>";

这是我使用的查询:

$query = "
SELECT 
IF(@prev != tt.Gender, @rownum:=1, @rownum:=@rownum+1) as rownumber, @prev:=tt.Gender, tt.* 
FROM ((
	(SELECT comp1, first2, last2, Gender, DOB, Events, Events2, Events3, ID 
	FROM entries 
	WHERE (comp1='$chooser')
		) UNION 
	(SELECT comp2, first2, last2, Gender, DOB, Events, Events2, Events3, ID 
	FROM entries 
	WHERE (comp2='$chooser')
		) UNION
	(SELECT comp3, first2, last2, Gender, DOB, Events, Events2, Events3, ID 
	FROM entries 
	WHERE (comp3='$chooser')
		))) tt, 
	(SELECT @rownum := 0, @prev:='') r 
ORDER BY Gender, last2 ASC";

$result = mysql_query($query) or die(mysql_error());
$comp = "";

0 个答案:

没有答案