My favorites | Sign in
Project Logo
                
Search
for

Simple data access object for csv files in php5.

by Kazuyoshi Tlacaelel.

Some features

Cells

  1. fillCell cell value filler
  2. getCell cell fetcher
  3. hasCell checks if a coordinate is valid

Headers

  1. countHeaders header counter
  2. createHeaders header creator
  3. getHeaders header fetcher
  4. setHeaders header injector

Columns

  1. appendColumn column appender
  2. fillColumn collumn data injector
  3. getColumn column fetcher
  4. hasColumn column existance checker
  5. removeColumn column remover
  6. walkColumn column walker

Must see

  1. __construct data load initialize
  2. connect header and row relationship builder
  3. getRawArray raw data as array
  4. isSymmetric data length/symmetry checker
  5. load csv file loader
  6. settings settings alterator
  7. symmetrize all rows length equalizer
  8. walkGrid grid walker

Rows

  1. appendRow row appender
  2. countRows row counter
  3. fillRow fillRow
  4. getAsymmetricRows asymmetric data fetcher
  5. getRow row fetcher
  6. getRows multiple row fetcher
  7. hasRow row existance checker
  8. removeRow row remover
  9. walkRow row walker
Updated Dec 18, 2008 by kazu....@gmail.com
setHeaders  

Method:setHeaders

header injector

uses a $list of values which wil be used to replace current headers.

Note: that given $list must match the length of all rows. known as symmetric. see isSymmetric() and getAsymmetricRows() methods

Also, that current headers will be used as first row of data and consecuently all rows order will change with this action.

sample of a csv file "my_cool.csv"

name,age,skill
john,13,knows magic
tanaka,8,makes sushi
jose,5,dances salsa

load the library and csv file

require_once 'File/CSV/DataSource.php';
$csv = new File_CSV_DataSource;
$csv->load('my_cool.csv');

lets dump currently loaded data

var_export($csv->connect());

output

array (
0 =>
array (
'name' => 'john',
'age' => '13',
'skill' => 'knows magic',
),
1 =>
array (
'name' => 'tanaka',
'age' => '8',
'skill' => 'makes sushi',
),
2 =>
array (
'name' => 'jose',
'age' => '5',
'skill' => 'dances salsa',
),
)

And now lets create a new set of headers and attempt to inject them into the current loaded dataset

$new_headers = array('a', 'b', 'c');
var_export($csv->setHeaders($new_headers));

output

true

Now lets try the same with some headers that do not match the current headers length. (this should fail)

$new_headers = array('a', 'b');
var_export($csv->setHeaders($new_headers));

output

false

now let's dump whatever we have changed

var_export($csv->connect());

output

array (
0 =>
array (
'a' => 'name',
'b' => 'age',
'c' => 'skill',
),
1 =>
array (
'a' => 'john',
'b' => '13',
'c' => 'knows magic',
),
2 =>
array (
'a' => 'tanaka',
'b' => '8',
'c' => 'makes sushi',
),
3 =>
array (
'a' => 'jose',
'b' => '5',
'c' => 'dances salsa',
),
)
  1. Argument array $list a collection of names to use as headers,

Sign in to add a comment
Hosted by Google Code