I have a litte problem. I already tried many different ways, and I didnt find a right solution. Maybe guys, you can help me ? I got some errors if I try to import and a array.
Database name: kontaktid
Tabel name: kontaktids
Here is my function in KontaktidController.php
:
function import() {
$messages = $this->Kontaktid->import('export.csv');
debug($messages);
}
And here is my Model file Kontaktid.php
:
<?php
class Kontaktid extends AppModel {
public $validate = array(
'title' => array(
'rule' => 'notEmpty'
),
'body' => array(
'rule' => 'notEmpty'
)
);
var $name = 'Kontaktid';
function import($filename) {
// to avoid having to tweak the contents of
// $data you should use your db field name as the heading name
// eg: Post.id, Post.title, Post.description
// set the filename to read CSV from
$filename = TMP . 'uploads' . DS . 'Kontaktid' . DS . $filename;
// open the file
$handle = fopen($filename, "r");
// read the 1st row as headings
$header = fgetcsv($handle);
// create a message container
$return = array(
'messages' => array(),
'errors' => array(),
);
// read each data row in the file
while (($row = fgetcsv($handle)) !== FALSE) {
$i++; /*This is line 38*/
$data = array();
// for each header field
foreach ($header as $k=>$head) {
// get the data field from Model.field
if (strpos($head,'.')!==false) {
$h = explode('.',$head);
$data[$h[0]][$h[1]]=(isset($row[$k])) ? $row[$k] : '';
}
// get the data field from field
else {
$data['kontaktid'][$head]=(isset($row[$k])) ? $row[$k] : '';
}
}
// see if we have an id
$id = isset($data['kontaktid']['id']) ? $data['kontaktid']['id'] : 0;
// we have an id, so we update
if ($id) {
// there is 2 options here,
// option 1:
// load the current row, and merge it with the new data
//$this->recursive = -1;
//$post = $this->read(null,$id);
//$data['Post'] = array_merge($post['Post'],$data['Post']);
// option 2:
// set the model id
$this->id = $id;
}
// or create a new record
else {
$this->create();
}
// see what we have
// debug($data);
// validate the row
$this->set($data);
if (!$this->validates()) {
$this->_flash('warning');
$return['errors'][] = __(sprintf('Post for Row %d failed to validate.',$i), true);
}
// save the row
if (!$error && !$this->save($data)) { /*This is line 88*/
$return['errors'][] = __(sprintf('Post for Row %d failed to save.',$i), true);
}
// success message!
if (!$error) { /*This is line 93*/
$return['messages'][] = __(sprintf('Post for Row %d was saved.',$i), true);
}
}
// close the file
fclose($handle);
// return the messages
return $return;
}
}
Here is errors what I got:
Notice (8): Undefined variable: i [APP\Model\Kontaktid.php, line 38]
Notice (8): Undefined variable: error [APP\Model\Kontaktid.php, line 88]
Notice (8): Undefined variable: error [APP\Model\Kontaktid.php, line 93]
And here is array:
array(
'messages' => array(
(int) 0 => 'Post for Row 1 was saved.',
(int) 1 => 'Post for Row 2 was saved.',
(int) 2 => 'Post for Row 3 was saved.',
(int) 3 => 'Post for Row 4 was saved.',
(int) 4 => 'Post for Row 5 was saved.',
(int) 5 => 'Post for Row 6 was saved.',
(int) 6 => 'Post for Row 7 was saved.',
(int) 7 => 'Post for Row 8 was saved.',
(int) 8 => 'Post for Row 9 was saved.',
(int) 9 => 'Post for Row 10 was saved.',
(int) 10 => 'Post for Row 11 was saved.'
),
'errors' => array(
(int) 0 => 'Post for Row 1 failed to save.',
(int) 1 => 'Post for Row 2 failed to save.',
(int) 2 => 'Post for Row 3 failed to save.',
(int) 3 => 'Post for Row 4 failed to save.',
(int) 4 => 'Post for Row 5 failed to save.',
(int) 5 => 'Post for Row 6 failed to save.',
(int) 6 => 'Post for Row 7 failed to save.',
(int) 7 => 'Post for Row 8 failed to save.',
(int) 8 => 'Post for Row 9 failed to save.',
(int) 9 => 'Post for Row 10 failed to save.',
(int) 10 => 'Post for Row 11 failed to save.'
)
)
I was tried many different ways how to import csv into database, but I didnt found any solution what is working. Can somebody give me clue or some solution, how to do it right.
Thank you for any clue or solution.
You've not defined the variables, that's why its throwing errors.
$i
is not defined outside of the while loop, and is not available for evaluation or incrementation.
Same goes for $error
It doesn't exist so it cannot be evaluated. You need to define them and assign values to them before you can do anything.