Codeigniter REST CSV 导入到 mysql [英] Codeigniter REST CSV import to mysql

查看:42
本文介绍了Codeigniter REST CSV 导入到 mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 API 在我的控制器上发布 csv 文件.我正在使用 phil sturgeon 的 Codeigniter REST 库.如何在客户端实现将 CSV 导入我的 REST 服务器.我只是想问一下,因为我找不到任何关于它的文档.

I want to post csv file on my controller using API.I'm using Codeigniter REST Library by phil sturgeon. How to implement on the client side the importing of CSV to my REST Server.I just want to ask because I can't find any documentation about it.

推荐答案

这是一个简单的方法.我不知道人们在做什么,但我使用这个

Here is an easy way to do this. I don't know what people do but i use this

这是我的 csv 阅读器库,将其保存在库文件夹中为 csvreader.php.

This is my csv reader library, Save this in libraries folder as csvreader.php.

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class CSVReader {

    var $fields;            /** columns names retrieved after parsing */ 
    var $separator  =   ';';    /** separator used to explode each line */
    var $enclosure  =   '"';    /** enclosure used to decorate each field */

    var $max_row_size   =   4096;    /** maximum row size to be used for decoding */

    function parse_file($p_Filepath) 
    {
        $file           =   fopen($p_Filepath, 'r');
        $this->fields   =   fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure);
        $keys_values        =   explode(',',$this->fields[0]);

        $content            =   array();
        $keys           =   $this->escape_string($keys_values);

        $i  =   1;
        while(($row = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure)) != false ) 
        {
            if( $row != null ) { // skip empty lines
                $values         =   explode(',',$row[0]);
                if(count($keys) == count($values)){
                    $arr            =   array();
                    $new_values =   array();
                    $new_values =   $this->escape_string($values);
                    for($j=0;$j<count($keys);$j++){
                        if($keys[$j]    !=  ""){
                            $arr[$keys[$j]] =   $new_values[$j];
                        }
                    }
                    $content[$i]    =   $arr;
                    $i++;
                }
            }
        }
        fclose($file);
        return $content;
    }

    function escape_string($data)
    {
        $result =   array();
        foreach($data as $row){
            $result[]   =   str_replace('"', '',$row);
        }
        return $result;
    }   
}
?>

和控制器方法

function readExcel()
{
        $this->load->library('csvreader');
        $result =   $this->csvreader->parse_file('Test.csv');//path to csv file

        $data['csvData'] =  $result;
        $this->load->view('view_csv', $data);  
}

这是视图

<table cellpadding="0" cellspacing="0" width="100%">
    <tr>
            <td width = "10%">ID</td>
            <td width = "20%">NAME</td>
            <td width = "20%">SHORT DESCRIPTION</td>
            <td width = "30%">LONG DESCRIPTION</td>
            <td width = "10%">STATUS</td>
            <td width = "10%">PARENTID</td>
    </tr>

            <?php foreach($csvData as $field){?>
                <tr>
                    <td><?php echo $field['id']?></td>
                    <td><?php echo $field['name']?></td>
                    <td><?php echo $field['shortdesc']?></td>
                    <td><?php echo $field['longdesc']?></td>
                    <td><?php echo $field['status']?></td>
                    <td><?php echo $field['parentid']?></td>
                </tr>
            <?php }?>
</table>

注意:这只会读取服务器上存在的文件.如果需要上传文件,使用文件上传类上传文件并保存到在您的服务器上的位置然后在 parse_file 方法中给出定位文件的路径.一切都会好起来的.

Note : This will only read a file which exists on server. If the file is needed to be uploaded use File Upload Class to upload file and save it to some location on your server then give the path of located file in parse_file method. And everything will work fine.

这篇关于Codeigniter REST CSV 导入到 mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