2014-02-08 58 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

不像你說的。在不知道其他列的實用程序以及您想要對它們做什麼的情況下很難給出答案 –