i'm new on Codeigniter. I want to know how to get table data in view, send it through ajax(maybe), and retrieve the table data in controller(i want to export table data into an excel file). Thanks in advance.
My view:
<table id="mytable">
<?php
$i=1;
foreach ($placeout as $placeout_item){
?>
<tr class="gradeX">
<td class="center"><?php echo $i; $i++; ?></td>
<td><?php echo $placeout_item['pjname']; ?></td>
<td><?php echo $placeout_item['entries_date']; ?></td>
<td><?php echo $placeout_item['lg_id']; ?></td>
<td class="right"><?php echo $placeout_item['entries_amt']; ?></td>
<td class="right"><?php echo $placeout_item['comm']; ?></td>
<td class="center"><?php echo $placeout_item['curr_id']; ?></td>
</tr>
<?php
}
?>
</table>
<button type="reset" class="btn btn-icon btn-default" onclick="exportExcel('Placeout')"><i></i>Export to Excel</button>
My jquery:
<script>
var data_table = [];
$('#mytable td').each(function() {
data_table.push($(this).html()); //make an array from table data
});
function exportExcel(name) {
$.ajax(
{
url: "<?php echo site_url('export/excel/"+name+"'); ?>",
type:'POST', //data type
dataType : "json", //this line should be erased???
data : {tes:data_table}
});
}
</script>
My controller:
function excel($name="")
{
$data_table=$this->input->post('tes');
$objPHPExcel = new PHPExcel();
if($data_table!=null)
{
foreach ($data_table as $data)
{
$exceldata[] = $data;
}
$objPHPExcel->getActiveSheet()->fromArray($exceldata, null, 'A2');
}
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$name.'.xls"');
header('Cache-Control: max-age=0');
$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
}