简体   繁体   中英

The code repeats the table headings :

I have a code which writes data from the database to ms document : it works pretty well but it repeats the Table headings like this : Subscriber's Name Subscriber's Account No. S-Card No. Decoder No. Contact No Date Installed Activation Date Timer 0 .. Subscriber's Name Subscriber's Account No. S-Card No. Decoder No. Contact No Date Installed Activation Date Musajhujasasa 903343434342 2013 .. Subscriber's Name Subscriber's Account No. S-Card No. Decoder No. Contact No Date Installed Activation Date

But I want it to display like this 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 Musa 7878787878 Vc3454. Dc56 089898892 2013 2013 Musajhujasasa 87u8u78u8u Vc4565 Dc786 089887387 2013 2013 Dread 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);
    ?>

It repeats because the headings are inside while loop.

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

Your doing mistake in looping,

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

In your code you create table and table heading within while loop so it is repeating.. You have to put code of table and table heading outside of while loop. Please try code given below.

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

thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM