【问题标题】:The code repeats the table headings :代码重复表格标题:
【发布时间】:2013-01-17 06:49:20
【问题描述】:

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

但我希望它像这样显示 订阅者姓名 订阅者帐号 S-卡号 解码器号 联系人号码 安装日期 激活日期 定时器 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);
    ?>

【问题讨论】:

  • 请不要在新代码中使用 mysql_* 函数。它们不再被维护并被正式弃用。改为了解准备好的语句,并使用 PDO 或 MySQLi

标签: php mysql


【解决方案1】:

你在循环中犯的错误,

     $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); 

【讨论】:

    【解决方案2】:

    因为标题在 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);
        ?>
    

    【讨论】:

      【解决方案3】:
           <?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);
          ?>
      

      【讨论】:

      • 以上更新的代码将解决您的问题。请回复以进行任何澄清
      【解决方案4】:

      在您的代码中,您在 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);  
      

      谢谢

      【讨论】:

        猜你喜欢
        • 2018-09-18
        • 2018-09-15
        • 1970-01-01
        • 1970-01-01
        • 2021-07-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-09
        相关资源
        最近更新 更多