2012-06-20 25 views
0

我想在PDF文件中顯示結果。現在的問題是PDF文件只能捕獲while循環的第一行。第一行有row[0]['rates'],第二行有row[1]['rates']?所以我只能取一次,顯示row[0]['rates']row[1]['rates'] ...PHP mysql在一個目標中顯示4個結果

創建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'); 

?> 

回答

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 

這樣,它增加了循環到$中間結果比較字符串,而不是每次都覆蓋它。查找字符串連接以獲取更多詳細信息http://php.net/manual/en/language.operators.string.php。道歉,如果這裏有任何次要代碼問題,因爲它是未經測試

0

而不是創建while循環中一個全新的文檔,你可以在錶行循環:

首先設置你的$比較變量(注意[TABLE_ROWS]字符串變量$比較)內:

$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'); 
相關問題