- 浏览: 13641327 次
- 性别:
- 来自: 洛杉矶
文章分类
- 全部博客 (1994)
- Php / Pear / Mysql / Node.js (378)
- Javascript /Jquery / Bootstrap / Web (435)
- Phone / IOS / Objective-C / Swift (137)
- Ubuntu / Mac / Github / Aptana / Nginx / Shell / Linux (335)
- Perl / Koha / Ruby / Markdown (8)
- Java / Jsp (12)
- Python 2 / Wxpython (25)
- Codeigniter / CakePHP (32)
- Div / Css / XML / HTML5 (179)
- WP / Joomla! / Magento / Shopify / Drupal / Moodle / Zimbra (275)
- Apache / VPN / Software (31)
- AS3.0/2.0 / Flex / Flash (45)
- Smarty (6)
- SEO (24)
- Google / Facebook / Pinterest / SNS (80)
- Tools (22)
最新评论
-
1455975567:
xuezhongyu01 写道wocan23 写道我想问下那个 ...
Mysql: LBS实现查找附近的人 (两经纬度之间的距离) -
xuezhongyu01:
wocan23 写道我想问下那个111.1是怎么得来的我也看不 ...
Mysql: LBS实现查找附近的人 (两经纬度之间的距离) -
18335864773:
试试 pageoffice 在线打开 PDF 文件吧. pag ...
jquery在线预览PDF文件,打开PDF文件 -
青春依旧:
opacity: 0.5; 个人喜欢这种方式!关于其他css特 ...
css透明度的设置 (兼容所有浏览器) -
July01:
推荐用StratoIO打印控件,浏览器和系统的兼容性都很好,而 ...
搞定网页打印自动分页问题
A Comma separated values (CSV) file is a computer data file used for implementing the tried and true organizational tool, the Comma Separated List. The CSV file is used for the digital storage of data structured in a table of lists form, where each associated item (member) in a group is in association with others also separated by the commas of its set. Each line in the CSV file corresponds to a row in the table. Within a line, fields are separated by commas, each field belonging to one table column. Read more about this file format here .
There is a handy library available in CodeIgniter framework which is the CSVReader that will makes reading or parsing CSV formatted data easily.
In this article, I try to show you how to use it. But since this
library is not included in CodeIgniter package you need to add this to
your system/libraries
directory.
Create a new file in your system/libraries
directory named it to csvreader.php
. Go here
and download the code.
The CSVReader Class
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); /** * CSVReader Class * * $Id: csvreader.php 147 2007-07-09 23:12:45Z Pierre-Jean $ * * Allows to retrieve a CSV file content as a two dimensional array. * The first text line shall contains the column names. * * @author Pierre-Jean Turpeau * @link http://www.codeigniter.com/wiki/CSVReader */ class CSVReader { var $fields; /** columns names retrieved after parsing */ var $separator = ','; /** separator used to explode each line */ /** * Parse a text containing CSV formatted data. * * @access public * @param string * @return array */ function parse_text($p_Text) { $lines = explode("\n", $p_Text); return $this->parse_lines($lines); } /** * Parse a file containing CSV formatted data. * * @access public * @param string * @return array */ function parse_file($p_Filepath) { $lines = file($p_Filepath); return $this->parse_lines($lines); } /** * Parse an array of text lines containing CSV formatted data. * * @access public * @param array * @return array */ function parse_lines($p_CSVLines) { $content = FALSE; foreach( $p_CSVLines as $line_num => $line ) { if( $line != '' ) { // skip empty lines $elements = split($this->separator, $line); if( !is_array($content) ) { // the first line contains fields names $this->fields = $elements; $content = array(); } else { $item = array(); foreach( $this->fields as $id => $field ) { if( isset($elements[$id]) ) { $item[$field] = $elements[$id]; } } $content[] = $item; } } } return $content; } }
Then supposing we have a CSV file that contains data like this.
Id,Name,Category,Price 1,iPhone,Mobile,300, 2,iMac,Desktop,529, 3,MacBook,Mobile,2000, 4,iTouch,Gadgets,157, 5,Wii,Gaming,1250,
What is our aim here is read the data form the CSV file then present it in a tabular form in an html table.
Supposing you have a clean install CI in your development server. Go tosystem/application/controller
folder and open the welcome.php
file and add the function below.
function index() { $this->load->library('csvreader'); $filePath = './csv/products.csv'; $data['csvData'] = $this->csvreader->parse_file($filePath); $this->load->view('csv_view', $data); }
Next open system/application/views
create a new file name it csv_view.php
and populate the code below.
<table cellpadding="0" cellspacing="0"> <thead> <th> <td>PRODUCT ID</td> <td>PRODUCT NAME</td> <td>CATEGORY</td> <td>PRICE</td> </th> </thead> <tbody> <?php foreach($csvData as $field){?> <tr> <td><?=$field['id']?></td> <td><?=$field['name']?></td> <td><?=$field['category']?></td> <td><?=$field['price']?></td> </tr> <?php }?> </tbody> </table>
Thats how easy reading CSV data using CI. Add some styling and you can have like this.
After writing this short tutorial, I realized that this may not be helpful in any way. I dont know why I came up with that stupid thought. Maybe for the reason CSV is quite old data format? And maybe no one is using this right now? hmmm tell me what you think about it. Anyway I still posting it hoping it will help someone.
发表评论
-
CakePHP你必须知道的21条技巧
2012-10-19 06:25 1850原文链接:http://www.avatarfinancial ... -
cakephp 1.3 Views
2012-10-09 06:12 1409Section 1 Views 视图 一个vie ... -
cakephp 1.3 Models
2012-10-09 06:07 2475Section 1 What is a model? ... -
cakephp 1.3 Controller
2012-10-09 05:49 3300Controller 一个controller用于管理 ... -
cakephp 1.3 配置CakePHP
2012-10-09 05:31 4603Section 1 数据库配置 app/config/ ... -
CakePHP 2.x十分钟博客教程
2012-10-07 01:27 243691. CakePHP2十分钟博客教 ... -
Create an Admin panel with CodeIgniter
2010-05-23 02:15 4144Create an Admin panel with Code ... -
Codeigniter Grid 使用方法 (flexigrid)
2010-05-23 02:05 2766来源:http://codeigniter.com/forum ... -
CI集成 ckeditor 配置
2010-05-23 01:34 3723配置 ckeditor 1.上传 下载 ckedito ... -
codeigniter 辅助函数 - 敏感词过滤
2010-05-05 06:18 4544我们都知道有些敏感的词汇是不适合出现在互联网上的,特别是在有用 ... -
实现简单 codeigniter 缓存 (cache)
2010-04-30 23:47 5236代码 class Test extends Contr ... -
CKEditor Helper for CodeIgniter
2010-04-19 00:37 3943Using CKEditor as a plugin in y ... -
codeigniter 生成 excel
2010-04-19 00:33 3288Excel Plugin Th ... -
CakePHP 中文手册
2010-04-14 21:04 2310基本概念 Section1 简介 ... -
利用 Cache_Lite代替codeigniter中的cache功能
2010-01-29 06:15 5473codeigniter的功能纵然强大,也有不足之处。其cach ... -
CodeIgniter 操作PDF ( Generating PDF files using CodeIgniter )
2010-01-03 04:03 3572PDF files rock! Some of the p ... -
CodeIgniter 合作 Authorize.net
2009-12-30 00:25 1560function payment(){ // 略... ... -
CodeIgniter 合作paypal
2009-12-30 00:15 2317<?php class Paypal extend ... -
codeigniter 操作 Rss
2009-12-29 07:12 1927I wrote a codeigniter library t ... -
codeigniter操作xml
2009-12-29 06:57 3957This Simplexml class provides a ...
相关推荐
NULL 博文链接:https://justcoding.iteye.com/blog/561475
CodeIgniter_2.1.2 中文操作手冊
在不到一个小时的时间里,您安装并配置了 CodeIgniter,创建了一个包含主页、将信息添加到数据库...可以添加在数据库插入操作结束后发送电子邮件通知的功能。 目前,您只是掌握了开始使用 CodeIgniter 所需的一些内容。
PHP框架之CodeIgniter留言板实例PHP框架之CodeIgniter留言板实例PHP框架之CodeIgniter留言板实例PHP框架之CodeIgniter留言板实例PHP框架之CodeIgniter留言板实例PHP框架之CodeIgniter留言板实例PHP框架之CodeIgniter...
CodeIgniter最新版本,开源、免费、轻量级的web编程,基于MVC的编程架构能大大提高php编码生产力。
NULL 博文链接:https://justcoding.iteye.com/blog/558776
codeigniter 代码实例分享,基于jquery1.4,codeigniter,eclipse 开发
CodeIgniter框架
敏捷php开发框架CodeIgniter 1.7.1中文手册
CodeIgniter_2.1.4.zip
CodeIgniter 架构 教程. CodeIgniter 架构的使用说明教程.
codeigniter中文文档codeigniter中文文档codeigniter中文文档codeigniter中文文档codeigniter中文文档
codeigniter分页
CodeIgniter最新版 CodeIgniter1.72版 帮助文档 帮助文件
CodeIgniter_2.0.0.zip CodeIgniter_2.0.0.zip CodeIgniter_2.0.0.zip
研究了半天的codeigniter,基本把结构弄清楚了。
codeigniter-oauth2-server, 用于CodeIgniter的OAuth2.0 服务器 OAuth2服务器,服务器,OAuth2.0 服务器的一个CodeIgniter包,对于 OAuth2.0 客户端:https://github.com/philsturgeon/codeigniter-oauth2对于 OAuth
codeigniter(CI)中文手册下载
CodeIgniter1.7敏捷框架开发最新出版书(EN)