使用多个while循环只获得1条记录

时间:2014-04-25 15:08:51

标签: php

我刚开始学习php,所以请耐心等待......

我正在尝试从主管名单中获取员工名单。 我只是得到了最后一位主管..

我猜这是因为我把代码放在了错误的地方..

这是我到目前为止的代码..

//Get List of Supervisors
$result_supervisors = mysql_query("SELECT emp_no as semp_no, full_name as sfull_name,employee_email as s_email FROM employees where supervisor = '1' ORDER BY emp_no");
// Initializes Supervisor Array 
$supervisors = array();
while($row = mysql_fetch_array($result_supervisors, MYSQL_ASSOC))
{
 $supervisor = $row['semp_no'];
 $supervisor_name = $row['sfull_name'];
 $supervisor_email = $row['s_email'];
//add row to supervisors array
    $supervisors[] = array($supervisor, $supervisor_name, $supervisor_email);
}
//Get employee for the supervisor
$empquery = "SELECT emp_no, full_name, review_date,employee_email as e_email FROM employees where review_date < curdate() and employment_status !='2' and emp_supervisor = ". $supervisor ;
$empresult = mysql_query($empquery); 
while($emprow = mysql_fetch_array($empresult)) 
{
if ($emprow['cnt'] == '0')
{
    // no records for supervisor
}
else
{
    $emp_no =   $emprow['emp_no'];
    $full_name =    $emprow['full_name'];
    $e_email =  $emprow['e_email'];
    $review_date =  $emprow['review_date'];

//add row to employees array
    $employees[] = array($supervisor, $supervisor_name, $supervisor_email,$emp_no,$full_name,$e_email,$review_date);    
}
}
print_r($employees);

我正在尝试将结果放入html表格并通过电子邮件发送给主管以获得最终结果。 但是,获得一个以上的主管是我最大的问题..

有没有一种简单的方法可以将数据放入html表格中......万一有人知道为什么只有一位主管在做...

任何可以帮助新手的帮助?

KD

2 个答案:

答案 0 :(得分:0)

更改您的第一个查询:

$result_supervisors = mysql_query("SELECT emp_no as semp_no, full_name as sfull_name,employee_email as s_email FROM employees ORDER BY emp_no");

答案 1 :(得分:0)

试试这段代码:

//Get List of Supervisors
$result_supervisors = mysql_query("SELECT emp_no as semp_no, full_name as sfull_name,employee_email as s_email FROM employees where supervisor = '1' ORDER BY emp_no");
// Initializes Supervisor Array 
$supervisors = array();
$employees = array();
while($row = mysql_fetch_array($result_supervisors, MYSQL_ASSOC))
{
    $supervisor = $row['semp_no'];
    $supervisor_name = $row['sfull_name'];
    $supervisor_email = $row['s_email'];
    //add row to supervisors array
    $supervisors[] = array($supervisor, $supervisor_name, $supervisor_email);

    //Get employee for the supervisor
    $empquery = "SELECT emp_no, full_name, review_date,employee_email as e_email FROM employees where review_date < curdate() and employment_status !='2' and emp_supervisor = ". $supervisor ;
    $empresult = mysql_query($empquery); 
    while($emprow = mysql_fetch_array($empresult)) 
    {
        if ($emprow['cnt'] == '0')
        {
            // no records for supervisor
        }
        else
        {
            $emp_no =   $emprow['emp_no'];
            $full_name =    $emprow['full_name'];
            $e_email =  $emprow['e_email'];
            $review_date =  $emprow['review_date'];

        //add row to employees array
            $employees[] = array($supervisor, $supervisor_name, $supervisor_email,$emp_no,$full_name,$e_email,$review_date);    
        }
    }
}
print_r($employees);