从MySQL生成表到PDF(使用PHP)

时间:2016-11-25 08:45:55

标签: php mysql pdf fpdf mpdf

我试图将表格(已过滤)从MySQL导出为pdf。

这里的主要问题是它只打印列表中的最后一个结果,而不是打印所有结果。

我尝试过其他代码,但我仍然得到相同的结果。我不知道我的代码有什么问题:

<?php
include("configsample.php");
?>
<?php

$string=$_GET['string'];
$course=$_GET['course'];
$category=$_GET['category'];
$from=$_GET['from'];
$to=$_GET['to'];

if ($_REQUEST["string"]<>'') {
$search_string = " AND restu_title LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%'";
}
if ($_REQUEST["course"]<>'') {
$search_course = " AND restu_course='".mysql_real_escape_string($_REQUEST["course"])."'";
}
if ($_REQUEST["category"]<>'') {
$search_category = " AND category='".mysql_real_escape_string($_REQUEST["category"])."'";
}


if ($_REQUEST["from"]<>'' and $_REQUEST["to"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE restu_year BETWEEN '".mysql_real_escape_string($_REQUEST["from"])."' AND '".mysql_real_escape_string($_REQUEST["to"])."'".$search_string.$search_course;
} else if ($_REQUEST["from"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE restu_year = '".mysql_real_escape_string($_REQUEST["from"])."'".$search_string.$search_course;
} else if ($_REQUEST["to"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE restu_year = '".mysql_real_escape_string($_REQUEST["to"])."'".$search_string.$search_course;
} else {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE restu_id>0".$search_string.$search_course.$search_category;
}
$html='<table>
<tr style="color:#010101; font:bold 13px Times New Roman; height:40px;">
<th>Research Title</th>
<th>Year</th>
<th>Proponent(s)</th>
<th>Adviser</th>
<th>Research Panel</th>
</tr>';
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
if (mysql_num_rows($sql_result)>0) {
while ($row = mysql_fetch_assoc($sql_result)){
$html2='<tr>
<td style="padding:10px;">'.$row['restu_title'].'</td>
<td style="padding:10px;">'.$row['restu_year'].'</td>
<td style="padding:10px;">'.$row['restu_by'].'</td>
<td style="padding:10px;">'.$row['restu_ad'].'</td>
<td style="padding:10px;">'.$row['restu_panel'].'</td>
</tr>
</table>';
}
}

include("mpdf60/mpdf.php");
$mpdf=new mPDF('c','Letter','','',20,20,18,16,9,9,'L');
$mpdf->SetDisplayMode('fullpage');
$mpdf->AddPage('L');
$mpdf->WriteHTML($html);
$mpdf->WriteHTML($html2);
$mpdf->Output();
exit;
?>

2 个答案:

答案 0 :(得分:2)

您需要concatenate文本字符串,以避免$html2被下一个值覆盖。

每次循环运行时,$html2都会被新值覆盖。在您的情况下,您需要在等号前添加一个点:

while ($row = mysql_fetch_assoc($sql_result)){
    $html .='<tr>
            <td style="padding:10px;">'.$row['restu_title'].'</td>
            <td style="padding:10px;">'.$row['restu_year'].'</td>
            <td style="padding:10px;">'.$row['restu_by'].'</td>
            <td style="padding:10px;">'.$row['restu_ad'].'</td>
            <td style="padding:10px;">'.$row['restu_panel'].'</td>
    </tr>';
}

小心正确放置</table>。如果您的SQL请求没有结果,则表格将不会关闭,导致HTML无效。

你需要这样做:

$html='<table>
    <tr style="color:#010101; font:bold 13px Times New Roman; height:40px;">
    <th>Research Title</th>
    <th>Year</th>
    <th>Proponent(s)</th>
    <th>Adviser</th>
    <th>Research Panel</th>
    </tr>';
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
if (mysql_num_rows($sql_result)>0) {
    while ($row = mysql_fetch_assoc($sql_result)){
        $html .='<tr>
            <td style="padding:10px;">'.$row['restu_title'].'</td>
            <td style="padding:10px;">'.$row['restu_year'].'</td>
            <td style="padding:10px;">'.$row['restu_by'].'</td>
            <td style="padding:10px;">'.$row['restu_ad'].'</td>
            <td style="padding:10px;">'.$row['restu_panel'].'</td>
            </tr>';
    }
}
$html .= '</table>';

答案 1 :(得分:0)

追加$ html2。您在循环中分配值,因此它只显示最后一条记录。

像这样使用..

 $html2=$html2.'<tr>
    <td style="padding:10px;">'.$row['restu_title'].'</td>
    <td style="padding:10px;">'.$row['restu_year'].'</td>
    <td style="padding:10px;">'.$row['restu_by'].'</td>
    <td style="padding:10px;">'.$row['restu_ad'].'</td>
    <td style="padding:10px;">'.$row['restu_panel'].'</td>
    </tr>';