2015-04-27 131 views
-1

我正在使用php & mysql。使用php創建PDF文件?

我想檢索數據庫中的表中的所有記錄,並將它們打印到pdf文件中。

我寫了一些代碼,但它只檢索表中的第一條記錄,而沒有檢索到所有其他記錄。

這裏是我的代碼:

<?php 
$con = mysql_connect("localhost","root","") 
    or die("Failed to connect to the server: " . mysql_error()); 
mysql_select_db("Users") 
    or die("Failed to connect to the database: " . mysql_error()); 

$query = mysql_query("SELECT * FROM reservations"); 
$numrows = mysql_num_rows($query); 
if($numrows > 0) 
{ 
    while($result = mysql_fetch_assoc($query)) 
    { 
     $db_id  = $result['id']; 
     $db_fname = $result['fname']; 
     $db_lname = $result['lname']; 
     $db_email = $result['email']; 
     $db_phone = $result['phone']; 
     $db_day  = $result['day']; 
     $db_time  = $result['time']; 
     $db_cartype = $result['car_type']; 
     $db_carcolor = $result['car_color']; 
     $db_carmodel = $result['car_year']; 
     $db_carno = $result['car_no']; 
     $db_problem = $result['problem']; 
     $db_status = $result['status']; 
     $db_remtime = $result['remaining_time']; 
     $db_cost  = $result['cost']; 
     $db_payment = $result['payment']; 
     $db_date  = $result['date']; 

     require("FPDF/fpdf.php"); 
     $pdf = new FPDF(); 
     $pdf->AddPage(); 
     $pdf->SetFont("Arial","B","15"); 
     $pdf->Cell(0,10,"Freedom Car: Total Reservations",1,1,"C"); 
     $pdf->Image("porchue.jpg"); 
     $pdf->SetFont("Arial","","10"); 
     $pdf->Write(5,"ID: $db_id\n"); 
     $pdf->Write(5,"First Name: $db_fname\n"); 
     $pdf->Write(5,"Last Name: $db_lname\n"); 
     $pdf->Write(5,"Email Address: $db_email\n"); 
     $pdf->Write(5,"Phone Number: $db_phone\n"); 
     $pdf->Write(5,"Reserved Day: $db_day\n"); 
     $pdf->Write(5,"Reserved Time: $db_time\n"); 
     $pdf->Write(5,"Car Type: $db_cartype\n"); 
     $pdf->Write(5,"Car Color: $db_carcolor\n"); 
     $pdf->Write(5,"Car Model: $db_carmodel\n"); 
     $pdf->Write(5,"Car Number: $db_carno\n"); 
     $pdf->Write(5,"Car Problem: $db_problem\n"); 
     $pdf->Write(5,"Car Status: $db_status\n"); 
     $pdf->Write(5,"Remaining Time: $db_remtime\n"); 
     $pdf->Write(5,"Total Cost: $db_cost\n"); 
     $pdf->Write(5,"Payment Method: $db_payment\n"); 
     $pdf->Write(5,"Reservation Date: $db_date\n\n"); 
     $pdf->Output(); 
    } 
} 
else 
    echo "<h4>Sorry, No reservations at this time.</h4>"; 
?> 
+2

看看什麼應該和不應該在那裏while循環 –

+0

我不明白你,請解釋你的想法在代碼中不只是在一些文本。 @Dagon –

回答

0

首先你需要創建一個數組,並且 畢竟數據設置爲陣列使您$pdf->Write();評論工作。 你不能這樣寫,因爲所有數據都在。

while($result = mysql_fetch_assoc($query)) 
    { 
// create an array or call all data here ! 
} 
// use forachone array i main kame loop and start your code. 

它只檢索第一條記錄,因爲您需要獲取所有數據一次! 但是您在while循環中寫入pdfCommand,因此它僅檢索第一個數據並且無法傳遞給其他數據。

1
<?php 
$con = mysql_connect("localhost","root","") 
    or die("Failed to connect to the server: " . mysql_error()); 
mysql_select_db("Users") 
    or die("Failed to connect to the database: " . mysql_error()); 

$query = mysql_query("SELECT * FROM reservations"); 
$numrows = mysql_num_rows($query); 
if($numrows > 0) 
{ 
    require("FPDF/fpdf.php"); 
     $pdf = new FPDF(); 
     $pdf->AddPage(); 
     $pdf->SetFont("Arial","B","15"); 
     $pdf->Cell(0,10,"Freedom Car: Total Reservations",1,1,"C"); 
     $pdf->Image("porchue.jpg"); 
     $pdf->SetFont("Arial","","10"); 
    while($result = mysql_fetch_assoc($query)) 
    { 
     $db_id  = $result['id']; 
     $db_fname = $result['fname']; 
     $db_lname = $result['lname']; 
     $db_email = $result['email']; 
     $db_phone = $result['phone']; 
     $db_day  = $result['day']; 
     $db_time  = $result['time']; 
     $db_cartype = $result['car_type']; 
     $db_carcolor = $result['car_color']; 
     $db_carmodel = $result['car_year']; 
     $db_carno = $result['car_no']; 
     $db_problem = $result['problem']; 
     $db_status = $result['status']; 
     $db_remtime = $result['remaining_time']; 
     $db_cost  = $result['cost']; 
     $db_payment = $result['payment']; 
     $db_date  = $result['date']; 


     $pdf->Write(5,"ID: $db_id\n"); 
     $pdf->Write(5,"First Name: $db_fname\n"); 
     $pdf->Write(5,"Last Name: $db_lname\n"); 
     $pdf->Write(5,"Email Address: $db_email\n"); 
     $pdf->Write(5,"Phone Number: $db_phone\n"); 
     $pdf->Write(5,"Reserved Day: $db_day\n"); 
     $pdf->Write(5,"Reserved Time: $db_time\n"); 
     $pdf->Write(5,"Car Type: $db_cartype\n"); 
     $pdf->Write(5,"Car Color: $db_carcolor\n"); 
     $pdf->Write(5,"Car Model: $db_carmodel\n"); 
     $pdf->Write(5,"Car Number: $db_carno\n"); 
     $pdf->Write(5,"Car Problem: $db_problem\n"); 
     $pdf->Write(5,"Car Status: $db_status\n"); 
     $pdf->Write(5,"Remaining Time: $db_remtime\n"); 
     $pdf->Write(5,"Total Cost: $db_cost\n"); 
     $pdf->Write(5,"Payment Method: $db_payment\n"); 
     $pdf->Write(5,"Reservation Date: $db_date\n\n"); 
    } 
     $pdf->Output(); 
} 
else 
    echo "<h4>Sorry, No reservations at this time.</h4>"; 
?> 
+1

我查看我的代碼並發現問題。謝謝閣下的幫助。檢查while循環是個問題。這就是答案。 @Dagon我從埃及歡迎你。 –