首页 > 解决方案 > 如何使用 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>

标签: phphtmlcodeigniter

解决方案


将数据发布到控制器方法后,

像下面一样访问它们

$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();

         });
})

推荐阅读