Professional CodeIgniter

(singke) #1

Chapter 7: Improving the Dashboard


198


the following URL: http://www.codeigniter.com/wiki/CSVReader. Essentially, this library allows you to
parse either CSV text or CSV in a file (which naturally needs to be uploaded first or already be on the file
system) and return an array of data that can be uploaded into a database table. When you download it,
add it to the /system/application/libraries folder, which will keep the libraries for this particular
CodeIgniter application separate from the core libraries.

Before continuing, though, it might be helpful to cover some of the ground rules of using custom
libraries:

The filename you use must be capitalized. (The library you ’ re using is CSVReader.php.)

Class declarations must also be capitalized (e.g., class CSVReader() ).

Class names and filenames must match.

Custom libraries should be stored in /system/application/libraries to keep them separate from
the core CodeIgniter libraries.

Once you ’ ve installed a library, you can load it in the familiar way:

$this- > load- > library(‘CSVReader’);

Any methods can be called using arrow notation and the lowercase library name:

$this- > csvreader- > parseFile($path);

Please note that you will probably want to change the default separator from “ ; ” to “ , ” in this particular
library. If you don ’ t change it, the CSVReader library will try to split your data on the “ ; ” character.
Since you ’ re exporting comma - separated data, that wouldn ’ t work very well for you.

Now that you ’ ve installed a third - party library, it ’ s time to get to work. The best way to approach this
problem is to start with the admin_products_home view first. You ’ re going to add a very simple form to
the top of that view that will allow an administrator to upload a CSV file.

< h1 > < ?php echo $title;? > < /h1 >
< p > < ?php echo anchor(“admin/products/create”, “Create new product”);? >
| < ?php echo anchor(“admin/products/export”,”Export”);? > < /p >

< ?php
echo form_open_multipart(“admin/products/import”);
$data = array(‘name’ = > ‘csvfile’, ‘size’= > 15);
echo form_upload($data);
echo form_hidden(‘csvinit’,true);
echo form_submit(‘submit’,’IMPORT’);
echo form_close();
? >

The form should look something like Figure 7 - 6.




Free download pdf