【问题标题】:How to fetch id of selected chebox and export selected record as CSV using Codeigniter()?如何使用 Codeigniter() 获取所选 chebox 的 id 并将所选记录导出为 CSV?
【发布时间】:2019-08-05 04:18:30
【问题描述】:

我不知道如何获取 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>

【问题讨论】:

    标签: php html codeigniter


    【解决方案1】:

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

    像下面这样访问它们

    $ids=$this->input->post('select_checkbox');
    

    如果你这样做了,print_r($ids),你会得到如下的值

    Array ( [0] => 1 [1] => 2 )
    

    然后,创建查询并导出到 csv,codeigniter dbutil 有简单的方法将您的查询转换为 csv,下面是 sn-p 来做到这一点,

    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 sn-p 将是,

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

    【讨论】:

    • 我的按钮在桌子外面。我该如何管理?我如何获得帖子价值。
    • 我也尝试使用 ajax,但它只是打印数据,而不是下载内容
    猜你喜欢
    • 2019-03-20
    • 2020-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多