使用CakePHP将CSV导入数据库

使用CakePHP将CSV导入数据库,php,cakephp,csv,Php,Cakephp,Csv,我有点小问题。我已经尝试了许多不同的方法,但没有找到正确的解决方案。也许伙计们,你们能帮我吗?如果我尝试导入一个数组,我会遇到一些错误 数据库名称:kontaktid 标签名称:kontaktids 以下是我在KontaktidController.php中的函数: 这是我的模型文件Kontaktid.php: 这里是数组: array( 'messages' => array( (int) 0 => 'Post for Row 1 was saved.',

我有点小问题。我已经尝试了许多不同的方法,但没有找到正确的解决方案。也许伙计们,你们能帮我吗?如果我尝试导入一个数组,我会遇到一些错误

数据库名称:kontaktid

标签名称:kontaktids

以下是我在KontaktidController.php中的函数:

这是我的模型文件Kontaktid.php:

这里是数组:

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.'
    )
)
我尝试了许多不同的方法将csv导入数据库,但我没有找到任何有效的解决方案。有人能给我一些线索或解决办法吗,怎样做才对


感谢您提供的任何线索或解决方案。

您尚未定义变量,这就是它抛出错误的原因

$i未在while循环之外定义,并且不可用于计算或增量

$error也是如此,因为它不存在,所以无法对其进行计算。您需要先定义它们并为它们赋值,然后才能执行任何操作。

Ye,我添加了这些$I=null$error=null;,现在它没有抛出错误,但这没有帮助,它仍然无所作为。只给我这个数组。
<?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;

    }

}
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]
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.'
    )
)