2013-03-05 100 views
2

我想使用Codeigniter將我的MYSQL數據庫中的數據導出爲DOC文件。 我的代碼如下:命名爲 'profile_top_view.php'Codeigniter - 以DOC格式導出數據庫數據

視圖,其中錨被聲明爲:命名爲 '的welcome.php'

<?php echo anchor('welcome/todoc','Export Posts to DOC File') ?> 

控制器具有功能:

public function todoc() { 
    $id = $this->tank_auth->get_user_id(); 
    $this->mpdf->useOnlyCoreFonts = true; 
    $filename = "POSTS"; 
    $data['member'] = $this->s_model->alldata($id); 
    $this->load->view('export_posts_doc_view', $data, true); 
    $this->index(); 
} 

的圖名爲'export_posts_doc_view.php',其中將爲DOC文件創建表格:

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <meta charset="utf-8"> 
     <title>Exported Posts in PDF File</title> 
    </head> 
    <body> 
     <?php 
     header("Content-Type: application/vnd.ms-word"); 
     header("Expires: 0"); 
     header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
     header("Content-disposition: attachment; filename=\"posts.doc\""); 
     ?> 


     <div id="container"> 
      <h4>Posts</h4> 
      <table border="1"> 
       <tr> 
        <th>title</th> 
        <th>content</th> 
        <th>group</th> 
        <th>video_url</th> 
        <th>pic_path</th> 
        <th>name</th> 
        <th>phone</th> 
        <th>email</th> 
        <th>yes_no</th> 
        <th>single-line-text</th> 
        <th>para_text</th> 
        <th>pdf_file_name</th> 
        <th>add_photo_name</th> 
       </tr> 
<?php 
foreach ($member as $rows) { 
    ?> 
        <tr> 
         <td><?php echo $rows['title'] ?></td> 
         <td><?php echo $rows['content'] ?></td> 
         <td><?php echo $rows['group'] ?></td> 
         <td><?php echo $rows['video_url'] ?></td> 
         <td><?php echo $rows['pic_path'] ?></td> 
         <td><?php echo $rows['name'] ?></td> 
         <td><?php echo $rows['phone'] ?></td> 
         <td><?php echo $rows['email'] ?></td> 
         <td><?php echo $rows['yes_no'] ?></td> 
         <td><?php echo $rows['single-line-text'] ?></td> 
         <td><?php echo $rows['para_text'] ?></td> 
         <td><?php echo $rows['pdf_file_name'] ?></td> 
         <td><?php echo $rows['add_photo_name'] ?></td> 
        </tr> 
    <?php 
} 
?> 
      </table> 

      <br> <br> 

     </div> 
    </body> 
</html> 

,也是一個名爲「s_model.php」的模式,其功能:

function alldata($id) 
    { 
     $this->db->select(''); 
     $this->db->from('posts'); 
      $this->db->where('user-id',$id); 
     $getData = $this->db->get(); 
     if($getData->num_rows() > 0) 
     return $getData->result_array(); 
     else return null; 
    } 

如果我把這個頭那裏,那麼它顯示的錯誤爲:

未在此網址找到網頁地址: http://www.my_ip.com/project/welcome/todoc

但是,如果我刪除了相同的標題,它顯示在控制器呼應的東西。 我從以下URL獲取了該標題: exporting MS word documents using codeigniter?

任何人都可以請讓我知道該怎麼辦?

在此先感謝..

+0

如果您在加載視圖之前設置控制器中的標題,它會有所作爲嗎?每當我使用codeigniter來創建RSS feed,pdf等時,我總是在控制器中設置標題,而不是視圖。 – Jeemusu 2013-03-05 09:04:20

+0

@Jeemusu它顯示爲錯誤 – V15HM4Y 2013-03-05 09:20:18

回答

2

得到了答案,

我改變了我的控制器 '的welcome.php' 幾點變化:

public function todoc() { 
     $id = $this->tank_auth->get_user_id(); 
     $this->mpdf->useOnlyCoreFonts = true; 
     $filename = "POSTS"; 
     $data['member'] = $this->s_model->alldata($id);  
     $this->load->view('export_posts_doc_view', $data); 
    } 
0

看到下面的代碼我希望我會努力for you

function exportexcel() 
    { 
    header("Content-type: application/vnd.ms-excel"); 

    /* change the content-type depends upon our requerment */ 

     header("Content-Disposition: attachment; filename=Sadhak.xls"); 

     header("Pragma: no-cache"); 

     header("Expires: 0"); 

    $table = "your content goes here"; 

    echo $table; 
    }