Foreach循环以特定格式显示值

时间:2013-11-25 08:24:30

标签: php

我有两个foreach循环,其中从mysql数据库表中获取结果。值正在显示但我正在尝试压缩结果,因此它不会重复值。例如,表格中有两个courses和两个instructors。这意味着当我运行循环时,我希望显示总共4个结果。但是在完成循环后,总共显示了8个结果。但额外的结果是重复。在进入下一个循环之前,可以完全循环一个foreach循环吗?或者最好的方法是什么才能准确显示4个结果?

$db_select  = $db_con->prepare("
SELECT      a.academy_id,
           ca.course_name,
           ca.course_start_date,
           ca.course_end_date,
           p.first_name,
           p.last_name
    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 = 123
");
if (!$db_select) return false;
    if (!$db_select->execute(array('123' => $acad_id))) return false;
    $results = $db_select->fetchAll(\PDO::FETCH_ASSOC);
    if (empty($results)) return false;
    $final_result = '';
    $final_result2 = '';

    foreach ($results as $value){
          $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>";      
    }

    foreach ($results as $value2){
          $final_result2 .= "---------------------STAFF-----------------------</br>";
          $final_result2 .= "<b>First Name: </b>".$value2['first_name']."</br><b>Last Name: </b>".$value2['last_name']."</br>";
    }

}

当前回声显示结果:

-------------------COURSES_OFFERED------------------
Course Name: Biology
Start Date: 2013-11-15
End Date: 2013-11-27
-------------------COURSES_OFFERED------------------
Course Name: Biology
Start Date: 2013-11-15
End Date: 2013-11-27
-------------------COURSES_OFFERED------------------
Course Name: Calculus
Start Date: 2013-11-16
End Date: 2013-11-30
-------------------COURSES_OFFERED------------------
Course Name: Calculus
Start Date: 2013-11-16
End Date: 2013-11-30
--------------------STAFF-----------------------
First Name: Person1
Last Name: Last1
---------------------STAFF-----------------------
First Name: Person2
Last Name: Last2
---------------------STAFF-----------------------
First Name: Person2
Last Name: Last2
---------------------STAFF-----------------------
First Name: Person1
Last Name: Last1

寻求这样的结果:

-------------------COURSES_OFFERED------------------
Course Name: Biology
Start Date: 2013-11-15
End Date: 2013-11-27
-------------------COURSES_OFFERED------------------
Course Name: Calculus
Start Date: 2013-11-16
End Date: 2013-11-30
--------------------STAFF-----------------------
First Name: Person1
Last Name: Last1
---------------------STAFF-----------------------
First Name: Person2
Last Name: Last2

2 个答案:

答案 0 :(得分:0)

由于你在每个迭代中连接字符串,因此值将被加到变量中。因此,在每次迭代之后,变量中的值不会被清除,因为它是追加的。所以在循环中对变量进行初始化将解决你的问题。万阿英,蒋达清。

foreach ($results as $value){
          $final_result = '';
          $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>";      
    }

    foreach ($results as $value2){
          $final_result2 = '';
          $final_result2 .= "---------------------STAFF-----------------------</br>";
          $final_result2 .= "<b>First Name: </b>".$value2['first_name']."</br><b>Last Name: </b>".$value2['last_name']."</br>";
    }

答案 1 :(得分:0)

你的sql会产生4行

p1 biology
p2 biology
p1 calculus
p2 Calculus

尝试使用LEFT JOIN而不是INNER JOIN