Issue adding data from csv to mysql using PHP - php

I have a PHP code that is supposed to save values fro a CSV file into a MySql database table. Everything works fine except that only the first row of the CSV is added. Here's the code:
<?php
public function saveProductsFromCsv($productId, $val) {
$productId = (int) $productId;
$data = array();
$fieldNames = $this->_config->getCsvColumnNames();
array_shift($fieldNames);
$numberOfFields = count($fieldNames);
$lines = explode("\n", $val);
foreach ($lines as $line) {
$line = trim($line);
if (empty($line))
continue;
$values = explode(',', $line);
if (count($values) != $numberOfFields){
throw new Exception();
return;
}
$make = trim($values[0]);
$model = trim($values[1]);
$yearFrom = (int) $values[2];
$yearTo = (int) $values[3];
$engine = trim($values[4]);
if ($yearFrom > 0){
if ($yearFrom < 1950){
$yearFrom = 1950;
} elseif ($yearFrom > 2030){
$yearFrom = 2030;
}
}
if ($yearTo > 0){
if ($yearTo < 1950){
$yearTo = 1950;
} elseif ($yearTo > 2030){
$yearTo = 2030;
}
}
$data[] = array($productId, $make, $model, $yearFrom, $yearTo, $engine);
}
if (count($data) > 0){
$this->saveValues($data);
}
}
public function saveValues($data)
{
$valuesStr = '';
foreach ($data as $values){
$cell = '';
foreach ($values as $value)
$cell .= ",'" . esc_sql(trim($value)). "'";
$valuesStr .= ($valuesStr != '' ? ',' : '') . "(NULL{$cell})";
}
$this->_wpdb->query("INSERT IGNORE INTO {$this->_mainTable} VALUES {$valuesStr}");
}
?>
Below is the CSV:
product_sku,make,model,year_from,year_to,engine
63118,Toyota,FJ Cruiser,2000,2008,V6 4.7L 2UZ-FE 20R/22R 1st Gen FJ Cruiser
28216,Toyota,GX470,1992,1997,V8 4.7L 2UZ-FE GX470
62687,Toyota,Land Cruiser,1998,2007,V8 4.7L 2UZ-FE 100-Series
28485,Toyota,Land Cruiser,2007,2018,V8 5.7L 3UR-FE 200-Series
Incidentally, if I use this:
product_sku,make,model,year_from,year_to,engine
63118,Toyota,FJ Cruiser,2000,2008,engine1
28216,Toyota,GX470,1992,1997,engine2
62687,Toyota,Land Cruiser,1998,2007,engine3
28485,Toyota,Land Cruiser,2007,2018,engine4
everything gets inserted fine. I think the problem is with the last column because if I use the same values in the last column in the second or third columns, everything works fine.

Never mind, I've solved it. The problem was with the encoding of the CSV file. I changed it to UTF-8 and everything is working fine.

Related

Skip first row when overwriting the CSV file in PHP

I need help with skipping the first row when i overwrite the file. Because i get the date over the first row where i want to put a headers. Like date name etc .
I tried with continue; , but its not working .
$result =[];
foreach($sumArray as $key => $totalItems) {
$result[$totalItems][ ]= $sumArray1[$key];
$timeNeeded = implode(" ", $result[$totalItems]);
list($id1, $name1) = explode('_', $key);
$rows = 0;
$procenti = round(($perMin*$totalItems)/($timeNeeded/$precentEff),2);
$procentiCon = $procenti . " %";
$pickingEffRes = [$name1 , $totalItems, $procentiCon];
if($rows == 1) continue;
fputcsv($out, $pickingEffRes);
$rows++;
}
$result =[];
$rows = 0;
foreach($sumArray as $key => $totalItems) {
$result[$totalItems][ ]= $sumArray1[$key];
$timeNeeded = implode(" ", $result[$totalItems]);
list($id1, $name1) = explode('_', $key);
$procenti = round(($perMin*$totalItems)/($timeNeeded/$precentEff),2);
$procentiCon = $procenti . " %";
$pickingEffRes = [$name1 , $totalItems, $procentiCon];
if($rows == 1) continue;
fputcsv($out, $pickingEffRes);
$rows++;
}

