i have a database table where except id column all columns have comma separated values, i am trying to download the data according to id, so i did the following code:
view:
<a href="<?php echo base_url('admin_works/downloadexcel/'.$val['id']);?>" style="margin-left:7%">
<i aria-hidden="true" style="color:green"></i>
</a>
controller:
public function downloadexcel(){
$this->load->helper('file');
$this->load->helper('download');
// file name
$filename = 'users_'.date('Ymd').'.csv';
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/csv; ");
// get data
$eid = $this->uri->segment(3);
$response = $this->db->where("id", $eid)->get('tbl_plans')->result();
// file creation
$file = fopen('php://output', 'w');
$header = array("Table ID","Date","Topic","Name","Client ID","Type","Reference","Status","Assigned","Image URL","Remarks","Captions");
fputcsv($file, $header);
foreach ($response as $val){
$s1=explode(',',$val->ddate);
$s2=explode(',',$val->details);
$s3=explode(',',$val->type);
$s4=explode(',',$val->ref);
$s5=explode(',',$val->status);
$s6=explode(',',$val->assign);
$s7=explode(',',$val->imageurl);
$s8=explode(',',$val->remarks);
$s9=explode(',',$val->captions);
for($i=1;$i<10;$i ){
for($j=1;$j<10;$j ){
$var = 's'.$j;
$newArr[] = $$var[$i]??'';
}
fputcsv($file,$newArr);
}
}
fclose($file);
exit;
}
however this gives me blank file, can anyone please tell me what is wrong in here, thanks in advance
CodePudding user response:
for($i=0;$i<count($s1);$i ){
$newArr=[];
for($j=1;$j<12;$j ){
$var = 's'.$j;
$newArr[] = $$var[$i]??'';
}
fputcsv($file,$newArr);
}
CodePudding user response:
This should be a comment, but space is limited.
i have a database table where except id column all columns have comma separated values
Then you are starting from a really bad place. Unless this is something like sleepycat or memcache, then you shouldn't be storing CSV formatted data inside the database. However that code looks a lot like mangled SQL in which case you have no reason known to us for putting CSV in there.
can anyone please tell me what is wrong in here
Yes, PHP is already telling you but you are not listening. Look at your error logging/reporting.
this gives me blank file
If that really is the case AND a file is being created AND you are not getting even the column headers then you have a problem arising in a different dimension from the ones PHP usually runs in. Speak to an exorcist/shaman.
If the file is not being recreated each time you run this, check your error logging/reporting and filesystem permissions.
If the file is being recreated AND it contains a header row, then there are issues in your code - since you keep changing the code you published there's not much point in enumerating these - but most of them will be reported by PHP as errors/warnings.