我正在使用foreach循環來獲取結果,但我得到不必要的重複結果顯示。下面你會看到我獲得的值超過兩次。在我的查詢select語句中,我在表之間進行內部連接。然後我做一個foreach
循環來顯示這些結果。結果從三個表格顯示:學院,courses_by_academy和person。他們都分享一個外鍵academy_id。我不確定這是否與查詢或foreach循環的格式一致。我如何顯示下面顯示的信息而沒有任何重複顯示?避免從選擇語句顯示重複的值
PHP
$db_select = $db_con->prepare("
SELECT a.name,
a.academy_id,
ca.course_name,
ca.course_start_date,
ca.course_end_date,
p.contact_role,
p.instructor_role,
p.first_name,
p.last_name,
p.person_email,
p.person_phone,
p.person_fax
FROM academy a
INNER JOIN courses_by_academy ca ON a.academy_id = ca.academy_id
INNER JOIN person p ON a.academy_id = p.academy_id
WHERE a.academy_id = :acad_id
");
if (!$db_select) return false;
if (!$db_select->execute(array(':acad_id' => $acad_id))) return false;
$results = $db_select->fetchAll(\PDO::FETCH_ASSOC);
if (empty($results)) return false;
$final_result = '';
$first = true;
foreach ($results as $value){
if($first){
$first = false;
$final_result .= "<b>Academy Name: </b>".$value['name']."<b> ID: </b>".$value['academy_id']."</br>";
}
$final_result .= "-------------------COURSES_OFFERED------------------</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>";
$final_result .= "---------------------PERSONEL-----------------------</br>";
$final_result .= "<b>First Name: </b>".$value['first_name']."</br><b>Last Name: </b>".$value['last_name']."</br><b>Email: </b>".$value['person_email']."</br>";
$final_result .= "<b>This person has the role of an instructor: </b>".$value['instructor_role']."</br><b>This person has the role of a contact: </b>".$value['contact_role']."</br>";
$final_result .= "<b>Phone: </b>".$value['person_phone']."</br><b>Fax: </b>".$value['person_fax']."</br>";
}
}
顯示所需的方式 -
-------------------COURSES_OFFERED------------------
Course Name: Biology
Start Date: 2013-11-13
End Date: 2013-11-26
-------------------COURSES_OFFERED------------------
Course Name: Calculus
Start Date: 2013-11-19
End Date: 2013-11-16
---------------------PERSONEL-----------------------
First Name: Person1
Last Name: Last1
Email: [email protected]
This person has the role of an instructor: 1
This person has the role of a contact: 0
Phone: 1234567890
Fax: 1234567890
---------------------PERSONEL-----------------------
First Name: Person2
Last Name: Last2
Email: [email protected]
This person has the role of an instructor: 0
This person has the role of a contact: 1
Phone: 1234567890
Fax: 1234567890
目前顯示:
-------------------COURSES_OFFERED------------------
Course Name: Biology
Start Date: 2013-11-13
End Date: 2013-11-26
---------------------PERSONEL-----------------------
First Name: Person1
Last Name: Last1
Email: [email protected]
This person has the role of an instructor: 1
This person has the role of a contact:
Phone: 1234567890
Fax: 1234567890
-------------------COURSES_OFFERED------------------
Course Name: Calculus
Start Date: 2013-11-19
End Date: 2013-11-16
---------------------PERSONEL-----------------------
First Name: Person1
Last Name: Last1
Email: [email protected]
This person has the role of an instructor: 1
This person has the role of a contact:
Phone: 1234567890
Fax: 1234567890
-------------------COURSES_OFFERED------------------
Course Name: Biology
Start Date: 2013-11-13
End Date: 2013-11-26
---------------------PERSONEL-----------------------
First Name: Person2
Last Name: Last2
Email: [email protected]
This person has the role of an instructor: 0
This person has the role of a contact: 1
Phone: 1234567890
Fax: 1234567890
-------------------COURSES_OFFERED------------------
Course Name: Calculus
Start Date: 2013-11-19
End Date: 2013-11-16
---------------------PERSONEL-----------------------
First Name: Person2
Last Name: Last2
Email: [email protected]
This person has the role of an instructor: 0
This person has the role of a contact: 1
Phone: 1234567890
Fax: 1234567890
表值
你爲什麼說你有重複?你在圖像中顯示的四行都是不同的。 – David