CSV data from interface to MySQL with PHP - php

I have this data from a real time interface call of an url:
"DATETIME";"ACTION";"ACTIONTEXT";"POSITION";"AREA";"DRIVERKEY";"DRIVERNAME";"CAR";"LAT";"LON";
"21.05.2013 10:59:00";"G";"Arbeitsende";"";"20037100 ARCUS Cottbus";"0000161395E7";"Süsse, Daniel";"Neubuchung";"0";"0";
"21.05.2013 19:07:00";"G";"Arbeitsende";"";"20037100 ARCUS Cottbus";"0000161395E7";"Süsse, Daniel";"Neubuchung";"0";"0";
"27.05.2013 05:38:05";"K";"Arbeitsbeginn";"D-02625 Bautzen Hanns-Eisler-Straße 10";"";"0000161395E7";"Süsse, Daniel";"BZ-DK 101";"51.17421";"14.45179";
"27.05.2013 06:04:25";"K";"Arbeitsbeginn";"D-02733 Cunewalde Gewerbegebiet Roßkaupe 11";"";"00001613B355";"Wypior, Andreas";"BZ-DK 11";"51.09184";"14.53524";
"27.05.2013 06:20:26";"K";"Arbeitsbeginn";"D-02733 Cunewalde Gewerbegebiet Roßkaupe 11";"";"000016127326";"Henze, Jens";"BZ-DK 55";"51.09194";"14.53433";
"27.05.2013 06:52:33";"K";"Arbeitsbeginn";"D-97792 Riedenberg";"";"000016139436";"Rother, Thomas";"BZ-DK 260";"50.3171";"9.845136";
I have tried different functions to get this data directly from the url call imported (structured) to a MySQL database via PHP but I always end up having the entire data in one field, not separated like the following:
DATETIME: 21.05.2013 10:59:00
ACTION: G
ACTIONTEXT: Arbeitsende
...etc
Hope you can help/teach me solving this.
The PHP part for saving the csv data (text in body-element) in a temporary csv:
$fp = fopen('$url', 'w');
$delimiter = ';';
$headers = array('DATETIME', 'ACTION', 'ACTIONTEXT', 'POSITION');
fputcsv($fp, $headers, $delimiter);
foreach ($worker as $worker) {
$fields = array($worker->...);
fputcsv($fp, $fields, $delimiter);
}
fclose($fp);
I really do not know how to grab this text (csv-formated) directly from the site to have it correctly imported to a MySQL db.

foreach ($worker as $worker)
This is looking ugly and "shadows" the $worker array from the global scope with a local variable of the same name in the scope of the loop. In the loop (and worse as that: even after the loop), the original $worker array has been replaced by one of its elements.
Its probably just a typo, but if it is from real code, please consider choosing unique variable names.

Related

How to split csv data in two parts.?

Is it possible to export csv data in to two parts:
From the below image i have two things to be considered
1. summery
2. Detail information
I worked with only 2nd type is it possible to do like 2 batches(like shown in image)..?
please suggest any alternate idea if you got.
Example:
summary header
$titleSummery = array('Course Name','Average watched','semi watched','notwached','sudents attempted','sudents notattempted','Total students','Branch','passout');
/*summery data */
Details header
$titleDetail = array('student','passout','branch','percentage watched','student email');
/*Details data */
In this case how can i export the data..?
$output = fopen('php://output', 'w');
fputcsv($output, $title);
foreach($data as $k=>$res){
fputcsv($output,$res);
}
You need to prepare array for each line. see my inline comments.
$titleSummery = array('Course Name','Average watched','semi watched','notwached','sudents attempted','sudents notattempted','Total students','Branch','passout');
$titleSummeryData = array('Number System','50%','40%',....); // fill remaining data.
$output = fopen('php://output', 'w');
// put first table
foreach($titleSummery as $key=>$val){
fputcsv($output,array($val,$titleSummeryData[$key]));
}
// begin second table
// put all title/header
fputcsv($output,$titleDetail);
// For second table i assume that you have data in 2D array
foreach($titleDetailsData as $row){
fputcsv($output);
}
fclose($output);
You direction is good, you just need to understand that each call to fputcsv prints a line, so you'll need to call it for each row in the first batch of data also, for example:
fputcsv($output,"course name","php for dummies");

Write 2 columns in a csv file from an array

I'm trying to write in 2 differents columns in a csv file but everything I've found set all the values in the first column with a separator.
Here is a small modification of the script example on php.net
<?php
$list = array (
['City', 'Country'],
['Brussels', 'Belgium'],
['Paris', 'France'],
['Berlin', 'Germany']
);
$fp = fopen('file.csv', 'w');
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
?>
And here is my csv file (I use Excel)
My question: is it possible to have the cities in A column and the countries in B column?
Thanks you for your help
You need to tell your spreadsheet program that the columns are separated by commas.
You will probably get the option to nominate a delimiter on opening the file.
I think you also have an option on a column - something like "Text to columns"
You can tell fputcsv() to use a different string as the delimiter instead of comma. This will use TAB, which is probably Excel's default:
fputcsv($fp, $fields, "\t");

How to use setcsvcontrol()

Good morning,
I'm developing a script to create a csv file. My problem is the delimiter of csv file, I need to use ";".
I read that I have to use "setcsvcontrol()" but I'm trying to use it and I haven't had any results.
This is a part of my script:
$create = fopen($fecha."p.csv", "a");
$file = new SplFileObject($fecha."p.csv");
$file->setFlags(SplFileObject::READ_CSV);
$file->setCsvControl(';');
foreach($cadena as $fields){
fputcsv($create,$fields);
}
Thanks.
SplFileObject::setCsvControl() only affects the object on which it was called, i.e. the $file object in your case. It does not affect the $create object that you got from fopen, even though both objects operate on the same file.
Either write to the $create object using fputcsv($handle, $fields, $delimiter, $enclosure), passing $create as first argument and the desired delimiter and enclosure as third and fourth arguments, or call $file->fputcsv(). In the latter case, you have to create the SplFileObject as writable using a suitable $open_mode.
Your not passing the delimiter to fputcsv;
fputcsv($create,$fields, ";");
you can also safely remove
$file = new SplFileObject($fecha."p.csv");
$file->setFlags(SplFileObject::READ_CSV);
$file->setCsvControl(';');
if you must use the SplFileObject Then change your code to.
$file = new SplFileObject("data.csv");
$file->setFlags(SplFileObject::WRITE_CSV);
$file->setCsvControl(';');
foreach ($rows as $row) {
$file->fputcsv($row);
}

How to update csv column names with database table header

I am facing this problem some past days and now frustrate because I have to do it.
I need to update my CSV file columns name with database table header. My database table fields are different from CSV file. Now the problem is that first I want to update column name of CSV file with database table headers and then import its data with field mapping into database.
Please help me I don't know how I can solve this.
This is my php code:
$file = $_POST['fileName'];
$filename = "../files/" . $file;
$list = $_POST['str'];
$array_imp = implode(',', $list);
$array_exp = explode(',', $array_imp);
$fp = fopen("../files/" . $file, "w");
$num = count($fp);
for ($c = 0; $c < $num; $c++) {
if ($fp[c] !== '') {
fputcsv($fp, $array_exp);
}
}
fclose($fp);
require_once("../csv/DataSource.php");
$path = "../files/test_mysql.csv";
$dbtable = $ext[0];
$csv = new File_CSV_DataSource;
$csv->load($path);
$csvData = $csv->connect();
$res='';
foreach($csvData as $key)
{ print_r($key[1]);
$myKey ='';
$myVal='';
foreach($key as $k=>$v)
{
$myKey .=$k.',';
$myVal .="'".$v."',";
}
$myKey = substr($myKey, 0, -1);
$myVal = substr($myVal, 0, -1);
$query="insert into tablename($myKey)values($myVal)";
$res= mysql_query($query);
You have got an existing file of which the first line needs to be replaced.
This has been generally outlined here:
Overwrite Line in File with PHP
Some little explanation (and some tips that are not covered in the other question). Most often it's easier to operate with two files here:
The existing file (to be copied from)
A new file that temporarily will be used to write into.
When done, the old file will be deleted and the new file will be renamed to the name of the old file.
Your code does not work because you are already writing the new first line into the old file. That will chop-off the rest of the file when you close it.
Also you look misguided about some basic PHP features, e.g. using count on a file-handle does not help you to get the number of lines. It will just return 1.
Here is step by step what you need to do:
Open the existing file to read from. Just read the first line of it to advance the file-pointer (fgets)
Open a new file to write into. Write the new headers into it (as you already successfully do).
Copy all remaining data from the first file into the new, second file. PHP has a function for that, it is called stream_copy_to_stream.
Close both files.
Now check if the new file is what you're looking for. When this all works, you need to add some more steps:
Rename the original file to a new name. This can be done with rename.
Rename the file you've been written to to the original filename.
If you want, you then can delete the file you renamed in 5. - but only if you don't need it any longer.
And that's it. I hope this is helpful. The PHP manual contains example code for all the functions mentioned and linked. Good luck. And if you don't understand your own code, use the manual to read about it first. That reduces the places where you can introduce errors.
If you are managing to insert the table headers then you're half way there.
It sounds to me like you need to append the data after the headers something like:
$data = $headers;
if($fp[c]!=='')
{
$data .= fputcsv($fp, $array_exp);
}
Notice the dot '.' before the equals '=' in the if statement. This will add none blank $fp[c]values after the headers.

how to insert value in a particular location in csv file using php

Is it possible to write at a particular location in a CSV file using PHP?
I don't want to append data at the end of the CSV file. But I want to add data at the end of a row already having values in the CSV.
thanks in advance
No, it s not possible to insert new data in the middle of a file, due to filesystem nature.
Only append at the end is possible.
So, the only solution is to make another file, write a beginning part of source, append a new value, and then append the rest of the source file. And finally rename a resulting file to original name.
There you go. Complete working code:
<?php
//A helping function to insert data at any position in array.
function array_insert($array, $pos, $val)
{
$array2 = array_splice($array, $pos);
$array[] = $val;
$array = array_merge($array, $array2);
return $array;
}
//What and where you want to insert
$DataToInsert = '11,Shamit,Male';
$PositionToInsert = 3;
//Full path & Name of the CSV File
$FileName = 'data.csv';
//Read the file and get is as a array of lines.
$arrLines = file($FileName);
//Insert data into this array.
$Result = array_insert($arrLines, $PositionToInsert, $DataToInsert);
//Convert result array to string.
$ResultStr = implode("\n", $Result);
//Write to the file.
file_put_contents($FileName, $ResultStr);
?>
Technically Col. Shrapnel's answer is absolutely right.
Your problem is that you don't want to deal with all these file operations just to change some data. I agree with you. But you're looking for the solution in a wrong level. Put this problem in a higher level. Create a model that represents an entity in your CSV database. Modify the model's state and call its save() method. The method should be responsible to write your model's state in CSV format.
Still, you can use a CSV library that abstracts low level operations for you. For instance, parsecsv-for-php allows you to target a specific cell:
$csv = new parseCSV();
$csv->sort_by = 'id';
$csv->parse('data.csv');
# "4" is the value of the "id" column of the CSV row
$csv->data[4]['firstname'] = 'John';
$csv->save();

Categories