0

我有一个将数据库中的数据写入 ms 文档的代码:它工作得很好,但它重复了这样的表格标题: 订阅者姓名 订阅者帐号 S 卡号 解码器号 联系人编号 安装日期 激活日期 计时器 0 。 . 订户姓名 订户帐号 S-卡号 解码器号 联系电话 安装日期 激活日期 Musajhujasasa 903343434342 2013 .. 订户姓名 订户帐户号 S-卡号 解码器编号 联系电话 安装日期 激活日期

但我希望它像这样显示 Subscriber's Name Subscriber's Account No. S-Card No. Decoder No. Contact No Date Installed Activation Date Timer 676737363743 Vc 67676 Dc456 07855625426 2013 。2013 穆萨 7878787878 Vc3454。Dc56 089898892 2013 2013 Musajhujasasa 87u8u78u8u Vc4565 Dc786 089887387 2013 2013 恐惧 S77878787879 Vc 45454 Dc5653 078563536 2013 2013

   <?php
   #this connects to my database
   include("db.inc.php"); 
   ?>
       <?php
     $query='SELECT  * 
    from clientinfo';
    $result = mysql_query($query,$con) or die (mysql_error($con));
        $fp = fopen("report.doc", 'w+'); 

    while($record = mysql_fetch_array($result)){
    $firstname = $record['firstname'];
        $refno = $record['refno'];
        $smartcard = $record['smartcard'];
        $decoderno = $record['decoderno'];
        $cell = $record['cell'];
        $date= $record['date'];
     #create word document starts here
    $str ="<table border=\"1\"><tr><td><b>Subscriber's Name</b></td><td>  <b>     Subscriber's      Account No.</b></td> <td><b>S-Card No.</b></td><td><b>Decoder     No.</b>    </td>
    <td><b>Contact No</b></td><td><b>Date Installed</b></td><td><b>Activation Date</b></td>
    </tr>
     </tr><tr><td>$firstname</td><td >$refno</td><td >$smartcard</td><td>$decoderno</td>
    <td>$cell</td><td >$date</td><td >..</td>
    </tr></table>";
       fwrite($fp, $str);  
     }
       fclose($fp);
    ?>
4

4 回答 4

0

它重复是因为标题在 while 循环内。

  <?php
   #this connects to my database
   include("db.inc.php"); 
   ?>
       <?php
     $query='SELECT  * 
    from clientinfo';
    $result = mysql_query($query,$con) or die (mysql_error($con));
        $fp = fopen("report.doc", 'w+'); 

$str ="<table border=\"1\"><tr><td><b>Subscriber's Name</b></td><td>  <b>     Subscriber's      Account No.</b></td> <td><b>S-Card No.</b></td><td><b>Decoder No.</b>    </td>";

while($record = mysql_fetch_array($result)){
    $firstname = $record['firstname'];
        $refno = $record['refno'];
        $smartcard = $record['smartcard'];
        $decoderno = $record['decoderno'];
        $cell = $record['cell'];
        $date= $record['date'];
     #create word document starts here
   $str.="
    <td><b>Contact No</b></td><td><b>Date Installed</b></td><td><b>Activation Date</b></td>
    </tr>
     </tr><tr><td>$firstname</td><td >$refno</td><td >$smartcard</td><td>$decoderno</td>
    <td>$cell</td><td >$date</td><td >..</td>
    </tr></table>";


fwrite($fp, $str);  
     }
       fclose($fp);
    ?>
于 2013-01-17T06:51:15.820 回答
0

你在循环中犯的错误,

     $str = "<table border=\"1\"><tr><td><b>Subscriber's Name</b></td><td>  <b>     Subscriber's      Account No.</b></td> <td><b>S-Card No.</b></td><td><b>Decoder     No.</b>    </td>
    <td><b>Contact No</b></td><td><b>Date Installed</b></td><td><b>Activation Date</b></td>
    </tr>
     </tr>";
   fwrite($fp, $str);  
    while($record = mysql_fetch_array($result)){
    $firstname = $record['firstname'];
        $refno = $record['refno'];
        $smartcard = $record['smartcard'];
        $decoderno = $record['decoderno'];
        $cell = $record['cell'];
        $date= $record['date'];
     #create word document starts here
    $str ="<tr><td>$firstname</td><td >$refno</td><td >$smartcard</td><td>$decoderno</td>
    <td>$cell</td><td >$date</td><td >..</td>
    </tr>";
    fwrite($fp, $str);  
     }
      $str = "</table>";
     fwrite($fp, $str);
     fclose($fp); 
于 2013-01-17T07:02:57.560 回答
0
     <?php
   #this connects to my database
   include("db.inc.php"); 
   ?>
       <?php
     $query='SELECT  * 
    from clientinfo';
    $result = mysql_query($query,$con) or die (mysql_error($con));
        $fp = fopen("report.doc", 'w+'); 
     if(mysql_num_rows($result))
     {    

    $str ="<table border=\"1\"><tr><td><b>Subscriber's Name</b></td><td>  <b>     Subscriber's      Account No.</b></td> <td><b>S-Card No.</b></td><td><b>Decoder     No.</b>    </td>
    <td><b>Contact No</b></td><td><b>Date Installed</b></td><td><b>Activation Date</b></td></tr>";
    while($record = mysql_fetch_array($result)){
    $firstname = $record['firstname'];
        $refno = $record['refno'];
        $smartcard = $record['smartcard'];
        $decoderno = $record['decoderno'];
        $cell = $record['cell'];
        $date= $record['date'];
     #create word document starts here

     $str .= "<tr><td>$firstname</td><td >$refno</td><td >$smartcard</td><td>$decoderno</td>
    <td>$cell</td><td >$date</td><td >..</td>
    </tr>";

     }
     $str .= "</table>";
      fwrite($fp, $str);  
     }
       fclose($fp);
    ?>
于 2013-01-17T07:05:05.057 回答
0

在您的代码中,您在 while 循环中创建表格和表格标题,因此它会重复。您必须将表格和表格标题的代码放在 while 循环之外。请尝试下面给出的代码。

  $str = "<table border=\"1\"><tr><td><b>Subscriber's Name</b></td><td>  <b>     Subscriber's Account No.</b></td> <td><b>S-Card No.</b></td><td><b>Decoder No.</b></td>
<td><b>Contact No</b></td><td><b>Date Installed</b></td><td><b>Activation Date</b></td>
</tr>";

 while($record = mysql_fetch_array($result)){
        $firstname = $record['firstname'];
        $refno = $record['refno'];
        $smartcard = $record['smartcard'];
        $decoderno = $record['decoderno'];
        $cell = $record['cell'];
        $date= $record['date'];

        #create word document starts here
        $str .=  "<tr><td>$firstname</td><td >$refno</td><td >$smartcard</td>  
        <td>$decoderno</td><td>$cell</td><td >$date</td><td >..</td></tr>";

     }
    $str .= "</table>";
    fwrite($fp, $str);  

谢谢

于 2013-01-17T07:13:54.393 回答