0

我正在为一个办公室做一个考勤管理系统项目,其中还包括休假管理系统。有一个报告显示页面,其中显示了员工的缺勤/当前报告。现在,如果特定员工在休假日缺席,我必须显示in leave报告而不是absent报告,这些休假天数是根据他的休假申请确定的,并与申请的休假日期一起存储在数据库中。我创建了一个leave表,其中存储了所有相关信息leave。这是我的leave桌子..

lid     empname     username    nod       sdate        edate       reason  action  empID 
===     =======     ========    ===     ==========  ===========    ======  ====== =======
 1      Maxwell       max        1      2013/04/09  2013/04/09      xam   Denied    3
 2      Sukant        suk        1      2013/03/11  2013/03/11      xam   Denied    4
 3      Sukant        suk        1      2013/02/20  2013/02/20      xam   Accepted  4
 4      Taylor        tay        1      2013/05/23  2013/05/23      xam   Accepted  1
 5      Sukant        suk        1      2013/05/23  2013/05/23      xam   Accepted  4

问题是我无法一次获取leave表的所有行。但是我必须获取leave表的所有行来检查所有sdateedateaction显示报告in leaveabsent在我的报告页面中。我编写了以下代码,但它只获取leave表的第一行或单行。它将in leave在报告中显示(如果接受)报告,仅显示它正在获取的行的页面。

 <?php
            $leave = mysql_query("SELECT `action` FROM  `leave` 
                            where uname =('SELECT `username` FROM `users` 
                            where `empID` =$user')", $dbCon) 
                            or die(mysql_error());
            $sql = "SELECT * FROM `office_date` 
            WHERE year='$year' 
            AND month = '$month' ORDER BY odate DESC ";
            $query = mysql_query($sql, $dbCon);

            $leave_row = mysql_fetch_array($leave);

            $i = 1;
            while ($row2 = mysql_fetch_object($query)){ 

               if(....)
               {

                   //some code here

                } 
                 else if ($leave_row[7] == "accepted" && $leave_row[4]  == $row2->odate)
                 {

                    echo '<tr bgcolor="' . $bgcolor . '">
                    <td>&nbsp;' . $i . '</td>
                    <td>&nbsp;' . $row2->odate . '</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td><font color="red">In leave</font></td>
                </tr>';

                 }
                else {
                    echo '<tr bgcolor="' . $bgcolor . '">
                    <td>&nbsp;' . $i . '</td>
                    <td>&nbsp;' . $row2->odate . '</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td><font color="red">Absent</font></td>
                </tr>';
                }

                $i++;

            }

            ?> 

sql查询是否SELECT * FROM应该只获取我表where empID = $user的一行?leave

我的 if 条件只检查一行的信息。我知道为什么:查询 $leave = mysql_query("SELECT * FROM leavewhere empID = $user "); 只获取一行。如何获取所有行以及应该用什么 php 代码代替else if我的代码语句来一次获取或检查所有行?

4

2 回答 2

0

这是因为您只从第一个查询中获取一行,您需要循环检索您需要的所有行

$leave_row = mysql_fetch_array($leave);

应该

while($leave_row = mysql_fetch_array($leave)) { .... }
于 2013-04-27T06:59:24.603 回答
0

您的原始代码有

       $leave = mysql_query("SELECT `action` FROM  `leave` 
                        where uname =('SELECT `username` FROM `users` 
                        where `empID` =$user')", $dbCon) 
                        or die(mysql_error());

一个问题是您需要'从子查询中删除标记,如下所示:

       $leave = mysql_query("SELECT `action` FROM  `leave` 
                        where uname =(SELECT `username` FROM `users` 
                        where `empID` =$user)", $dbCon) 
                        or die(mysql_error());

假设这有效,你能试试这个看看会发生什么吗?

<?php
        $leave_query = mysql_query("SELECT `action` FROM  `leave` 
                        where uname =(SELECT `username` FROM `users` 
                        where `empID` =$user)", $dbCon) 
                        or die(mysql_error());
        $office_date_query = mysql_query("SELECT * FROM `office_date` 
        WHERE year='$year' 
        AND month = '$month' ORDER BY odate DESC", $dbCon)
                        or die(mysql_error());

        // Not sure if we want this to be a 1 or a 0...
        $i = 1;

        while ($office_date_query_results = mysql_fetch_object($office_date_query)){ 
          $office_date_results[] = $office_date_query_results;
        }

        while ($leave_query_results = mysql_fetch_array($leave_query)){ 
          $leave_row[] = $leave_query_results;
        }

        for ($x = 0; $x < count($leave_row); $x++) {

          foreach ($office_date_results as $row2) {

            if (....) {
               //some code here
            } 

            else if ($leave_row[$x][7] == "accepted" && $leave_row[$x][4]  == $row2->odate) {
              echo '<tr bgcolor="' . $bgcolor . '">
              <td>&nbsp;' . $i . '</td>
              <td>&nbsp;' . $row2->odate . '</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td><font color="red">In leave</font></td>
              </tr>';
            }

            else {
              echo '<tr bgcolor="' . $bgcolor . '">
               <td>&nbsp;' . $i . '</td>
               <td>&nbsp;' . $row2->odate . '</td>
               <td>&nbsp;</td>
               <td>&nbsp;</td>
               <td>&nbsp;</td>
               <td>&nbsp;</td>
               <td><font color="red">Absent</font></td>
             </tr>';
            }

            $i++;
          }
        }

        ?> 
于 2013-04-27T07:39:33.160 回答