0

我想在 PDF 文件中显示结果。现在的问题是 PDF 文件只能捕获 while 循环的第一行。row[0]['rates']第一行和row[1]['rates']第二行有类似的东西吗?所以我可以只取一次并row[0]['rates']显示row[1]['rates']...

创建 PDF:

<?php
session_start();

if( empty( $_POST['compare'] ) ) 
include"connect_to_mysql.php";
$packages = mysql_query("SELECT*FROM package_creation WHERE id IN (". implode(',', $_POST['compare']) ." ) LIMIT 4"); 

while($row=mysql_fetch_array($packages)){
 $compare ='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Compare Packages</title>
<style type="text/css">
.print_title{
    font-family:Arial, Helvetica, sans-serif;
    font-size:20px;
    font-weight:bold;
    color:#000; 
}
.print_text{
    font-family:Arial, Helvetica, sans-serif;
    font-size:10px;
    color:#000000;
}
.print_bold{
    font-family:Arial, Helvetica, sans-serif;
    font-size:10px;
    color:#000000;
    font-weight:bold;
}
.print_footer{
    font-family:Arial, Helvetica, sans-serif;
    font-size:9px;
    color:#000000;
}
.print_big{
    font-family:Arial, Helvetica, sans-serif;
    font-size:14px;
    color:#000000;
    font-weight:bold;
}
body{
    margin:0px;
}
table td{
    height:1px;
}
</style>
</head>

<body>
<table class="print_text" align="left">
<tr>
<td width="200px" class="print_bold" align="right">Package</td><td width="100px" class="print_bold" align="center">Package 1</td><td width="100px" class="print_bold" align="center">Package 2</td><td width="100px" class="print_bold" align="center">Package 3</td><td width="100px" class="print_bold" align="center">Package 4</td>
</tr>
<tr>
<td class="print_bold" align="right">Bank Code:</td><td width="100px" align="center">'.$row['bank_name'].'</td><td width="100px" align="center">display 2nd bank</td><td width="100px" align="center">display 3rd bank</td><td width="100px" align="center">display 4th bank</td>
</tr>
</table>
</body>
</html>';

}
include_once('dompdf/dompdf_config.inc.php');
$dompdf = new DOMPDF();
$dompdf->load_html($compare);
$dompdf->render();
$dompdf->stream('Compare_Packages.pdf');

?>
4

2 回答 2

1

问题是你的while循环。每次循环时,您都会覆盖 $compare 的值。你想要做的是像

 $compare = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Compare Packages</title>
<style type="text/css">
....//you styles here
</style>
</head>

<body>
<table class="print_text" align="left">
<tr>
<td width="200px" class="print_bold" align="right">Package</td><td width="100px"  class="print_bold" align="center">Package 1</td><td width="100px" class="print_bold" align="center">Package 2</td><td width="100px" class="print_bold" align="center">Package 3</td><td width="100px" class="print_bold" align="center">Package 4</td>
</tr>';

while($row=mysql_fetch_array($packages)){

    $compare .= '<tr>
 <td class="print_bold" align="right">Bank Code:</td><td width="100px" align="center">'.$row['bank_name'].'</td><td width="100px" align="center">display 2nd bank</td><td width="100px" align="center">display 3rd bank</td><td width="100px" align="center">display 4th bank</td>
</tr>';
} //end while

$compare .= '</table>
 </body>
  </html>';


//rest of your code

这样,它将循环的结果添加到 $compare 字符串的中间,而不是每次都覆盖它。查找字符串连接以获取更多详细信息http://php.net/manual/en/language.operators.string.php。抱歉,如果这里有任何小代码问题,因为它未经测试

于 2012-06-20T07:17:16.537 回答
0

您可以在表格行上循环,而不是在 while 循环中创建一个全新的文档:

首先设置您的 $compare 变量(注意[TABLE_ROWS]在字符串变量 $compare 中):

$compare = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Compare Packages</title>
<style type="text/css">
.print_title{
    font-family:Arial, Helvetica, sans-serif;
    font-size:20px;
    font-weight:bold;
    color:#000; 
}
.print_text{
    font-family:Arial, Helvetica, sans-serif;
    font-size:10px;
    color:#000000;
}
.print_bold{
    font-family:Arial, Helvetica, sans-serif;
    font-size:10px;
    color:#000000;
    font-weight:bold;
}
.print_footer{
    font-family:Arial, Helvetica, sans-serif;
    font-size:9px;
    color:#000000;
}
.print_big{
    font-family:Arial, Helvetica, sans-serif;
    font-size:14px;
    color:#000000;
    font-weight:bold;
}
body{
    margin:0px;
}
table td{
    height:1px;
}
</style>
</head>

<body>
<table class="print_text" align="left">
<tr>
<td width="200px" class="print_bold" align="right">Package</td><td width="100px" class="print_bold" align="center">Package 1</td><td width="100px" class="print_bold" align="center">Package 2</td><td width="100px" class="print_bold" align="center">Package 3</td><td width="100px" class="print_bold" align="center">Package 4</td>
</tr>
[TABLE_ROWS]
</table>
</body>
</html>';

然后获取所有要显示的行:

while($row=mysql_fetch_array($packages)){
$table_rows .= '<tr>
<td class="print_bold" align="right">Bank Code:</td><td width="100px" align="center">'.$row['bank_name'].'</td><td width="100px" align="center">display 2nd bank</td><td width="100px" align="center">display 3rd bank</td><td width="100px" align="center">display 4th bank</td>
</tr>';
}

$compare = str_replace('[TABLE_ROWS]', $table_rows, $compare);

然后调用其余代码:

include_once('dompdf/dompdf_config.inc.php');
$dompdf = new DOMPDF();
$dompdf->load_html($compare);
$dompdf->render();
$dompdf->stream('Compare_Packages.pdf');
于 2012-06-20T07:27:43.567 回答