使用表格式的fwrite()写入文件

时间:2013-03-15 05:37:31

标签: php mysql

嗨,我正在使用此代码并将所有数据提取到excel表...但是它没有以正确的格式出现...如果我想将数据写入tabel的格式看起来像 这个..正如我在下面提到的那样

  <table border="0" cellspacing="0" cellpadding="0" >
    <tbody> 

         <tr>   <input name="id" type="hidden" value="<?php echo $data['0']; ?>" >
            <td><?php echo $data['2']; ?></td>
            <td><?php echo $data['3']; ?></td>
            <td><?php echo $data['8']; ?></td>
            <td><?php echo $data['9']; ?></td>          
            <td><?php echo $data['11']; ?></td>
            <td><?php echo $data['12']; ?></td>
            <td><?php echo $data['4']; ?></td>
            <td><?php echo $data['5']; ?></td>
            <td><?php echo $data['6']; ?></td>
            <td><?php echo $data['13']; ?></td>
             <td><?php echo $data['10']; ?></td>
            <td><?php echo $data['14']; ?></td>
            <td><input name="edit" type="submit" value="Edit">
            <input  name="BOOK" type="submit" value="Book" style="color:#d90f0f">

            </td>
            </tr>



    </tbody>
     </table>"






<?php 
            include 'functions.php';
          ob_start();




  if($_POST['types'] == 'name') {  

      $query = "SELECT * FROM visitor_detail WHERE name='".$_POST['filter']."'                               ORDER BY id     DESC ";  
   }  
                     elseif($_POST['types'] == 'mobile') {  

        $query = "SELECT * FROM visitor_detail WHERE mobile='".$_POST['filter']."' ORDER BY id   DESC ";  
   } 
          elseif($_POST['types'] == 'OccasionType') {  

    $query = "SELECT * FROM visitor_detail WHERE OccasionType='".$_POST['filter']."' ORDER BY id DESC ";  
        } 

     elseif($_POST['types'] == 'InquiryDate') {  
 $query="SELECT * FROM visitor_detail WHERE TodayDate between '".$_POST['From']."' and   '".$_POST['TO']."' ORDER BY id DESC " ;  
   }

  elseif($_REQUEST['types'] == 'OccasionDate') {  
                 $que="SELECT * FROM visitor_detail WHERE date between       '".$_REQUEST['From']."' and '".$_REQUEST['To']."' ORDER BY id DESC ";  
                }
    else {  
             $query = "SELECT * FROM visitor_detail ORDER BY id DESC ";  
        } 

   $sql = mysql_query($query); 


              $line1="Id\tgender\tname\tmobile\taddress\temail\totherno\tzipcode\tOccasionType\tdate\tgue      st\tdescription\tTodayDate\tauther\t";
         $data="$line1\n";
       $br = '<br>';

      $fileName = "./my.txt";
  $file = fopen($fileName, "w");
  fwrite($file, $data."\n");
         $i=1;  
             while($row = mysql_fetch_assoc($sql))
                  {
        $line2=$i . "\t" . $row['gender'] . "\t" . $row['name'] . "\t" .                                                     $row['mobile'] . "\t" .    $row['address'] . "\t" . $row['email'] . "\t" . $row['otherno']  . "\t" .     $row['zipcode'] .    "\t" . $row['OccasionType'] . "\t" . $row['date'] . "\t"                                 . $row['guest'] . "\t" .    $row['description'] . "\t" . $row['TodayDate'] . "\t" .     $row['auther'] . "\t";
  $data1=$line2.$br;
     echo $data1;
          fwrite($file, $data1);
            $i++;
  }
            fclose($file);



 header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename=my.csv');
  header('Pragma: no-cache');
  fclose($file); ?>

1 个答案:

答案 0 :(得分:0)

您正在写入文本文件

$fileName = "./my.txt";

并阅读csv文件

header('Content-Disposition: attachment; filename=my.csv');

我认为应该是

$fileName = "./my.csv";