PHP function - How to take three fields and perform and action when the first, 2nd and last field are the same but on different rows of a file

I have file that is delimetered by a comma like below
Big2,red,0,Y
Big2,blue,0,N
Big2,green,1,N
Big6,red,0,Y
Big6,blue,0,N
Big6,green,1,N
Big6,yellow,0,Y
Big6,black,0,Y
Following is the code I used to read the file
$file_content = explode("\n",(file_get_contents("inven.csv")));
foreach ($file_content as $key => $value) {
$value = str_replace(array('\'', '"'), '', $value);
$line_records = explode(',', $value);
$reference = trim($line_records[0]);
$option = trim($line_records[1]);
$quantity = trim($line_records[2]);
$discontinued = trim($line_records[3]);
}
I want to check for the following condition and perform some action
If the 1st, 2nd and 4th field are the same for all lines
If all "Big2" rows have the 3rd field = 0 and the 4th field = Y
Hope this solves your problem
$references = $options =$quantities = $status = array();
$file_content = explode("\n",(file_get_contents("inven.csv")));
foreach ($file_content as $key => $value) {
$value = str_replace(array('\'', '"'), '', $value);
$line_records = explode(',', $value);
$references[] = trim($line_records[0]);
echo end($references);
$options[] = trim($line_records[1]);
echo end($options);
$quantities[] = trim($line_records[2]);
echo end($quantities);
$status[] = trim($line_records[3]);
echo end($status);
}
$flag = true;
foreach(array_combine($references, $quantities, $status) as $ref => $quantity => $stat) { // foreach line throws and error
if($ref == 'Big2') {
if($quantity != 0 || $stat != 'Y'){
$flag = false;
break;
}
}
}
if (count(array_unique($references)) === 1 && count(array_unique($options))==1 && count(array_unique($status))==1) {
//Do action if the 1st, 2nd and 4th field are the same for all lines
}
if ($flag) {
//Do action if all "Big2" rows have the 3rd field = 0 and the 4th field = Y
}

Upload excel file in MYSQL using PHP

I want to upload an Excel file into a MySQL table without using PDO, but just with a simple MySQL connection.
I want to load the Excel file using DOMDocument::load but can't get it to work.
Here's what I've tried so far:
mysql_connection("localhost", "root", "");
mysql_select_db("excel");
function add_data($abc, $pqr, $xyz) {
$str = "INSERT INTO excel(abc, pqr, xyz) VALUES('$abc', '$pqr', '$xyz')";
$qry = mysql_query($str);
}
if (isset($_FILES['file'])) {
$dom = DOMDocument::load($_FILES['file']['tmp_name']);
$rows = $dom->getElementsByTagName('Row');
$first_row = true;
foreach ($rows as $row) {
if (!$first_row) {
$abc = "";
$pqr = "";
$xyz = "";
$index = 1;
$cells = $row->getElementsByTagName('Cell');
foreach ($cells as $cell) {
$ind = $cell->getAttribute('Index');
if ($ind != null)
$index = $ind;
if ($index == 1)
$abc = $cell->nodeValue;
if ($index == 2)
$pqr = $cell->nodeValue;
if ($index == 3)
$xyz = $cell->nodeValue;
$index += 1;
}
add_data($abc, $pqr, $xyz);
}
$first_row = false;
}
}

How do I put this array into csv or excel?

