PHP Creating CSV not working as expected - php

i'm trying to create a CSV using this solution:
Creating csv file with php
Something is wrong, i keep getting data stuffed into one cell like this:
each value should be in an own cell .. any ideas what's wrong?
seems like the csv uses the wrong delimiter.
thanks

I finally found the solution: seems like it doesn't work for the German version of Excel,
all I had to do is changing the delimiter to a semi-colon like this:
fputcsv($fp, $val, ";");

Related

create excel line break within string using php xlsx writer

I'm using php xlsx writer (https://github.com/mk-j/PHP_XLSXWriter) to create my excel file and try to figure out how to force excel to make a line break.
My source data contains html tags (<br />) and i want to replace them with the correct formular. The replace function is working but i need how the string needs to be formated to force the break.
The line wrap option at format cells is enabled for the column as shown in the picture below (excel is installed german at the moment - please don't hate me for that)
What i tried so far is using CHAR(10) and CHAR(13) - altough CHAR(13) should be for mac and i am on windows.
I tried the following inputs to get my line break working. Also i tried all of the combinations with \n and \r\n instead of CHAR(10) and CHAR(13)
="text"&CHAR(10)&"text"
='text'&CHAR(10)&'text'
=text&CHAR(10)&text
The data was entered in the formular row in excel (picture below) - i think this is the only possible location to enter this isn't it?
Whatever i try my output always looks like this:
Any suggestions what i could do?
Thanks for your help!
The question got answered in https://github.com/mk-j/PHP_XLSXWriter/issues/114.
There was a release of a new brunch supporting new line character which fixed the problem.

OpenTBS Excel number format

I'm trying to format numbers in an Excel file using PHP and opentbs.
Here's template code I'm working from:
[gross_pay_names;block=begin;sub1=departments][gross_pay_names.name]
[gross_pay_names_sub1;block=begin]
[gross_pay_names_sub1.val; ope=tbs:num]
[gross_pay_names_sub1;block=end]
[gross_pay_names;block=end]
The problem is in the third line:
[gross_pay_names_sub1.val; ope=tbs:num]
It always renders with an apostrophe in the beginning ('0.00). So I can't use it in other formulas in the file.
Ok, I found a solutions myself. In case anybody needs it in the future here's the template code I ended up using:
[gross_pay_names.name;block=tbs:row;sub1=departments] [gross_pay_names_sub1.val;block=tbs:cell;ope=tbs:num]

Write Database table to txt file PHP

I need some help with this problem.
I'm work with laravel, and I got the select which look like this:
$table = Onlineszallas::all()->toArray();
I wanna write this query to a TXT file with latin2 file charset, every row in the table, in new line, strings separated by ; and every row end with;
I tried the fputcsv() method but if I open the txt file I found some string which contain space between to apostrophe like the name row.
"George Michael"
I need some help with file_put_contents() or any other method which can solve my problem!
Many thanks!
You can use Storage facade. Create a file:
use Illuminate\Support\Facades\Storage;
Storage::put('file.txt', $contents);
Add text content to a file:
Storage::append('file.txt', $someOtherContent);
But I would recomment you to store data as JSON:
$table = Onlineszallas::all()->toJson();
In this case you'll be able to easily import the data in any system.

How to add an array into a comma delimited text file without deleting previous array values

I'm trying to make a form where the user can add their own 'questions + answers' to the quiz.
I loaded the original questions from a text file. The added questions will then be processed by process_editadd.php
<?php
session_start();
$file = fopen('data.txt', 'r');
$array=$_SESSION['questions_array'];
//make array out of values
$q=array($_POST['question'],$_POST['one'],$_POST['two'],$_POST['three'],$_POST['four']);
//add to file
$file=fopen("data.txt","w+");
fwrite($file, implode(',', $q)).
header('Location:module.php');
?>
The array adds onto the text file, but the problem is that it replaces the whole thing. I don't want the questions to replace the previous ones, I just want them added. Do you guys know what's wrong with the code?
Note: I'm not allowed using mySQL or Javascript
You could switch to using an actual database and make your life a lot easier... Failing that, look into fputcsv and fgetcsv to make it a slightly less tedious problem.
Your implode version right now is also vulnerable to CSV injection... you don't handle the case where any of the text you're writing MIGHT contain a comma. If it does, you'll suddenly find you'll have extra "columns" when you read the data back in later on.

Excel to PHP array, possible and how?

I have a big excel file that looks like this:
I would like to put each row into an array.
Is this possible to access the first row's order id like this?
$result[0][2] // returns 7432
Assuming the actual first row that gives prefix for the columns' name is not present.
How could I do that?
I recommended to use PHPEXCEL library
https://github.com/PHPOffice/PHPExcel
you can see an example
Update:
Now the alternative to this library is phpspreadsheet
Save the spreadsheet as a CSV, then use PHP's built-in CSV functions. See the sample code here:
http://php.net/manual/en/function.fgetcsv.php
May be my answer is too simple (for one time work only), but I use the CONCATENATE "Function" in excell.
The last cell on each row will have concatenation function, like this:
=CONCATENATE("['";A2;"'=>['data1' => '";B2;"', 'data2' => '";C2;"'],")
where:
column "A" is ID of something;
column "B" is first characteristic;
column "C" is second characteristic;
etc.
Then just copy and paste function results to Your script or config file, and do not forget the first and the last bracket.
This works for me:
$content = file_get_contents($your_file_path);
$lines = array_map("rtrim", explode("\n", $content));
Since the PHPExcel library deprecated they've released "PhpSpreadsheet"
This will help PhpSpreadsheet

Categories