2014-02-08 59 views
1

我有可以简化下降到这样的事情一个MySQL表:使用的foreach()与多维数组

animal  breed 
Cat   Persian 
Cat   Siamese 
Dog   Alsatian 
Dog   Poodle 

我想显示这种形式的信息:

<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> 

到目前为止,我有这样一个查询:

$query = "SELECT * FROM aa_animal"; 
$result = mysql_query($query, $MySQL_extranet) or die(mysql_error()); 
while($row = mysql_fetch_assoc($result)){ 
    $resultarray[] = $row; 
} 

然后,我想我需要一对嵌套的foreach()循环,b这是多维数组的处理,让我绝望地失去了。

foreach("unique_animal") { 
    <tr> 
    <td class-"heading">$animal</td> 
    </tr> 
    if ($animal=="unique_animal") { 
    foreach("row") { 
    <tr> 
     <td class-"body">$breed</td> 
    </tr> 
    } 
} 

我该如何操作$ resultarray来获取$ animal,$ breed等的正确值?

迄今进展

如上所述Justin的答复提供的解决问题的办法。然而,真实生活页面在数据库表中有更多的列。这是我最初给出的简单示例和我尝试创建的页面之间的中途文件的代码。

<?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> 

mySQL查询中的所有这些值都需要在表中使用,但是如何获取它们?

+0

第一件事,我相信你应该看看'foreach'语法:http://nl3.php.net/manual/en/control- structures.foreach.php。 – lucke84

+0

你也应该考虑订购/分组查询结果,如果你想先显示所有的猫,然后所有的狗等 – lucke84

回答

2

您获取

while($row = mysql_fetch_assoc($result)) 
{ 
    if (isset($resultarray[$row['animal']])) 
     $resultarray[$row['animal']][] = $row['breed']; 
    else 
     $resultarray[$row['animal']] = array($row['breed']); 
} 

然后当应组数据,显示它

foreach($resultarray as $animal => $breed) 
{ 
    echo '<tr><td class="heading">'.$animal.'</td></tr>'; 

    foreach($breed as $value) 
     echo '<tr><td class="body">'.$value.'</td></tr>'; 
} 

编辑: 如果你需要更多的列,你必须做的是存储整行而不是一个项目:

while($row = mysql_fetch_assoc($result)) 
{ 
    if (isset($resultarray[$row['region']])) 
     $resultarray[$row['region']][] = $row; 
    else 
     $resultarray[$row['region']] = array($row); 
} 

然后,你也可以先获取这些行

foreach($resultarray as $region => $rows) 
{ 
    echo '<tr><td class="heading">'.$region.'</td></tr>'; 

    // this way (display everything) 
    foreach($rows as $row) 
     foreach($row as $key => $value) 
     echo '<tr><td class="body">'.$key.': '.$value.'</td></tr>'; 

    // or this way (if you want to display only specific item) 
    foreach($rows as $row) 
     echo '<tr><td class="body">Hotel: '.$row["hotel"].'/price: '.$row["pricefrom"].'</td></tr>'; 
} 
+1

令人惊叹!我只是复制并粘贴Justin的代码,甚至没有停下来理解它,而且它工作。谢谢,贾斯汀。现在我所要做的就是扩大逻辑,在我的真实生活页面上工作。 – TrapezeArtist

+0

不可避免地,我在试图扩大规模时遇到了一个问题。实际上,桌子上大约有10或15列,而不仅仅是两列。我如何处理它们?是否有可能做类似 foreach($ resultarray为$ animal => $ breed,$ legs,$ fur)从其他列创建变量? – TrapezeArtist

+0

不像你说的。在不知道其他列的实用程序以及您想要对它们做什么的情况下很难给出答案 –