I have this code
$dateFile = "data.txt";
$data = $this->Setting->Loop("data");
foreach($data->result() as $dat){
$dataString = "USERNAME| ".$dat->user." / DATA| ".$dat->values_text.".\n";
file_put_contents($dateFile,$dataString);
}
header('Content-Type: application/text');
header('Content-Disposition: attachment; filename="'.$dateFile);
echo file_get_contents($dateFile);
which get data from table data and insert it into file called data.txt with this format
USERNAME| qwq / DATA| www.
My problem is that the code take just one record of data because the data stored in a single string, how can I make it get all records?
Edit #1
I found a solution and this is the new working code
$dateFile = "data.txt";
$data = $this->Setting->Loop("data");
$dataContent = array();
$i = 0;
foreach($data->result() as $dat){
$i++;
$dataContent[$i] = "USERNAME| ".$dat->user." / DATA| ".$dat->values_text.".\n";
}
file_put_contents($dateFile,$dataContent);
header('Content-Type: application/text');
header('Content-Disposition: attachment; filename="'.$dateFile);
echo file_get_contents($dateFile);
Try this code:
$filename = __dir__.'test.php';
$data = 'This is sample text';
fopen($filename, 'w') or die('Cannot open file: '.$filename);
fwrite($handle, $data);
Hope this will help you
Related
I have a database table where except the id column all other columns have comma separated values. I am trying to download the data related to the id, so I used the following code:
view:
<a href="<?php echo base_url('admin_works/downloadexcel/'.$val['id']);?>" style="margin-left:7%">
<i class="fa fa-file-excel-o" 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 a blank file, what is wrong with my code?
for($i=0;$i<count($s1);$i++){
$newArr=[];
for($j=1;$j<12;$j++){
$var = 's'.$j;
$newArr[] = $$var[$i]??'';
}
fputcsv($file,$newArr);
}
I have to fetch the result of a mysql query to a "user friendly" excel (.xls or .xlsx) table.
I don't want to use imports or packages for php.
This is what I got so far:
<?php
function query_to_csv($database, $query, $filename, $attachment = false, $headers = true) {
if ($attachment) {
// send response headers to the browser
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=' . $filename);
$fp = fopen('php://output', 'w');
} else {
$fp = fopen($filename, 'w');
}
$result = mysqli_query($database, $query) or die(mysqli_error($database));
foreach ($result as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
}
This is the
Result
How can I get the array sperated in different Cells?
Sadly I had to use a library named PHP Excel (old) or PhpSpreadsheet (new).
$objReader->setDelimiter(',');
That's the PHP Excel Function to set the Delimiter.
Im having a problem of exporting my csv. Yes it can export but when it exported the colmun name is included. How can i remove the first row (column name) after i exported?
Tried looking for other solution yet it doesnt fit on my program
<?php
//include database configuration file
include 'config2.php';
//get records from database
$query = $db->query("SELECT * FROM maternalproblem ");
if($query->num_rows > 0){
$delimiter = ",";
$filename = "maternalproblem" . date('Y-m-d') . ".csv";
//create a file pointer
$f = fopen('php://memory', 'w');
//set column headers
$fields = array('MPID', 'district_id', 'barangay_id', 'PID', 'tuberculosis', 'sakit','diyabetes','hika','bisyo');
fputcsv($f, $fields, $delimiter);
//output each row of the data, format =line as csv and write to file pointer
while($row = $query->fetch_assoc()){
$lineData = array($row['MPID'], $row['district_id'], $row['barangay_id'], $row['PID'], $row['tuberculosis'],$row['sakit'],$row['diyabetes'],$row['hika'],$row['bisyo']);
df.to_csv($filename , header=False);
fputcsv($f, $lineData, $delimiter);
}
//move back to beginning of file
fseek($f, 0);
//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;
?>
I just need to export the data and not with the column name. Thank you
It would probably be simpler to just not put the column titles out into the file
So remove these lines
//set column headers
$fields = array('MPID', 'district_id', 'barangay_id', 'PID', 'tuberculosis', 'sakit','diyabetes','hika','bisyo');
fputcsv($f, $fields, $delimiter);
I'm trying to create a PHP page that opens the query and automatically updates the csv file with a new value.
I disc location I have file "graph.csv" with date, and COUNT from SQL Query.
I try open this file and add new row everytime when page is loaded. But, currently browser try save and overwrite file manual. php and csv file are in the same location.
<?php
$connect = oci_connect("login","pass","db");
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=graph.csv');
$output = fopen("php://output", "w");
fputcsv($output, array('TIME','Orders'));
$query = "SELECT column1, column2 FROM table1";
$result = oci_parse($connect, $query);
$r=oci_execute($result);
while ($row = oci_fetch_array($result, OCI_BOTH))
{
$handle = fopen("graph.csv", "a");
$handle = fputcsv($output, $row);
}
$handle = fclose($output);
?>
When I export data to csv file and open file with wordpad myfield Sr. No. looks like Sr. No. I don't want double space after Sr. and also don't wanr space after No. Code is as follow.
$filename = "file.csv";
$fp = fopen('php://output', 'w');
$array = array('Sr. No.','Name','DOB','Address');
$header = str_replace('',' ', $array);
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);
fputcsv($fp, $header,',',' ');
$query = "select * from registratin";
$result = mssql_query($query);
$i = 1;
while($row = mssql_fetch_row($result)) {
$row = array_merge(array($i), $row);
fputcsv($fp, $row);
$i++;
}
If yo don't want your headers line to conform to the rules for a CSV, then just write that one line using fwrite() rather than fputcsv()
$headerLine = 'Sr. No,Name,DOB,Address';
fwrite($fp, $headerLine);