gpt4 book ai didi

php - 尝试结合两个 Codeigniter 脚本,将数据从 mysql 导出到 excel

转载 作者:行者123 更新时间:2023-11-30 21:46:13 25 4
gpt4 key购买 nike

我在 Tech Arise Team 网站上发现了两个很棒的脚本。第一个是:http://techarise.com/export-data-to-excel-using-codeigniter/第二个是:http://techarise.com/datatable-server-side-processing-codeigniter-mysql-ajax-custom-search/ .我尝试组合这些脚本,得到一个表单,可以在 Codeigniter 框架中根据用户输入的日期范围将过滤后的数据导出到 excel。我修改的脚本是这些:应用程序/ Controller /Order.php:

<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
* Description of Order Controller
*
* @author TechArise Team
*
* @email info@techarise.com
*/
if (!defined('BASEPATH'))
exit('No direct script access allowed');

class Order extends CI_Controller {

public function __construct() {
parent::__construct();
$this->load->model('Site_model', 'site');
}
// upload xlsx|xls file
public function index() {
$data['page'] = 'order';
$data['title'] = 'Data Table | TechArise';
$this->load->view('order/index', $data);
}
// get Orders List
public function getOrderList() {
$orderID = $this->input->post('order_id');
$name = $this->input->post('name');
$startDate = $this->input->post('start_date');
$endDate = $this->input->post('end_date');
if(!empty($orderID)){
$this->site->setOrderID($orderID);
}
if(!empty($name)){
$this->site->setName($name);
}
if(!empty($startDate) && !empty($endDate)) {
$this->site->setStartDate(date('Y-m-d', strtotime($startDate)));
$this->site->setEndDate(date('Y-m-d', strtotime($endDate)));
}
$getOrderInfo = $this->site->getOrders();
$dataArray = array();
foreach ($getOrderInfo as $element) {
$dataArray[] = array(
$element['order_id'],
date(DATE_FORMAT_SIMPLE, $element['order_date']),
$element['name'],
$element['city'],
$element['amount'],
$element['status'],
);
}
echo json_encode(array("data" => $dataArray));
}

}

应用程序/ Controller /Export.php:

    <?php

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
* Description of Export Controller
*
* @author TechArise Team
*
* @email info@techarise.com
*/
if (!defined('BASEPATH'))
exit('No direct script access allowed');

class Export extends CI_Controller {

// construct
public function __construct() {
parent::__construct();
// load model
// $this->load->model('Export_model', 'export');
$this->load->model('Site_model', 'site');
}

// export xls file
public function index() {
$data['page'] = 'order';
$data['title'] = 'tvep.hu - card export';
//$data['employeeInfo'] = $this->export->getOrders();
$this->load->view('order/index', $data);
}

// create xls
public function createXLS() {
// create file name
$fileName = 'befizetesek-' . date("Y-m-d") . '.xls';
// load excel library
$this->load->library('excel');
$getOrderList = $this->site->getOrders();
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
// set Header
$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Order No');
$objPHPExcel->getActiveSheet()->SetCellValue('B1', 'Date');
$objPHPExcel->getActiveSheet()->SetCellValue('C1', 'Name');
$objPHPExcel->getActiveSheet()->SetCellValue('D1', 'City');
$objPHPExcel->getActiveSheet()->SetCellValue('E1', 'Amount');
$objPHPExcel->getActiveSheet()->SetCellValue('F1', 'Status');
// set Row
$rowCount = 2;



$orderID = $this->input->post('order_id');
$name = $this->input->post('name');
$startDate = $this->input->post('start_date');
$endDate = $this->input->post('end_date');
if(!empty($orderID)){
$this->site->setOrderID($orderID);
}
if(!empty($name)){
$this->site->setName($name);
}
if(!empty($startDate) && !empty($endDate)) {
$this->site->setStartDate(date('Y-m-d', strtotime($startDate)));
$this->site->setEndDate(date('Y-m-d', strtotime($endDate)));
}
$getOrderInfo = $this->site->getOrders();
$dataArray = array();
foreach ($getOrderInfo as $element) {
$dataArray[] = array(
$objPHPExcel->getActiveSheet()->SetCellValue('A' . $rowCount, $element['order_id']),
$objPHPExcel->getActiveSheet()->SetCellValue('B' . $rowCount, date(DATE_FORMAT_SIMPLE, $element['order_date'])),
$objPHPExcel->getActiveSheet()->SetCellValue('C' . $rowCount, $element['name']),
$objPHPExcel->getActiveSheet()->SetCellValue('D' . $rowCount, $element['city']),
$objPHPExcel->getActiveSheet()->SetCellValue('E' . $rowCount, $element['amount']),
$objPHPExcel->getActiveSheet()->SetCellValue('F' . $rowCount, $element['status']),
$rowCount++,
);
}
echo json_encode(array("data" => $dataArray));

$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
$objWriter->save(ROOT_UPLOAD_IMPORT_PATH . $fileName);
// download file
header("Content-Type: application/vnd.ms-excel");
redirect(HTTP_UPLOAD_IMPORT_PATH . $fileName);
}

}

应用程序/views/order/index.php:

    <?php
$this->load->view('templates/header');
?>
<div class="row">
<div class="col-lg-12">
<h2>Bankkártyás befizetések exportja - 2018.02.15.-től</h2>
</div>
</div><!-- /.row -->

<div class="row">
<div class="col-lg-2">
<div class="form-group">
<input type="text" name="order_id" value="" class="form-control" id="filter-order-no" placeholder="Order No">
</div>
</div>
<div class="col-lg-2">
<div class="form-group">
<input type="text" name="name" value="" class="form-control" id="filter-name" placeholder="Name">
</div>
</div>
<div class="col-lg-3">
<div class="form-group">
<input type="text" name="order_start_date" value="" class="form-control getDatePicker" id="order-start-date" placeholder="Start date">
</div>
</div>
<div class="col-lg-3">
<div class="form-group">
<input type="text" name="order_end_date" value="" class="form-control getDatePicker" id="order-end-date" placeholder="End date">
</div>
</div>
<div class="col-lg-2">
<div class="form-group">
<button name="filter_order_filter" type="button" class="btn btn-primary btn-block" id="filter-order-filter" value="filter"><i class="fa fa-search fa-fw"></i></button>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<div id="render-list-of-order">
</div>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<a class="btn btn-info btn-xs" style="margin: 2px" value="filter" href="<?php echo site_url() ?>export/createxls"><i class="fa fa-file-excel-o"></i> Export xls</a>
</div>
</div><!-- /.row -->
<?php
$this->load->view('templates/footer');
?>

其他脚本未修改。

问题是导出的文件包含数据表中的所有数据,而不仅仅是过滤后的数据。 :/测试站点是:https://tvep.hu/data-search/我做错了什么?

最佳答案

写你的 base_url 而不是 HTTP_UPLOAD_IMPORT_PATH 和 ROOT_UPLOAD_IMPORT_PATH

关于php - 尝试结合两个 Codeigniter 脚本,将数据从 mysql 导出到 excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49359086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com