嵌套While While循环显示第一行数据

时间:2012-11-10 01:31:11

标签: php mysql

我是一名新手PHP程序员,并且遇到了“嵌套”想法的问题。当我运行以下代码时,它将显示从表中的$ query3(FirstName,LastName等)列出的每个人,但是当它显示第二个和第三个while循环时(分别使用$ query1和$ query2),它只会执行它在第一个记录(列出的第一个员工)但没有其他记录。我是否需要/如何为每个循环实例重置$ employeeID变量,或者我需要在哪里进行更改?

//pen some querying
$query1 = mysql_query("SELECT * FROM employees WHERE position='Supervisor' OR position='Area Manager' ORDER BY area, LastName, EmployeeNumber ");
$query2 = mysql_query("SELECT * FROM meetings WHERE meetingGroup='Management'");


//now write some wicked sick code that will change the world!
while($rows3 = mysql_fetch_array($query1)):
    $firstname = $rows3['FirstName'];
    $lasttname = $rows3['LastName'];
    $area = $rows3['area'];
    $employeeID = $rows3['EmployeeNumber'];

echo "<table border='1' align='center' width='75%'><tr><td width='25%'>$firstname $lasttname</td>";

        while($rows1 = mysql_fetch_array($query2)):
            $meetingID = $rows1['meetingID'];
            $meetingName = $rows1['meetingName'];

            echo "<td width='19%'>$meetingName</td>";

            $query3 = mysql_query("SELECT * FROM meetingattendance WHERE employeeid='$employeeID' AND meetingid='$meetingID'");
            while($rows2 = mysql_fetch_array($query3)):
                $attendanceMark = $rows2['employeeattendance'];
                if($attendanceMark = 1)
                    echo "<td><center><strong>X</strong></center></td>";
                else
                echo "No Record";
            endwhile;


        endwhile;

endwhile;
?>

1 个答案:

答案 0 :(得分:1)

让我们从嵌套循环开始。我将在这里给出一个示例,它将跳过查询以使其更容易理解。

<?php
$nested1 = array(10,11,12,13,14,15,16,17);
$nested2 = array(20,21,22,23,24,25,26,27);
$nested3 = array(30,31,32,33,34,35,36,37);

while ($rows3 = next($nested1)) {
  echo "First Level: $rows3\n";

  while ($rows1 = next($nested2)) {
    echo " -- Second Level: $rows1\n";

    while ($rows2 = next($nested3)) {
      echo " -- -- Third Level: $rows2\n";
    }
  }
}

这样的结果看起来有点像我期待的那样

First Level: 10
 -- Second Level: 20
 -- -- Third Level: 30
 -- -- Third Level: 31
 -- -- Third Level: 32
 -- -- Third Level: 33
 -- -- Third Level: 34
 -- -- Third Level: 35
 -- -- Third Level: 36
 -- -- Third Level: 37
 -- Second Level: 21
 -- Second Level: 22
 -- Second Level: 23
 -- Second Level: 24
 -- Second Level: 25
 -- Second Level: 26
 -- Second Level: 27
First Level: 11
First Level: 12
First Level: 13
First Level: 14
First Level: 15
First Level: 16
First Level: 17

这样做的原因是我们在此示例中使用next,它将内部指针移动到数组中的下一个元素。由于下次我们通过外循环时指针永远不会复位,我们将点击回显“First Level:$ rows \ n”;行,然后是下一个while循环。由于指针仍在数组的末尾,因此next()将失败并继续执行迭代的其余部分。这与最内层循环($ rows2)相同。

我在这个例子中使用next的原因是它的行为就像你的查询中使用的mysql_fetch_array函数一样,它将到达结果集的末尾并且指针永远不会重置回到开头。

所有这些都说,你真的应该考虑在你的查询中使用JOIN。具体来说,请从左侧JOIN开始,类似于以下内容

SELECT * FROM employees as e
LEFT JOIN meetingattendance as m ON e.EmployeeNumber = m.employeeid
WHERE e.position='Supervisor' OR e.position='Area Manager' 
ORDER BY e.area, e.LastName, e.EmployeeNumber

从那里开始,看看是否能让你更接近你想要的东西。