php - import data from csv to mysql using cakephp
Get the solution ↓↓↓Solution:
This line here is in-correct
$data[$h[0]][$h[1]][$h[2]]=(isset($row[$k])) ? $row[$k] : '';
$h[2]
will never be defined.
When you explode a header name like "sc_widths.chainage" (with '.' as separator) you will get
$h[0] = 'sc_widths';
$h[1] = 'chainage';
You will never get a $h[2] with that data.
So the fix for your particular problem is to drop the $h[2] like this:
$data[$h[0]][$h[1]]=(isset($row[$k])) ? $row[$k] : '';
Answer
Solution:
Here is working code.
function import($filename) {
$i = null; $error = null;
$filename = $_SERVER['DOCUMENT_ROOT'] . '/dashboards/app/webroot/files/' .$filename;
$handle = fopen($filename, "r");
$header = fgetcsv($handle);
$return = array(
//'messages' => array(),
'errors' => array(),
);
while (($row = fgetcsv($handle)) !== FALSE) {
$i++;
$data = array();
foreach ($header as $k=>$head) {
if (strpos($head,'.')!==false) {
$h = explode('.',$head);
$data[$h[0]][$h[1]]=(isset($row[$k])) ? $row[$k] : '';
}
else
{
$data['Project'][$head]=(isset($row[$k])) ? $row[$k] : '';
}
}
$id = isset($row[0]) ? $row[0] : 0;
if (!empty($id)) {
$projects = $this->find('all', array('conditions' => array('Project.item_sku' =>$id)));
if (!empty($projects)){
$apiConfig = (isset($projects[0]['Project']) && is_array($projects[0]['Project'])) ? ($projects[0]['Project']) : array();
//debug($apiConfig);
//debug($data['Project']);
$data['Project'] = array_merge($apiConfig,$data['Project']);
}else {
$this->id = $id;
}
}
else {
$this->create();
}
//debug($data);
$this->set($data);
if (!$this->validates()) {
//$this->_flash('warning');
//$errors = $this->ModelName->invalidFields();
if(!empty($this->validationErrors['item_name'])){
$limit = $this->validationErrors['item_name'] ;
$return['errors'][] = __(sprintf("Listing Skip Row %d failed to validates:--- $limit .",$i), true);
}
else if(!empty($this->validationErrors['brand_name'])){
$limit = $this->validationErrors['brand_name'] ;
$return['errors'][] = __(sprintf("Listing Skip Row %d failed to validates:--- $limit .",$i), true);
}
else if(!empty($this->validationErrors['manufacturer'])){
$limit = $this->validationErrors['manufacturer'] ;
$return['errors'][] = __(sprintf("Listing Skip Row %d failed to validates:--- $limit .",$i), true);
}
else if(!empty($this->validationErrors['feed_product_type'])){
$limit = $this->validationErrors['feed_product_type'] ;
$return['errors'][] = __(sprintf("Listing Skip Row %d failed to validates:--- $limit .",$i), true);
}
else if(!empty($this->validationErrors['product_description'])){
$limit = $this->validationErrors['product_description'] ;
$return['errors'][] = __(sprintf("Listing Skip Row %d failed to validates:--- $limit .",$i), true);
}
else if(!empty($this->validationErrors['bullet_point1'])){
$limit = $this->validationErrors['bullet_point1'] ;
$return['errors'][] = __(sprintf("Listing Skip Row %d failed to validates:--- $limit .",$i), true);
}
else if(!empty($this->validationErrors['bullet_point2'])){
$limit = $this->validationErrors['bullet_point2'] ;
$return['errors'][] = __(sprintf("Listing Skip Row %d failed to validates:--- $limit .",$i), true);
}
else if(!empty($this->validationErrors['bullet_point3'])){
$limit = $this->validationErrors['bullet_point3'] ;
$return['errors'][] = __(sprintf("Listing Skip Row %d failed to validates:--- $limit .",$i), true);
}
else if(!empty($this->validationErrors['bullet_point4'])){
$limit = $this->validationErrors['bullet_point4'] ;
$return['errors'][] = __(sprintf("Listing Skip Row %d failed to validates:--- $limit .",$i), true);
}
else if(!empty($this->validationErrors['bullet_point5'])){
$limit = $this->validationErrors['bullet_point5'] ;
$return['errors'][] = __(sprintf("Listing Skip Row %d failed to validates:--- $limit .",$i), true);
}
else if(!empty($this->validationErrors['quantity'])){
$limit = $this->validationErrors['quantity'] ;
$return['errors'][] = __(sprintf("Listing Skip Row %d failed to validates:--- $limit .",$i), true);
}
else { echo "Welcome Amit....";}
}
if ($this->saveAll($data)) {
// $return['errors'][] = __(sprintf("Listing Skip Row %d failed to save.",$i), true);
}/*else {
$return['messages'][] = __(sprintf('Listing for Row %d was saved.',$i), true);
}*/
}
fclose($handle);
return $return;
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: composer detected issues in your platform: your composer dependencies require a php version ">= 7.3.0".
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.