Importing Data from a CSV

Hi guys

This is a bit of a complicated one ( im sure some people have clicked off already :slight_smile:

Ok. I have a CSV which I need to import. It has a id down the left and the usernames along the top then each cell has a yes if is a match

CSV Spreadsheet

ID | CHRIS | BOB | BILL
1 | yes
2
3 | yes

So what I want to do is basically get the results and list the cells that have the value yes

Chris 1
Chris 3

I know how to how to to read rows. But how can I match a cell with the value of the cell at the top of that column

many thanks in advance

So if Bill was 2 how would your csv file look?

like this

ID | CHRIS | BOB | BILL
1 | yes
2 | ________yes
3 | yes

sorry for the line but it will not let me put multiple spaces in

Can you upload an actual portion of this file somewhere? We’re going to need to see what characters are used to delimit the rows/columns/fields.

:slight_smile:

Anthony.

I have attached a zip with the spreadsheet in

That may take a while, can you find a host?

I have uploaded here

http://www.systembookingsdemo.com/ss.zip

:slight_smile:


<?php
error_reporting(-1);
ini_set('display_errors', true);

function get_csv($file){
  $csv = new SplFileObject($file);
  $csv->setFlags(SplFileObject::READ_CSV);
  return $csv;
}

$csv = get_csv('php/test.csv');

$users = $csv->current();

foreach($csv as $row){
  $goal = current($row);
  foreach($row as $index => $col){
    if('yes' !== $col){
      continue;
    }
    printf("%s attained Goal %d\
", $users[$index], $goal);
  }
}

/*
  Chris attained Goal 2
  Steve attained Goal 2
  Toby attained Goal 3
  Gemma attained Goal 4
*/

Thanks Ant

I am getting a error on line 9

Parse error: syntax error, unexpected ‘)’, expecting ‘(’ in /home/joiyoya/public_html/admin/testing.php on line 9

  $csv->setFlags(SplFileObject::READ_CSV);

Strange, looks good and works here. What PHP version are you running?

4.4.9

I’m sure this is why. Is there a work around?

Ouch, you don’t make it easy do you. :stuck_out_tongue:

Apologies :slight_smile:

Here you go. Trouble causer. :stuck_out_tongue:


<?php
error_reporting(-1);
ini_set('display_errors', true);

$csv = fopen('php/test.csv', 'r');

$users = fgetcsv($csv);

while($row = fgetcsv($csv)){
  $goal = current($row);
  foreach($row as $index => $col){
    if('yes' !== $col){
      continue;
    }
    printf("%s attained Goal %d\
", $users[$index], $goal);
  }
}

/*
  Chris attained Goal 2
  Steve attained Goal 2
  Toby attained Goal 3
  Gemma attained Goal 4
*/

Fantastic

Just one more thing. If i wanted to actually get the value of the cell how would I do this

say the cell value was “Maybe”

No worries.

The cell value is contained in $col; knock yourself out. :slight_smile:

Anthony.