在最后一行仅显示1列

时间:2014-06-26 15:36:32

标签: php mysql sql

我已经多次检查过phpMyAdmin中的查询,并且确定它们绝对是正确的。此外,如果我手动写入循环3次,设置2,3,4而不是递增计数器,它仍然只显示最后一行中的一列。前两行结果准确无误。

foreach($row as $rec)基本上是从另一个打印表头的查询中运行了17次。

$by_type1 = array("First","Second+","Final"); 
$counter = 2; //this counter represents type of interview (2-First, 3-Second+, 4-Final)

foreach ($by_type1 as $type1) 
{    
    $table_row = '<tr><td class="rborder">'.$type1.'</td>';

    foreach ($row as $rec)
    {
        $id=$rec['id'];
        $qry2 = "SELECT 
            CONCAT( r.fname, ' ', r.lname ) AS rname, 
            ch.status_id as Type, 
            count(ch.status_id) as number

            FROM candidateJoborderHistory ch 

            LEFT JOIN candidates_info c ON ch.candidate_id = c.candidate_id 
            LEFT JOIN recruiters r ON c.recruiter_id=r.recruiter_id 
            LEFT JOIN interviewtypes i ON ch.interview_id = i.interview_id 

            WHERE c.recruiter_id = $id AND UNIX_TIMESTAMP(ch.date_interview) BETWEEN 1401667200 AND     
            1402099200 AND ch.status_id = $counter
            group by ch.status_id";

        global $conn;
        $conn->open();
        $stmt2 = $conn->prepare($qry2);
        $stmt2->execute();
        $row2 = $stmt2->fetchAll();

        foreach($row2 as $row_x)
        { 
            $table_row .= '<td>'.$row_x['number'].'</td>';
        }
    }

    $table_row .='</tr>';
    echo $table_row . "\n";
    $counter++;
}

我想要的是:

First   6   6   4   4   11  6   12  3
Second+ 3   1   2   1   3
Final   3   2   1   4   1

但我得到的是:

First   6   6   4   4   11  6   12  3
Second+ 3   1   2   1   3
Final   3

1 个答案:

答案 0 :(得分:1)

我认为这是一种MySQL方法,我对您的代码进行了一些更改并对其进行了解释:

$by_type1 = array(2=>"First", 3=>"Second+", 4=>"Final"); 
$counter = 2; //this counter represents type of interview (2-First, 3-Second+, 4-Final)

// Open connection first.
global $conn;
$conn->open();

// Using key => value array gets code simple
foreach ($by_type1 as $counter=>$type1) 
{    
    $table_row = '<tr><td class="rborder">'.$type1.'</td>';

    foreach ($row as $rec)
    {
        $id=$rec['id'];

        $qry2 = "SELECT 
            CONCAT( r.fname, ' ', r.lname ) AS rname, 
            ch.status_id as Type, 
            count(ch.status_id) as number

            FROM candidateJoborderHistory ch 

            LEFT JOIN candidates_info c ON ch.candidate_id = c.candidate_id 
            LEFT JOIN recruiters r ON c.recruiter_id=r.recruiter_id 
            LEFT JOIN interviewtypes i ON ch.interview_id = i.interview_id 

            WHERE c.recruiter_id = $id 
            AND UNIX_TIMESTAMP(ch.date_interview) 
            BETWEEN 1401667200 AND 1402099200 
            AND ch.status_id = $counter
            GROUP BY ch.status_id"; 

            $res = $conn->prepare($qry2);
            $res->execute();

        // Loop to get data...
         while($row_x = $res->fetch(PDO::FETCH_ASSOC))
        { 
            $table_row .= '<td>'.$row_x['number'].'</td>';
        }

    }

    $table_row .='</tr>';
    echo $table_row . "\n";
}
$conn->close();

代码未经过测试,但如果您有任何疑问,请询问。希望它有所帮助!

参考文献:

Prepared Statements Executing Statements