2013-11-21 75 views
0

在我的表courses中,我有幾個條目共享相同的​​值。我正在使用foreach循環來檢索具有相同​​的所有行。在下面的例子中,有一個學院有兩門課程。這些值顯示正確,但顯示了一些不必要的值。例如,它創建了兩個結果。是否有可能在wanted result to display部分有如下所示的組合結果?對於每個循環顯示來自mysql表的值

PHP

$academy_id = 123 

$db_select = $db_con->prepare(" 
SELECT ca.course_name, 
     ca.course_start_date, 
     ca.course_end_date 
FROM courses_by_academy ca 
WHERE ca.academy_id = :academy_id 
"); 
$final_result = ''; 
if (!$db_select) return false; 
    if (!$db_select->execute(array(':academy_id' => $academy_id))) return false; 
    $results = $db_select->fetchAll(\PDO::FETCH_ASSOC); 
    if (empty($results)) return false; 
    foreach ($results as $value){ 
    $final_result .= "<b>Academy Name: </b>".$value['name']."</br><b>Academy Type: </b>".$value['type']."</br><b>Status: </b>".$value['status']."</br>"; 
    $final_result .= "<b>Course Name: </b>".$value['course_name']."</br><b>Start Date: </b>".$value['course_start_date']."</br><b>End Date: </b>".$value['course_end_date']."</br>"; 
    } 
} 

表值

+----+------------+----------------------+---------------+------------+ 
| id | academy_id |  course_name  | start_date | end_date | 
+----+------------+----------------------+---------------+------------+ 
| 1 |  123 | Biology - Basic  | 2013-11-30 | 2013-12-25 | 
| 2 |  123 | Biology - Nutrition | 2014-01-15 | 2014-01-25 | 
+----+------------+----------------------+---------------+------------+ 

當前結果顯示

Academy Name: North Valley Schools 
Academy Type: Post-Secondary 
Status: INACTIVE 
ID: 123 
Course Name: Biology - Basic 
Start date: 2013-11-30 
End date: 2013-12-25 

Academy Name: North Valley Schools 
Academy Type: Post-Secondary 
Status: INACTIVE 
ID: 123 
Course Name: Biology - Nutrition 
Start date: 2014-01-15 
End date: 2014-01 

誠徵Result-聯合

Academy Name: North Valley Schools 
Academy Type: Post-Secondary 
Status: INACTIVE 
ID: 123 
Course Name: Biology - Basic 
Start date: 2013-11-30 
End date: 2013-12-25 
Course Name: Biology - Nutrition 
Start date: 2014-01-15 
End date: 2014-01-25 
+1

您顯示查詢結果集中不存在的列'$ value ['name']'和'$ value ['type'] for academy name/type。您目前如何讓顯示屏實際按照您所顯示的方式工作?其次,如果你一次只查詢一個學院,爲什麼要輸出循環內的學院信息呢?只需將循環學院信息顯示在循環之外,並將特定於班級的內容留在循環中即可解決問題。 –

+0

@MarkBryant好點。它如何看起來像循環外的學院信息?我的頭腦無法把握這個概念 –

回答

1

你的循環:

foreach ($results as $value){ 
    $final_result .= "<b>Academy Name: </b>".$value['name']."</br><b>Academy Type: </b>".$value['type']."</br><b>Status: </b>".$value['status']."</br>"; 
    $final_result .= "<b>Course Name: </b>".$value['course_name']."</br><b>Start Date: </b>".$value['course_start_date']."</br><b>End Date: </b>".$value['course_end_date']."</br>"; 
} 

,而不是做:

$first = true; 
foreach ($results as $value){ 
    if($first){ 
     $first = false; 
     $final_result .= "<b>Academy Name: </b>".$value['name']."</br><b>Academy Type: </b>".$value['type']."</br><b>Status: </b>".$value['status']."</br>"; 
    } 
    $final_result .= "<b>Course Name: </b>".$value['course_name']."</br><b>Start Date: </b>".$value['course_start_date']."</br><b>End Date: </b>".$value['course_end_date']."</br>"; 
} 

它只會執行一次學院的信息。 :)

+0

這是完美的! –

+0

當然是!我寫的。 ;)嘿。 J/K。我很高興我能幫上忙。 – Fallenreaper