php while循环没有正常执行

时间:2013-05-02 19:05:19

标签: php html

我的数据库中有三个病房 A,B,C 4 员工(病房A上3个,B上4个,C上1个)以及下面的脚本正确地打印出所有病房并打印出在病房A上工作的3名员工,但仅此而已(I:E仅与病房A进行比较而不是其他2名)See Image

你能看到php正在执行的顺序中有任何明显的错误吗?我是PHP的新手,所以这就是为什么我不太了解语法=)

while ( $row = mysql_fetch_array($wardNames) ) {

        echo("<tr><td><a href=\"javascript:displayWard('" . $row['Name'] ."')\">
        <div class='wardHeader'><div class='plus'></div><div class='wardName'><b>" . $row['Name'] . " </b></div>
        </div>
        </a></td></tr>");

        while ( $employeerow = mysql_fetch_array($Employees)) {//Only prints out employees on ward A and not B or C. Why?
            if($employeerow['Ward']==$row['Name']){
                echo("<tr><td>" . $employeerow['Name'] . "</td></tr>"); 
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

迭代结果集后,它已经耗尽,因此不再可迭代。在外循环之前将它存储在数组中:

$all_employees = array();
while($row = mysql_fetch_array($Employees)) {
    $all_employees[] = $row;
}

while ( $row = mysql_fetch_array($wardNames) ) {

    echo("<tr><td><a href=\"javascript:displayWard('" . $row['Name'] ."')\">
          <div class='wardHeader'><div class='plus'></div><div class='wardName'><b>" . $row['Name'] . " </b></div>
          </div>
          </a></td></tr>");

    foreach($all_employees as $employeerow)
        if($employeerow['Ward']==$row['Name']){
            echo("<tr><td>" . $employeerow['Name'] . "</td></tr>"); 
        }
    }
}