api - Codeigniter REST CSV 导入到 mysql

标签 api codeigniter rest codeigniter-2

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

最佳答案

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

这是我的 csv 阅读器库,将它保存在库文件夹中作为 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;
    }   
}
?>

和 Controller 方法
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);  
}

这是 View
<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>

注意:这只会读取服务器上存在的文件。如果需要上传文件,请使用 File Upload Class上传文件并将其保存到服务器上的某个位置,然后在 parse_file 中给出定位文件的路径方法。一切都会好起来的。

关于api - Codeigniter REST CSV 导入到 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11337221/

相关文章:

mysql - Codeigniter 不会在数据库中保存 session

到 "pay for product"的 RESTful 方式

web-services - 处理用户特定数据时的 Rest api 路由名称

python - Robot Framework 测试脚本因 SSLError 而失败

php - Web 服务 - REST 与 PHP JSON RPC

php - codeigniter INSERT 仅插入变量名称

codeigniter - 如何在 Codeigniter 框架中使用 form_input() 函数放置 HTML5 必填字段

javascript - Angular/Ionic 通过 REST 服务在导入的 HTML 中隐藏数据属性(data-***)

php - 如何在php中为json api实现缓存系统

java - 有人用过pubchemdb吗?有类似的API吗?