I can't figure out a way to do this and I really bad with working with arrays so I hope someone can help me.
I have array $stuck that just contains names, like this
$stuck = array('Daniel','Alex','alfredo','dina');
I am using sort to put the names in alphabetical order, like this
sort($stuck);
Now the thing is I want to put this array into a csv or excel file so the first letter of name will be the title and all the names with this letter will be under this title.
This is an example of what I am trying to accomplish
Here try this. I added comments between the code.
$csvArray = array();
$nameArray = array('Daniel','Alex','alfredo','dina');
//Create an array that can be used for creating the CSV
foreach($nameArray as $name)
{
$firstLetter = strtoupper(substr($name, 0,1));
$csvArray[$firstLetter][] = $name;
}
//Determine the subarray which have the must rules
$maxRuleCount = 0;
foreach($csvArray as $firstLetter => $nameArray)
{
if(count($nameArray) > $maxRuleCount)
{
$maxRuleCount = count($nameArray);
}
}
//Create the first rule (letters)
$csvContent = implode(';', array_keys($csvArray));
$csvContent .= "\r\n";
//Loop through the CSV array and create rules of the names
for($i = 0 ; $i < $maxRuleCount ; $i++)
{
foreach($csvArray as $firstLetter => $nameArray)
{
$csvContent .= #$csvArray[$firstLetter][$i].';';
}
$csvContent .= "\r\n";
}
//The hole csv content is in the $csvContent variable now
//If you want to print it you need to add the text/plain header because of the \r\n new line characters
header("Content-Type:text/plain");
echo $csvContent;
Ouput is:
D;A
Daniel;Alex;
dina;alfredo;
<?php
$stuck = array('Daniel','Alex','Dina','durban','eigor','alfredo','dina');
// associate names with first letters in a new array
foreach ($stuck as $name) {
$first_letter = strtoupper( substr( $name, 0, 1 ) );
$new_name_array[ $first_letter ][] = $name;
}
// store the first letter list in an array and sort it
$first_letters = array_keys( $new_name_array );
sort( $first_letters );
// sort the name lists
foreach( array_keys( $new_name_array ) as $letter ) {
sort( $new_name_array[$letter] );
}
// output csv header row
echo "'".implode( "','", $first_letters )."'\n";
// output the CSV name rows
$i = 0;
while ( true ) {
$row = array();
$is_row = false;
foreach( $first_letters as $letter ) {
$row[] = $new_name_array[ $letter ][ $i ];
if( ! empty( $new_name_array[ $letter ][ $i ] ) ) $is_row = true;
}
if( ! $is_row ) exit;
echo "'" . implode( "','", $row ) . "'\n";
$i++;
}
?>
Outputs quoted CSV:
'A','D','E'
'Alex','Daniel','eigor'
'alfredo','Dina',''
'','dina',''
'','durban',''
<?php
$stuck = array('Daniel','Alex','alfredo','dina', 'eigor', 'durban');
sort($stuck);
$sortedNames = array();
$maxEl = 0;
foreach ($stuck as $name) {
$name = strtolower($name);
$sortedNames[$name{0}][] = $name;
$maxEl = count($sortedNames[$name{0}]) < $maxEl ? $maxEl : count($sortedNames[$name{0}]);
}
$headers = array_keys($sortedNames);
$finalArray = array($headers);
for($i = 0; $i < $maxEl; $i++) {
$tmpRow = array();
foreach ($sortedNames as $names) {
$tmpRow[] = isset($names[$i]) ? $names[$i] : '';
}
$finalArray[] = $tmpRow;
}
$file = fopen("names.csv","w");
foreach ($finalArray as $line)
{
fputcsv($file,$line);
}
fclose($file);
Here is my attempt at this question:
$names = array('Daniel','Alex','alfredo','dina', 'eigor', 'falfoul', 'fiona');
natcasesort($names);
$columns = array();
foreach ($names as $name) {
$first = strtoupper(substr($name, 0, 1));
$columns[$first][] = $name;
}
// pad the columns so they all have the same number of rows
$maxRows = 0;
foreach ($columns as &$col) {
$numRows = count($col);
if ( $numRows > $maxRows) {
$maxRows = $numRows;
}
}
// pad the columns
foreach ($columns as &$column) {
$column = array_pad($column, $maxRows, '');
}
$firstLetter = array_keys($columns);
$numCols = count($firstLetter);
// transpose the columns array
$rows = array();
foreach ($columns as $csvCol) {
foreach ($csvCol as $k=>$n) {
$rows[$k][] = $n;
}
}
// pad the rows to ensure they all have the same number of
// columns
foreach ($rows as &$row) {
$row = array_pad($row, $numCols, '');
}
// add the title row to the rows
array_unshift($rows, $firstLetter);
$handle = fopen("names.csv", 'w');
foreach ($rows as $fRow) {
fputcsv($handle, $fRow);
}

