Greetings,

I have the following PHP code that generate Gender count and percentage distribution (with out put to table) from mysql database.

<?php
$link = mysql_connect("***", "****", "****");
mysql_select_db("****") or die(mysql_error());
$sql = "SELECT Gender, COUNT(*) AS total FROM New GROUP BY Gender WITH ROLLUP";
$result = mysql_query($sql);
$data = array();
while($row = mysql_fetch_assoc($result))
{
$key = !empty($row['Gender']) ? $row['Gender'] : 'Total';
$data[$key] = $row['total'];
}
$grandTotal = $data['Total'];
echo "<table width='50%' border='4' cellspacing='0' cellpadding='10' align='center'>";
echo "<tr><th>Gender</th><th>Number</th><th>Percent</th></tr>";
foreach($data as $gender => $total)
{
printf(
"<tr align='center'><td>%s</td><td>%d</td><td>%d%%</td></tr>",
$gender,
$total,
round($total/$grandTotal*100, 0)
);
}
echo "</table>";
?>

Now, I want to group the Gender by "Ethnicity". To display Gender nested under Ethnicity (keeping Ethnicity as column and Gender as row), how can I do that? I couldn't find any crosstabs in PHP. I tried group by also. My ethncity break down will be "Asian", "Black", and "White".

I would really appreciate any help?