简体   繁体   English

对于每个循环以显示mysql表中的值

[英]For each loop to display values from mysql table

In my table courses I have several entries that share the same academy_id value. 在我的餐桌courses我有几个条目具有相同的academy_id值。 I am using a foreach loop to retrieve all the rows with the same academy_id . 我正在使用foreach循环来检索具有相同academy_id所有行。 In the example below, there is an academy that has two courses. 在下面的示例中,有一所学院有两门课程。 The values are displaying correctly but there are some unnecessary values shown. 这些值显示正确,但是显示了一些不必要的值。 For example it creates two results. 例如,它创建两个结果。 Is it possible to have one combined result like shown below on the wanted result to display section? 是否可以在wanted result to display部分中显示如下所示的组合结果?

PHP 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>";
    }
}

Table Values 表值

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

Current Result Displaying 当前结果显示

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

Wanted Result- Combined 想要的结果-合并

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

your loop: 您的循环:

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>";
}

instead do: 而是:

$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>";
}

it will execute the academy information only once. 它只会执行一次学院信息。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM