I am using PHPleague to parse csv and insert it to the database. https://csv.thephpleague.com/
And I have code as follows:
$csv = Reader::createFromPath($path, 'r');
$csv->skipEmptyRecords();
$csv->setHeaderOffset(0);
$csv_header = $csv->getHeader();
$stmt = (new Statement())
->offset('0')
->limit('20')
;
$records = $stmt->process($csv);
foreach($records as $record){
print_r($record); // this show that it has processed empty records as well
}
exit();
I could trim the empty records myself as well as by calling following function as $records = $this->trimArray($records);
public function trimArray($arr)
{
$final = array();
foreach($arr as $k => $v)
{
if(array_filter($v)) {
$final[] = $v;
}
}
return $final;
}
However, I want it to be trimmed before than that. Instead of adding my own layer to filter it is there anyway on csvleague to skipemptyrecords. I have tried with $csv->skipEmptyRecords(); but it is not skipping empty records. Am I doing anything wrong?
My file looks like:
I want to skip all those empty records.
I had a simple experience with League\CSV and tried to catch the data from a csv file. The package codes depend strongly on the version that you have installed. So at first step check the version of League. I think you should read the League documentation page about stream filters.
https://csv.thephpleague.com/9.0/connections/filters/
I am new to PHP and trying to loop and concatenate csv column values
My input is
and expected output is
can somebody help me how to loop the column values to get the expected output and thanks in advance.
Basically, you need to read the CSV file line-by-line, store parsed data in array, and then do some nested loops.
This code will do the job for your example (only works for 3 columns):
<?php
$rows = [];
$file = fopen("your-file.csv", "r"); // open your file
while (!feof($file)) { // read file till the end
$row = fgetcsv($file); // get current row as an array of elements
if (!empty($row)) { // check that it's not the empty row
array_push($rows, $row); // store row in array
}
}
fclose($file);
for ($r1 = 0; $r1 < count($rows); $r1++) {
for ($r2 = 0; $r2 < count($rows); $r2++) {
for ($r3 = 0; $r3 < count($rows); $r3++) {
echo $rows[$r1][0] . '_' . $rows[$r2][1] . '_' . $rows[$r3][2] . PHP_EOL;
}
}
}
It's pretty dirty solution, I hope that you can make it cleaner, using recursion, instead of nested loops in case if you need to parse unknown columns count.
If you have another fixed number of columns, just add more nested loops (for $r4, $r5, and so on).
More info about how to read CSV file in PHP on w3schools.com
Documentation about join() function, which is alias of implode() function on php.net
I have data coming back from MySQL and the goal is to pull data from multiple servers.
The data can be grouped by one or more fields, and I want to pull data from X number of MySQL servers and sum the data that should be summed, using the other data as a key or index.
Example:
Result from Mysql[$x]:
[{"date":"2017-10-10","account":"1","trees":"1","people":"0","pets":"4"}
,{"date":"2017-10-10","account":"2","trees":"2","people":"5","pets":"1"}
,{"date":"2017-10-11","account":"1","trees":"3","people":"3","pets":"4"}
,{"date":"2017-10-11","account":"2","trees":"4","people":"1","pets":"4"}]
Result from Mysql[$x+1]:
[{"date":"2017-10-10","account":"1","trees":"5","people":"1","pets":"1"}
,{"date":"2017-10-10","account":"2","trees":"5","people":"2","pets":"1"}
,{"date":"2017-10-11","account":"1","trees":"5","people":"0","pets":"2"}
,{"date":"2017-10-11","account":"2","trees":"5","people":"1","pets":"2"}]
Desired end result:
[{"date":"2017-10-10","account":"1","trees":"6","people":"1","pets":"5"}
,{"date":"2017-10-10","account":"2","trees":"7","people":"7","pets":"2"}
,{"date":"2017-10-11","account":"1","trees":"8","people":"3","pets":"6"}
,{"date":"2017-10-11","account":"2","trees":"9","people":"2","pets":"6"}]
in this case, there are 2 keys, date and account, but in reality there can be between 1 and 4.
The grouping keys are known, so the code knows that in this case, date and account are the keys.
I'm trying to find a better way than iterating through each row of mysql[$x] and then iterating through each row of mysql[$x+1] ... mysql[$x+n] and checking each value of the known keys to see if they match the current row's key values and then summing the data.
Also note that in reality, there may not be a row for each set of keys, hence the number and order of rows from each server can be variable.
I need an efficient way to do this because there are potentially tens of thousands of rows with a dozen+ columns.
I'm experimenting with code like this:
$res = array();
$keys = array('date','account');
array_walk($t,'rehash',$res);
function rehash($data,$key,&$result) {
global $keys;
global $res;
$keydata = array_slice($data,0,count($keys));
$vals = array_slice($data,count($keys));
//this is how the data would ideally be structured, my model
//$res[$keydata[$keys[0]]][$keydata[$keys[1]]] = $vals;
//this code below successfully creates rows in the right format
for($x = count($keys) - 1; $x>= 0; $x--) {
if($x == count($keys)-1) {
$tmp = array($keydata[$keys[$x]] => $vals);
//} else if($x ==0) {
// maybe reconcile here?
} else {
$tmp = array($keydata[$keys[$x]] => $tmp);
}
}
}
But how do i reconcile this? I can't simply do $res[key1] = $tmp[key1] because with multiple keys it would remove all the other rows.
Also with nested arrays, how would I iterate through each nested array if the number of levels is variable?
Lastly if I do this, I would need to convert it back into the original format for the frontend, so... joy. :D (this part would be easy, just is worth taking into consideration from an efficiency standpoint)
You don't need to use array_walk, since your data isn't hierarchical. Just use an ordinary foreach loop.
$res = array();
foreach ($t as $row) {
$cur = &$res;
// Drill down into result array using the key fields, creating nested arrays
// as necessary
foreach ($keys as $keycol) {
$key = $row[$keycol];
if (!isset($cur[$key])) {
$cur[$key] = array();
}
$cur = &$cur[$key];
}
// Now go through the data columns in the row, adding them to the totals
foreach ($row as $key => $val) {
if (in_array($key, $keys)) {
// Put the key fields in the result row
$cur[$key] = $val;
} else {
// Accumulate other values in the result row
if (!isset($cur[$key])) {
$cur[$key] = 0;
}
$cur[$key] += $row[$key];
}
}
unset($cur); // Break the reference link
}
Here is a possible solution based on the structure of the sample data given in the question.
$jsn1 = '[{"date":"2017-10-10","account":"1","trees":"1","people":"0","pets":"4"}
,{"date":"2017-10-10","account":"2","trees":"2","people":"5","pets":"1"}
,{"date":"2017-10-11","account":"1","trees":"3","people":"3","pets":"4"}
,{"date":"2017-10-11","account":"2","trees":"4","people":"1","pets":"4"}]';
$jsn2 = '[{"date":"2017-10-10","account":"1","trees":"5","people":"1","pets":"1"}
,{"date":"2017-10-10","account":"2","trees":"5","people":"2","pets":"1"}
,{"date":"2017-10-11","account":"1","trees":"5","people":"0","pets":"2"}
,{"date":"2017-10-11","account":"2","trees":"5","people":"1","pets":"2"}]';
/* Decode the json strings into associative arrays: */
$arr1 = json_decode($jsn1, true);
$arr2 = json_decode($jsn2, true);
/* Put in an array the keys whose values are to be added: */
/* Do not include date and account keys if they are not to be added */
$arr_keys = ['trees', 'people', 'pets'];
/* Loop through both $arr1 and $arr2 while storing new arrays in arr: */
/* Be adding up the values for the keys that are in $arr_keys */
$arr = [];
foreach($arr1 as $k => $v){
$arr[$k]['date'] = $arr1[$k]['date']; $arr[$k]['account'] = $arr1[$k]['account'];
foreach($arr_keys as $y){
if($arr1[$k][$y] !== null && $arr2[$k][$y] !== null){
$arr[$k][$y] = $arr1[$k][$y] + $arr2[$k][$y];
}}}
/* encode the array to the required json string: */
$json = json_encode($arr);
echo $json;
/* Output:
[{"date":"2017-10-10","account":"1","trees":"6","people":"1","pets":"5"}
,{"date":"2017-10-10","account":"2","trees":"7","people":"7","pets":"2"}
,{"date":"2017-10-11","account":"1","trees":"8","people":"3","pets":"6"}
,{"date":"2017-10-11","account":"2","trees":"9","people":"2","pets":"6"}]
*/
im new to php and i hope you can help with an easy to get script. I have a txt file with this general format:
5TB-2A, L+, 1, 1695, 3255, l.jpg
5TB-2A, L-, 2, 1965, 3270, l.jpg
5TB-2A, D-, 7, 2970, 3270, l.jpg
5TB-2A, DFOK, 8, 3225, 3300, l.jpg
I want to read this txt file using php to retrive every data separated by "," into my MySQL table. Thanks for any help.
$lines=file($yourTxt);
foreach($lines as $v) {
$values = explode(',',$v);
PDO::prepare("INSERT INTO tbl (?,?,?,?,?)");
PDO::execute($values);
}
You can make a further check too see if the line just read is ok like:
foreach($lines as $v) {
$values = explode(',',$v);
if (count($values)!=6)
continue;
}
You could use fgetcsv (http://php.net/fgetcsv) to read the file into an array in PHP and then write a query to insert it into the table one row at a time.
<?php
$results = array();
$rows = file($filename, FILE_SKIP_EMPTY_LINES);
if(empty($rows)){
// no data to parse
} else{
foreach($rows as $row){
$values = explode(', ', $row);
// keep only those values which have the 5 variables
if(count($values) > 5){
$results[] = $values;
}
}
}
// see if we have the correct data
print_r($results);
// ... then do your db inserts here
?>
I want to retrieve xml data from three table stored in MySQL. I used the below code it works fine but first it retrieve one record from first table than iterate to second table and print the whole table and then iterate to third table and print the whole table but I want to print first table along with relevant records in second table (not whole table) then from third table and so on. My code is:
$table_first = 'recipe';
$query = "SELECT * FROM $table_first";
$resouter = mysql_query($query, $conn);
$table_second='instructions';
$query="SELECT instructions.instruction_id,instructions.instruction_text FROM $table_second";
$resinner=mysql_query($query, $conn);
$table_third='ingredients';
$query="SELECT ingredients.ingredient_id,ingredients.ingredient_name,ingredients.ammount FROM $table_third";
$resthird=mysql_query($query, $conn);
$doc = new DomDocument('1.0');
$root = $doc->createElement('recipes');
$root = $doc->appendChild($root);
while($row = mysql_fetch_assoc($resouter)){
$outer = $doc->createElement($table_first);
$outer = $root->appendChild($outer);
foreach ($row as $fieldname => $fieldvalue) {
$child = $doc->createElement($fieldname);
$child = $outer->appendChild($child);
$value = $doc->createTextNode($fieldvalue);
$value = $child->appendChild($value);
}// foreach
//while
$inner = $doc->createElement($table_second);
$inner = $outer->appendChild($inner);
while($row = mysql_fetch_assoc($resinner)){
// add node for each record
$inner1=$doc->createElement('instruction');
$inner1=$inner->appendChild($inner1);
// add a child node for each field
foreach ($row as $fieldname => $fieldvalue) {
$child = $doc->createElement($fieldname);
$child = $inner1->appendChild($child);
$value = $doc->createTextNode($fieldvalue);
$value = $child->appendChild($value);
} // foreach
}// while
$inner=$doc->createElement($table_third);
$inner=$outer->appendChild($inner);
while($row=mysql_fetch_assoc($resthird)){
$inner2=$doc->createElement('ingredient');
$inner2=$inner->appendChild($inner2);
foreach($row as $fieldname=> $fieldvalue)
{
$child=$doc->createElement($fieldname);
$child=$inner2->appendChild($child);
$value=$doc->createTextNode($fieldvalue);
$value=$child->appendChild($value);
}
}
}
mysql_close($conn);
$xml_string = $doc->saveXML();
echo $xml_string;
There is a much better way to accomplish what you're doing. It looks as though you pulling data from 3 different tables, using three different SQL queries, and three calls to the database.
The best way to accomplish this would be to build your table on the SQL side, using a single query. The queries are a little more complicated (you'll need JOIN) however the result will be much easier to work with in your program. You'll also probably avoid having to create your document on the fly using DOM. You could simple build your recipe using SQL, pull the whole result back and simply iterate through the table with the result.