php - 如何使用 Codeigniter() 获取所选 chebox 的 id 并将所选记录导出为 CSV?
问题描述
我无法获取如何获取 ID 以导出记录。
我有一个下拉按钮,其中包含三个选项(导入、导出和创建新用户)。单击导出按钮后,选中复选框的记录应导出为 CSV 文件。
按钮的外观:
我有导出 CSV 文件的代码,但我无法获取复选框的 ID。
导出 CSV 文件代码(工作正常):
public function export_csv($param = '')
{
$filename = strtotime("now"). ".csv";
$delimiter = ",";
// Create a file pointer
$f = fopen('php://memory', 'w');
// Set column headers
$fields = array('Id', 'Name', 'Email', 'Phone', 'Created', 'Status');
fputcsv($f, $fields, $delimiter);
$lineData = array(1,'34',$param,'859685968','29-09-2019','online');
fputcsv($f, $lineData, $delimiter);
// Move back to beginning of file
fseek($f, 0);
ob_clean();
// Set headers to download file rather than displayed
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="' . $filename . '";');
// Output all remaining data on a file pointer
fpassthru($f);
// Exit from file
exit();
}
HTML 表格将如下所示:
<table class="table">
<thead>
<tr>
<th style="width: 2%;"><input type="checkbox" name="select_all" id="select_check" class="checkbox select_check" ></th>
<th style="width: 18%;">الاسم</th>
<th style="width: 5%;"></th>
<th style="width: 25%;">البريد الالكتروني</th>
<th style="width: 20%;">مدينة</th>
<th style="width: 20%;">اخر ظهور</th>
<th style="width: 50%;">زيارات الموقع</th>
</tr>
</thead>
<tbody>
<?php if (count($all_visitors) > 0): ?>
<?php foreach($all_visitors as $visitor) : ?>
<form method="POST">
<tr>
<td><input type="checkbox" name="select_checkbox[]" id="select_checkbox" class="checkbox selectall_checkbox" value="<?=$visitor['id']?>"></td>
<td><?= !empty($visitor['full_name']) ? $visitor['full_name'] : 'الاسم لم يعطى حتى الان'?></td>
<td><a href="<?= BASE_URL.'chats?id='.$visitor['unique_id']?>" style="<?= empty($visitor["chat_id"]) ? " pointer-events: none;cursor: default;" :''?>"><i class="fa fa-comments" aria-hidden="true"></i></a> </td>
<td><?= !empty($visitor['email']) ? $visitor['email'] : 'البريد الإلكتروني لم يعطى حتى الان'?></td>
<td><?= !empty($visitor['city']) ? $visitor['city'] : 'لم يتم إعطاء المدينة بعد'?></td>
<td><?= !empty($visitor['updated']) ? get_timeago(strtotime($visitor['updated'])) : '30 Seconds ago'?></td>
<td class="site-visits" ><?= !empty($visitor['total_visits']) ? $visitor['total_visits'] : '0'?></td>
</tr>
</form>
<?php endforeach; ?>
<?php else:?>
<tr><td colspan="6">No records found</td></tr>
<?php endif; ?>
</tbody>
</table>
解决方案
将数据发布到控制器方法后,
像下面一样访问它们
$ids=$this->input->post('select_checkbox');
如果你这样做,print_r($ids)
你将有如下的值,例如
Array ( [0] => 1 [1] => 2 )
然后,创建查询并导出到 csv,codeigniterdbutil
可以轻松地将查询转换为 csv,下面是执行此操作的代码段,
public function export_csv()
{
/* Usually ids will be integers, so lets just filter only integer ids
from user input,
1st RULE : Never Trust User Inputs
2nd RULE : always validate User Inputs
*/
$ids = array_filter( $this->input->post('select_checkbox'), 'is_int');
if($ids)
{
$this->load->dbutil();
$this->load->helper('file');
$this->load->helper('download');
$query = $this->db->select('Id', 'Name', 'Email', 'Phone', 'Created', 'Status')
->where_in('Id', $ids)
->get("your_table");
$delimiter = ",";
$newline = "\r\n";
$data = $this->dbutil->csv_from_result($query, $delimiter, $newline);
/* output to browser */
force_download('Somereport.csv', $data);
}
}
如果您更喜欢单击某个按钮下载,那么您的 JQuery 片段将是,
$(function(){
/* Easy way is to set
action="http://example.com/your_controller/export_csv"
in your export form and then submit
*/
$('#your_export_button_outside_form').on("click",function()
{
/*
$('#your_export_form').attr('action','http://example.com/your_controller/export_csv');
*/
$('#your_export_form').submit();
});
})
推荐阅读
- database-design - 如何通过插入和更新为 Redshift 表规划 diststyle
- database - 如何完全删除 MarkLogic 数据库及其服务器和林?
- python-3.x - 向 Keras Add() 层添加一个激活层,并将该层用作模型的输出
- java - 如何在 Java 中将 ZonedDateTime 转换为毫秒?
- javascript - 为什么一个包的依赖会影响另一个包
- android - 无法在 MPChartView android 上的 xAxis 上设置标签
- java - 如何使用 Gson 删除 Json 对象的嵌套字段
- java - 相同的小数,不同的舍入结果?
- ubuntu - 无法弄清楚 VirtualBox(Ubuntu) 的问题
- windows-10 - Python Asyncio:RuntimeEror:此事件循环已在运行