need help with php loop logic

This code works perfectly except that it doesn't print the last 2 rows of my csv file:
This is the file:
603629,0,ATLV0008,"Vendor1",1942.60,11/04/2010,1942.60,9/1-9/30/10,EFT-JP
603627,2,ATLV0008,"Vendor1",1242.40,11/04/2010,1242.40,7/1-7/31/10,EFT-JP
600023,0,FLD4V0003,"Vendor2",1950.00,06/15/2010,1950.00,6/14/10 Request,EFT-JP
600024,0,FLD4V0003,"Vendor2",1800.00,06/15/2010,1800.00,6/14/10 Request,EFT-JP
603631,0,ATLV5066,"Vendor2",1000.00,11/09/2010,1000.00,11/4/10 Check Request,PM2
603647,0,ATLV5027,"DVendor3",2799.80,11/15/2010,2799.80,10/1-10/31/10 Bishop,PM2
603642,5,ATLV5027,"Vendor3",482.40,11/15/2010,482.40,10/1-10/18/10 Allen,PM2
603653,0,ATLV0403,"Vendor4",931.21,11/17/2010,931.21,9/1-9/30/10,EFT-JP
603661,0,ATLV0105,"Vendor5",26.75,11/19/2010,26.75,093139,PM2
603660,1,ATLV0105,"Vendor5",5.35,11/19/2010,5.35,093472,PM2
Here is the code: (It needs to display the sum of 2 rows with the same vendor before the actual rows)
if (($handle = fopen('upload/ATLANTA.csv', "r")) !== FALSE) {
while (($row_array = fgetcsv($handle, 1000, ","))) {
foreach ($row_array as $key => $val) {
$row_array[$key] = trim(str_replace('"', '', $val));
ob_flush();
}
$complete[] = $row_array;
ob_flush();
}
}
flush();
$prevVendor = '';
$sum = 0;
$venData = '';
if (isset($complete)) {
foreach ($complete as $key => $val) {
$venData .= "<br/>";
$currVendor = $complete[$key][3];
if ($currVendor != $prevVendor){
if ($prevVendor != NULL){
print "<br/>";
print $sum . "<br/>";
print $venData;
$sum = 0; $venData = '';
}
}
foreach ($val as $ikey => $ival){
if ($ikey != 1){
$venData .= $ival . '&nbsp';
$prevVendor = $val[3];
}
}
if ($currVendor == $prevVendor){
$sum += $complete[$key][6];
}
}
}
I don't really understand your problem but if you want to get (and save it wherever you want) the sum of each vendors, you should do something like this :
$prevVendor = null;
$venData = '';
$sums = array();
if (isset($complete) && is_array($complete)) {
$lim = count($complete);
for ($key=0;$key<$lim;++$key) { // no need foreach for simple array
$venData .= "<br/>";
$currVendor = $complete[$key][3];
if ($currVendor != $prevVendor){
if ($prevVendor != null){
print "<br/>";
print $venData;
}
$venData = '';
$prevVendor = $currVendor;
$sums[$currVendor] = 0; // set the counter to 0
}
foreach ($complete[$key] as $ikey => $ival){
if ($ikey != 1){
$venData .= $ival . ' '; // is useful for you I guess
}
}
$sums[$currVendor] += $complete[$key][6]; // add amounts
}
}
if ($prevVendor != null){ // you need to do this to display the last record
print "<br/>";
print $venData;
}
var_export($sums); // check results
This can be some syntax errors ...

Categories