2016-02-20 134 views
0

我想在PDFPHP/FPDF表打印相同的记录

enter image description here

打印这一点,但这些记录都是一样的,它的奇怪,因为他们不是我的数据库中一样。

问题: 条件,等级和备注下的记录相同,但在我的数据库中,这些记录不一样。

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

$sql_criteria = mysql_query("SELECT DISTINCT criteria, percentage FROM tb_equivalent WHERE instructor_id = '$inst_id' AND description = '$desc' AND subj_code = '$code' AND term = '$term'"); 
    $criteria = array(); 
    while($row = mysql_fetch_assoc($sql_criteria)){ 
     $criteria[] = $row['criteria']; 
     $pdf->SetFont('Arial','',9); 
     $pdf->Cell(35,5,$row['criteria'],1,'','C'); 
     $x = $pdf->GetX(); 
     $y = $pdf->GetY(); 
     $pdf->SetXY($x-35, $y+5); 
     $pdf->Cell(17.5,5,'Ave',1,'','C'); 
     $pdf->Cell(17.5,5,$row['percentage']."%",1,'','C'); 
     $pdf->SetXY($x, $y); 
    } 

    $pdf->Cell(35,10,'Grade',1,0,'C'); 
    $pdf->Cell(35,10,'Remark',1,0,'C'); 
    $pdf->SetFont('Arial','',9); 
    $pdf->Ln(); 
    $sql = mysql_query("SELECT * FROM tb_equivalent INNER JOIN tb_student ON tb_equivalent.stud_id=tb_student.stud_id WHERE tb_equivalent.instructor_id = '$inst_id' AND tb_equivalent.description = '$desc' AND tb_equivalent.subj_code = '$code' GROUP BY tb_equivalent.stud_name ORDER BY stud_lname ASC"); 
    while($row = mysql_fetch_array($sql)){ 
     $name = $row['stud_name']; 
     $course = $row['course_and_year']; 
     $pdf->SetFont('Arial','',9); 
     $pdf->Cell(1); 
     $pdf->Cell(40,4,$name,1); 
     $pdf->Cell(20,4,$course,1,0,'C'); 

     for($x = 0; $x < count($criteria); $x++){ 
      $query_rec = mysql_query("SELECT * FROM tb_equivalent INNER JOIN tb_student ON tb_equivalent.stud_id=tb_student.stud_id WHERE tb_equivalent.instructor_id = '$inst_id' AND tb_equivalent.criteria = '".$criteria[$x]."' AND tb_equivalent.description = '$desc' AND tb_equivalent.subj_code = '$code' AND tb_equivalent.term = '$term' ORDER BY stud_lname ASC"); 
      $record = mysql_fetch_array($query_rec); 
      $pdf->Cell(17.5,4,$record['average'],1,0,'C'); 
      $pdf->Cell(17.5,4,$record['equivalent'],1,0,'C'); 

     } 
     $query_grade = mysql_query("SELECT grade, remark FROM tb_record_grade WHERE instructor_id = '$inst_id' AND description = '$desc' AND subj_code = '$code' AND term = '$term'"); 
     $row_grade = mysql_fetch_array($query_grade); 
     $pdf->Cell(35,4,$row_grade['grade'],1,0,'C'); 
     $pdf->Cell(35,4,$row_grade['remark'],1,0,'C'); 
     $pdf->Ln(); 
    } 
+0

对于它的价值,我建议将查询移到它自己的函数中,关闭连接然后生成pdf。它关系较少的资源和清理代码... –

回答

2

你错过了while循环为其他MySQL取。

$stud_id = $row['stud_id']; 
for($x = 0; $x < count($criteria); $x++){ 
    $query_rec = mysql_query(" 
     SELECT * 
     FROM tb_equivalent 
     INNER JOIN tb_student ON tb_equivalent.stud_id = tb_student.stud_id 
     WHERE tb_equivalent.instructor_id = '$inst_id' AND tb_equivalent.criteria = '".$criteria[$x]."' 
      AND tb_equivalent.description = '$desc' AND tb_equivalent.subj_code = '$code' AND tb_equivalent.term = '$term' 
      AND tb_equivalent.stud_id = '$stud_id' 
    "); 
    while ($record = mysql_fetch_array($query_rec)) { 
     $pdf->Cell(17.5,4,$record['average'],1,0,'C'); 
     $pdf->Cell(17.5,4,$record['equivalent'],1,0,'C'); 
    } 

} 
$query_grade = mysql_query(" 
    SELECT grade, remark 
    FROM tb_record_grade 
    WHERE instructor_id = '$inst_id' AND description = '$desc' AND subj_code = '$code' AND term = '$term' 
"); 
while ($row_grade = mysql_fetch_array($query_grade)) { 
    $pdf->Cell(35,4,$row_grade['grade'],1,0,'C'); 
    $pdf->Cell(35,4,$row_grade['remark'],1,0,'C'); 
} 
$pdf->Ln(); 
+0

嗨,先生为什么这样? - > http://i.imgur.com/lMjGU7W.png – CallMeJeo

+0

已编辑上面的代码以包含学生ID作为查询以获取该数据在学生行的循环中 – awinwood

+0

谢谢先生,感谢您的努力sir:'' ) – CallMeJeo