I have a mySQL table which can be simplified down to something like this:
animal breed
Cat Persian
Cat Siamese
Dog Alsatian
Dog Poodle
I want to display the information in this form:
<table>
<tr>
<td class="heading">Cat</td>
</tr>
<tr>
<td class="body">Persian</td>
<td class="body">Siamese</td>
</tr>
<tr>
<td class="heading">Dog</td>
</tr>
<tr>
<td class="body">Alsatian</td>
<td class="body">Poodle</td>
</tr>
</table>
So far, I have a query like this:
$query = "SELECT * FROM aa_animal";
$result = mysql_query($query, $MySQL_extranet) or die(mysql_error());
while($row = mysql_fetch_assoc($result)){
$resultarray[] = $row;
}
Then I think I am going to need a pair of nested foreach() loops, but it is the handling of the multi-dimensional array that gets me hopelessly lost.
foreach("unique_animal") {
<tr>
<td class-"heading">$animal</td>
</tr>
if ($animal=="unique_animal") {
foreach("row") {
<tr>
<td class-"body">$breed</td>
</tr>
}
}
How do I manipulate $resultarray to get the right values for $animal, $breed, etc?
PROGRESS SO FAR
Justin's reply provided the solution to the problem as described above. However the real-life page has a lot more columns in the database table. Here is the code for a halfway file between the simple example I gave originally and the page I am trying to create.
<?php
$countryid='spain';
// MySQL query to select hotels and display all the hotel info
$query = "SELECT hid, hotel, pricefrom, place, region, country, picture, property, prop2, rooms, summary, lat, lng, zoomloc, breakfast, searchparam, specialoffer, biz_model, ta_rating, bc_rating FROM ex_hotel WHERE country LIKE '%$countryid%' AND showhide = 'show' ORDER BY orderr DESC";
$result = mysql_query($query, $MySQL_extranet) or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
if (isset($resultarray[$row['region']])) {
$resultarray[$row['region']][] = $row['hotel'];
}else{
$resultarray[$row['region']] = array($row['hotel']);
}
}
ksort($resultarray);
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<table width="500" border="1" cellspacing=" 4" cellpadding="4">
<?php foreach($resultarray as $region => $hotel)
{
echo '<tr><td colspan="2" style="background-color:#ddd">'.$region.'</td></tr>';
foreach($hotel as $value) {
echo '<tr><td width="250px">'.$value.'</td>';
}
foreach($hid as $value2) {
echo '<td>'.$value2.'</td></tr>';
}
}?>
</table>
</body>
</html>
All those values in the mySQL query need to be used in the table, but how do I get them?
foreachsyntax: nl3.php.net/manual/en/control-structures.foreach.php.