2011-07-30 73 views
0

小組和分組我有表數據庫:的foreach在PHP

Group: 
    | id | Category | title | 
    | 1 | 1  | group1 | 
    | 2 | 2  | group2 | 
    | 3 | 1  | group3 | 
    | 4 | 3  | group4 | 
    | 5 | 2  | group5 | 
    | 6 | 1  | group6 | 

News: 
    | id | Group | title | body | 
    | 1 | 3  | title1 | body1 | 
    | 2 | 2  | title2 | body2 | 
    | 3 | 1  | title3 | body3 | 
    | 4 | 4  | title4 | body4 | 
    | 5 | 1  | title5 | body5 | 
    | 6 | 5  | title6 | body6 | 
    | 7 | 3  | title7 | body7 | 
    | 8 | 2  | title8 | body8 | 
    | 9 | 1  | title9 | body9 | 
    | 10 | 6  | title10| body10 | 
    | 11 | 1  | title11| body11 | 
    | 12 | 5  | title12| body12 | 

我該怎麼讓這個爲:

-GROUP1, GROUP3 and GROUP6 
//GROUP1 (category1) 
--title3 
--title5 
--title9 
//GROUP3 (category1) 
--title1 
--title7 
//GROUP6 (category1) 
--title10 
-GROUP2 and GROUP5 
//GROUP2 (category2) 
--title2 
--title8 
//GROUP5 (category2) 
--title6 
--titl12 
-GROUP4 
//GROUP4 (category3) 
--title4 

我會的foreach使這個。感謝幫助!

+0

那麼,什麼是賞金? (:?這是衆所周知的根本任務及其算法也是知名 – heximal

+0

請告訴我問題/問題只是發佈了一堆代碼,更多的是一種懶惰的標誌,但不是一個問題 – KingCrunch

+1

@KC懶惰是一個問題 –

回答

1

您的具體要求的輸出,所以複雜。

$sql = 'SELECT n.title, n.Group AS group_id, g.Category AS cat_id 
     FROM News AS n 
     JOIN Group AS g ON g.id = group_id 
     ORDER BY cat_id, group_id, n.id'; 

$result = mysql_query($query); 

$categories = array(); 

while ($row = mysql_fetch_assoc($result)) { 
    $catID = $row['cat_id']; 
    $groupID = $row['group_id']; 
    $title = $row['title']; 

    $categories[$catID]['groups'][$groupID]['titles'][] = $title; 
} 

foreach ($categories as $catID => $groups) { 

    $catGroups = '-GROUP'.implode(', GROUP',array_keys($groups)).PHP_EOL; 
    $lastComma = strrpos($catGroups,','); 
    if ($lastComma !== false) { 
     $catGroups = substr($catGroups,0,$lastComma-1). 
        ' AND ' .substr($catGroups,$lastComma+1); 
    } 
    echo $catGroups; 

    foreach ($groups as $groupID => $titles) { 
     echo "//GROUP$groupID (category$catID)".PHP_EOL; 
     foreach ($groups as $group => $titles) { 
      echo '--'.$title.PHP_EOL; 
     } 
    } 
} 

如果你不需要這種奇特的輸出,這將會簡單得多。

$sql = 'SELECT n.title, n.Group AS group_id, g.Category AS cat_id 
     FROM News AS n 
     JOIN Group AS g ON g.id = group_id 
     ORDER BY cat_id, group_id, n.id'; 

$result = mysql_query($query); 

$lastCatID = null; 
$lastGroupID = null; 

while ($row = mysql_fetch_assoc($result)) { 
    $catID = $row['cat_id']; 
    $groupID = $row['group_id']; 
    $title = $row['title']; 

    if ($catID !== $lastCatID){ 
     echo "*** CATEGORY $catID\n"; 
     $lastCatID = $catID; 
    } 
    if ($groupID !== $lastGroupID){ 
     echo "GROUP $groupID\n"; 
     $lastGroupID = $groupID; 
    } 
    echo "-- $title\n"; 
} 
+0

非常感謝您的幫助:)這很難,但是謝謝:) –

1

你告訴你,你在數據庫中有你的價值。所以你必須先拿到他們,例如通過以下數據庫查詢:

SELECT 
    g.`title` AS `group_title` 
    , n.`title` AS `news_title` 
FROM 
    `Group` AS g 
INNER JOIN 
    `News` AS n 
ON 
    g.`id` = n.`Group` 
ORDER BY 
    g.`Category` 
    , n.`Group` 
    , n.`title` 

將數據存儲在數組中。現在你可以使用foreach循環遍歷數組。

=== 這裏我更新:

首先填充數組,而從數據庫(例如查詢見上文)讀書。

<?php 
$data = array(); 
$res = mysql_query('SELECT ...'); 
while (($row = mysql_fetch_assoc($res)) !== false) { 
    $data[$row['group_title']][] = $row['news_title']; 
} 
?> 

則數組寫入到屏幕上:

<?php 
foreach ($data as $group_title => $groups) { 
    echo $group_title . "\n"; 
    foreach ($groups as $news) { 
     echo "\t" . $news . "\n"; 
    } 
} 
?> 
+0

這我只想獲得數據庫,並且我想添加PHP組名稱。<?php echo $ groupName;?><?php echo $ title etc?> –