2013-01-04 34 views
1

我只是嘗試使用mPDF來創建數據庫的輸出表pdf,但仍然很難做到這一點。使用mPDF從數據庫調用行

<?php 
$html = ' 

<center><h3>TITLE</h3></center> 
<center> 
<table border="1"> 
<tr> 
    <th>COLUMN 1</th><th>COLUMN 2</th> 
</tr> 
<tr> 
     <!--how to fetch this row from DB? --> 
    <td>.$row[no1].</td><td>.$row[no2].</td> 
</tr> 
</table></center> 

'; 
//============================================================== 
//============================================================== 
//============================================================== 
include("../mpdf.php"); 
    include "conn.php"; 

    $res = mysql_query("select * from list"); 
    if (!$res) 
     die("query error : ".mysql_error()); 
$mpdf=new mPDF('c','A4','','',32,25,27,25,16,13); 
$mpdf->SetDisplayMode('fullpage'); 
$mpdf->list_indent_first_level = 0; // 1 or 0 - whether to indent the first level of a list 
// LOAD a stylesheet 
$stylesheet = file_get_contents('mpdfstyletables.css'); 
$mpdf->WriteHTML($stylesheet,1); // The parameter 1 tells that this is css/style only and no body/html/text 
while($row = mysql_fetch_array($res)) 
    if (!$res) 
     die("error fetch array : ".mysql_error()); 
$mpdf->WriteHTML($html,2); 
$mpdf->Output('mpdf.pdf','I'); 
exit; 
//============================================================== 
//============================================================== 
//============================================================== 
?> 

當我運行這個腳本,它可以生成PDF但行不匹配像數據庫?

+0

您從數據庫到$行獲取,但之後再也沒有在任何地方使用$行,所以你檢索的數據基本上是正在垃圾扔掉。你用$ html做什麼將**不**工作。變量不會神奇地以這種方式工作。 –

+0

任何其他的詭計?我把$ row放到$ html內容中,但我不知道如何以這種方式獲取$ row [no1]。 – andrian

+0

你使用了''''引用的字符串,它不插入變量。 PHP也不會「及時回到」來更改您插入到字符串中的變量。你必須把這塊html放到你的while()循環中。 –

回答

3

是這樣的:

<?php 

//============================================================== 
//============================================================== 
//============================================================== 
include("../mpdf.php"); 
include "conn.php"; 

$res = mysql_query("select * from list"); 
if (!$res) 
    die("query error : ".mysql_error()); 
$mpdf=new mPDF('c','A4','','',32,25,27,25,16,13); 
$mpdf->SetDisplayMode('fullpage'); 
$mpdf->list_indent_first_level = 0; // 1 or 0 - whether to indent the first level of a list 
// LOAD a stylesheet 
$stylesheet = file_get_contents('mpdfstyletables.css'); 
$mpdf->WriteHTML($stylesheet,1); // The parameter 1 tells that this is css/style only and no body/html/text 
$html = ' 

<center><h3>TITLE</h3></center> 
<center> 
<table border="1"> 
<tr> 
<th>COLUMN 1</th><th>COLUMN 2</th> 
</tr> 
<tr>'; 

while($row = mysql_fetch_array($res)){ 
$html .= '<td>'.$row['no1'].'</td><td>' . $row['no2']. '</td>'; 
} 
$html .= '</tr> 
</table></center> 
'; 
$mpdf->WriteHTML($html,2); 
$mpdf->Output('mpdf.pdf','I'); 
exit; 
//============================================================== 
//============================================================== 
//============================================================== 
?>