2010-06-06 68 views
0

使用FPDF插件,我使用PHP/MySQL打印標籤(http://www.fpdf.de/downloads/addons/29/)。我希望能夠讓用戶選擇打印多少個標籤。例如,如果查詢輸出10條記錄,並且用戶想要爲每條記錄打印3個標籤,則將它們全部打印在一組中。 1,1,1,2,2,2,3,3,3 ...等。有任何想法嗎?PHP/MySQL打印重複標籤

 <?php 
      require_once('auth.php'); 
      require_once('../config.php'); 
      require_once('../connect.php');  

      require('pdf/PDF_Label.php'); 

      $sql="SELECT $tbl_members.lastname, $tbl_members.firstname, 
    $tbl_members.username, $tbl_items.username, $tbl_items.itemname 
FROM $tbl_members, $tbl_items 
WHERE $tbl_members.username = $tbl_items.username"; 
      $result=mysql_query($sql); 

      if(mysql_num_rows($result) == 0){ 
      echo "Your search criteria does not return any results, please try again."; 
      exit(); 
      } 

      $pdf = new PDF_Label("5160"); 

      $pdf->AddPage(); 

      // Print labels 
      while($rows=mysql_fetch_array($result)){ 
       $name = $rows['lastname'].', '.$rows['firstname'; 
       $item= $rows['itemname']; 

       $text = sprintf(" * %s *\n %s\n", $name, $item); 
       $pdf->Add_Label($text); 
      } 

      $pdf->Output('labels.pdf', 'D'); 
      ?> 

回答

1

假設像$copies一個變量是他們想要多少份製成一個整數,我會做以下修改:

// Print labels 
while($row = mysql_fetch_array($result)){ 
    // Run Once for Each Result 
    $name = $row['lastname'].', '.$row['firstname']; 
    $item = $row['itemname']; 
    $text = sprintf(" * %s *\n %s\n", $name, $item); 
    if(isset($copies)) { 
     // The Copies Variable exists 
     for($i=0 ; $i<$copies ; $i++) { 
      // Run X times - Once for each Copy 
      $pdf->Add_Label($text); 
     } 
    } else { 
     // The Copies Variable does not exist - Assume 1 Copy 
     $pdf->Add_Label($text); 
    } 
} 

這應該提供所需的功能。

+0

完美!謝謝! – Michael 2010-06-06 12:40:12