简体   繁体   English

PHP循环遍历多个数据库表

[英]PHP loop through multiple database tables

let's say I have a database structure like below: 假设我有一个如下所示的数据库结构:

Students 学生们

+--------------+--------------------+-------------------+
| student_id   | student_firstname  | student_lastname  |
+--------------+--------------------+-------------------+
| 1            | John               | Doe               |
+--------------+--------------------+-------------------+
| 2            | Lisa               | Doe               |
+--------------+--------------------+-------------------+
| 3            | Derp               | Doe               |
+--------------+--------------------+-------------------+

Absence 缺席

+--------------+--------------------+-------------------+---------------+
| absence_id   | absence_student_id | absence_date      | absence_note  |
+--------------+--------------------+-------------------+---------------+
| 1            | 2                  | 2012-06-10        | A note...     |
+--------------+--------------------+-------------------+---------------+
| 2            | 3                  | 2012-06-30        | Another note. |  
+--------------+--------------------+-------------------+---------------+

And a relationship between column students.student_id and absence.absence_student_id 与列之间的关系students.student_idabsence.absence_student_id

In PHP I would like to loop through the above tables and get a complete output of the rows in students and if there is a record in absence matching that student, do something to that row and then continue the loop. 在PHP中,我想循环遍历上面的表并获得students行的完整输出,如果absence匹配该学生的记录,则对该行执行某些操作然后继续循环。

My desired HTML output would be something like this: 我想要的HTML输出是这样的:

<table>
 <thead>
  <tr>
    <td>Name:</td>
    <td>Absence date:</td>
    <td>Absence note:</td>
  </tr>
 </thead>

 <tr>
    <td>John Doe</td>
    <td></td>
    <td></td>
 </tr>

 <tr>
    <td>Lisa Doe</td>
    <td>2012-06-10</td>
    <td>A note...</td>
 </tr>

 <tr>
    <td>Derp Doe</td>
    <td>2012-06-30</td>
    <td>Another note.</td>
 </tr>
</table>

I use to loop like this: 我用这样循环:

<?php
  while ($row = mysql_fetch_array($students_sql_query)) {
   echo "<tr><td>".$row['student_firstname']."</td></tr>";
 }
?>

Is it possible to add some kind of if statement to this? 是否可以为此添加某种if语句? Would I need two different MySQL querys and some kind of foreach loop instead? 我需要两个不同的MySQL查询和某种foreach循环吗? I'm relatively new to web programming... Thank's in advance and sorry for a long post. 我对网络编程比较陌生......提前谢谢,很抱歉很长的帖子。

You'll need to join the tables in a query and then get the results from the query. 您需要在查询中加入表,然后从查询中获取结果。

SELECT Students.student_firstname
   , Students.student_lastname 
   , Absence.absence_date 
   , Absence.absence_note  
FROM Students
LEFT JOIN Absence ON Students.id = Absence.absence_student_id

You should then be able to loop through it similar to this: 然后你应该能够循环遍历它,类似于:

<?php
    while ($row = mysql_fetch_array($students_sql_query)) 
    {
?>
        <tr>
            <td>$row['student_firstname']</td>
            <td>$row['student_lastname']</td>
            <td>$row['absence_date']</td>
            <td>$row['absence_note']</td>
        </tr> 
<?
    }
?>

The left join will tell the database to pull in all the students and if they have absences it will populate those too. 左连接将告诉数据库拉入所有学生,如果他们缺席,它也会填充。 If the student doesn't have an absence, that will return as null from the database. 如果学生没有缺席,那么将从数据库返回null。 When it is displayed in the table row in the loop, it should display as an empty column if there is nothing in the absence table for that student. 当它显示在循环中的表行中时,如果该学生的缺席表中没有任何内容,则它应显示为空列。

The only downside is if a student has multiple absences it will duplicate rows in the display table. 唯一的缺点是如果学生有多次缺席,它将在显示表中复制行。 In that case, you'll have to do some pre-processing either in the code or in the display to handle that condition. 在这种情况下,您必须在代码或显示中进行一些预处理以处理该情况。

Using pdo would be as below: 使用pdo如下:

 $sql = $dbh->prepare('
        SELECT student_firstname, student_lastname, absense_date, absense_note 
        FROM students s inner join absense a 
        ON s.student_id=a.absense_student_id
        ');
        $sql->execute();
        $result = $sql->fetchAll(); <----- fetch All the data
        return $result;

     foreach($result as $row)
            {
            echo '<tr>';
            echo '<td>'.$row['student_firstname'].' '.$row['student_lastname'].'</td>';
            echo '<td>'.$row['absense_date'].'</td>';
            echo '<td>'.$row['absense_note'].'</td>';
            echo '</tr>';
            }

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

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