如何在codeigniter中导出两个日期之间的报告

chhqkbe1  于 2022-12-07  发布在  其他
关注(0)|答案(2)|浏览(98)

我试图导出codeigniter中的report .csv文件,记录分配在DB中的两个表中,问题是当我试图获取两个日期之间的数据时,它给予我一个空白文件,但它工作正常,没有查询之间!
另一件事,我需要在UI上显示报告(视图),然后再导出它,这不工作。
控制器:

public function Export_appointments() {
    //check if the user is admin
    $this->session->set_userdata('dest_url', site_url('backend/users'));
             if ( ! $this->has_privileges(PRIV_USERS))
            {
         return;
     }
    $this->load->dbutil();
    $this->load->helper('file');
    $this->load->helper('download');
    $this->load->view('backend/settings');
/* read input data*/
$date1 = date('Y-m-d',  strtotime($this->input->post("from_date"))); //date type
$date2 = date('Y-m-d', strtotime($this->input->post("to_date")));
/*working query*/
$this->db->select('first_name, last_name,     book_datetime,start_datetime,end_datetime');
$this->db->from('ea_appointments');
$this->db->join('ea_users', 'ea_users.id = ea_appointments.id');
 $this->db->where('ea_users.id_roles',3); 
/* I tried this ------book_datetime is datetime type---*/ 
          $this->db->where('CAST(book_datetime As Date) >= ',$date1);
          $this->db->where('CAST(book_datetime As Date) <= ',$date2);
          /* also this -------
$this->db->where('CAST(book_datetime as date) BETWEEN "'. date('Y-m-d',  strtotime($date1)). '" and "'. date('Y-m-d', strtotime($date2)).'"');*/ 
        $query = $this->db->get()->result_array();
        $fp = fopen('php://output', 'w');
        $header = array("First_Name","Last_Name","Booking_Date","Start_Date","End_Date"); 
    fputcsv($fp, $header);
    foreach ($query as $key => $value) {
    fputcsv($fp, $value);
    }

    $data = file_get_contents('php://output');
    $name = 'report.csv';

    // Build the headers to push out the file properly.
    header('Pragma: public');     // required
    header('Expires: 0');         // no cache
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Cache-Control: private',false);
    header('Content-Disposition: attachment; filename="'.basename($name).'"');  // Add the file name
    header('Content-Transfer-Encoding: binary');
    header('Connection: close');
    exit();
    force_download($name, $data);
    fclose($fp);
}

查看:

<?php $hidden = ($privileges[PRIV_SYSTEM_SETTINGS]['view'] == TRUE) ? '' : 'hidden'; ?>
<div id="export" class="tab-pane <?php echo $hidden; ?>">
<form class="row"  method= "post" action="<?php echo base_url();  ?>backend/Export_appointments">
<fieldset class="col-xs-12 col-sm-6 miscellaneous-wrapper">
<legend>Export Appointment Data from:</legend>
<input type="date" class="form-control" name="from_date">
<legend>To:</legend>
<input type="date" class="form-control" name="to_date">
<button type="button" id="data-csv-export" class="btn btn-default" onclick="window.location='<?php echo site_url("backend/Export_appointments");?>'">Download report.CSV</button>
<div class="panel-body">
 <div class="table-responsive">
  <table class="table table-bordered table-striped">
   <tr>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Book Date</th>
    <th>Start Date</th>
    <th>End Date</th>
   </tr>
   <?php
  /* foreach($query as $row)
   {
    echo '
    <tr>
     <td>'.$row["first_name"].'</td>
     <td>'.$row["last_name"].'</td>
     <td>'.$row["book_datetime"].'</td>
     <td>'.$row["start_datetime"].'</td>
     <td>'.$row["end_datetime"].'</td>
     </tr> ';  }*/
     ?>
   </table>
   </div>
     </div>
       </fieldset>
          </form>
            </div>
py49o6xq

py49o6xq1#

您可以使用“控制器名称”/export_appointments代替url格式中的base_url()。

9lowa7mx

9lowa7mx2#

您使用了$_post而不是$_POST来引用表单中发布的值。此外,您在标头后面有一个exit()语句,它是输出所必需的,以防止进一步执行,但它不是有条件的,并且将始终退出,因此它永远不会到达加载视图函数。在执行标头和退出之前的建议:

if (!empty($_POST)) {
    // Code that does things only when the form has been submitted. Headers and export features go here.
} else {
    // Do things when the form is NOT submitted. The load view will go here.
}

这应该会让你更接近解决方案。
如果您的表单/表单插件中已经捕获了Y-m-d格式的日期,您可以跳过对日期的strtotime()调用-在这种情况下,只需将发布的日期值放入查询中。请在这样做之前清理您的输入-您有SQL注入的风险。

相关问题