0

I am Sharam, current exchange student in Europe from a after school club tackling politics & issues concerning the youth of Southeastern Asia. In our group we have new volunteers around the world every month and we have setup central server to categorise & prioritise tasks the club must have performed in the future to have meaning in our country. I have little knowledge in PHP however I am eager to learn, here is my problem...

Because SQL it's too complex we use CSV. Everybody can edit master CSV & add tasks to it like here:

Priority,Task,Person
High,Campus flyers,Ashley
High,Test videocall with teacher,Sharam
Low,New Website,Martin & Aneet
Medium,Auditorium recruiting,Martin & Aneet & Ashley
Low,Garbage collecting,Sharam & Ashley
High,Ask permission to reserve classroom Sat & Sun,Sharam

As you see, priority of item is high, medium, low in any order. I tried array_merge_recursive() but maybe I am doing it wrong:

<?php

$file = @fopen("tasks-list.csv", "r") or exit("error, check server status");

$array = array();
$header = fgetcsv($file, 1000, ","); //don't need header in array

while (!feof($file)) {
  $data = fgetcsv($file, 1000, ",");
  if (!empty($data)) {
    $array = array_merge_recursive($array, array($data[0] => $data[1], $data[2]));
  }
}
fclose($file);
?>

Result is mixed up and I am missing how to make CSV input grouped by column like here:

High priority tasks:
1. Campus flyers (Ashley)
2. Test videocall with teacher (Sharam)
3. Ask permission to reserve classroom Sat & Sun (Sharam)

Medium priority tasks:
1. Auditorium recruiting (Martin & Aneet & Ashley)

Low priority tasks:
1. New Website (Martin & Aneet)
2. Garbage collecting (Sharam & Ashley)

My code it's missing presentational part but it is not the difficult part. HTML is easy to me but I have problem with array grouped properly. How can I achieve it? Thank you

Sorry about my grammars & spelling, I am just learning second year of English! --Sharam

2 Answers 2

1

Check out my PHP class: http://pastebin.com/KVcYxuXY

Here is how its used

<?php 
require_once('CSV_parser.php');
// path to your csv file
$parser = new CSV_parser('c:/sample.csv');
// Result an array
$parser->toArray();
// To table
$parser->toTable();
// To JSON
$parser->toJSON();
?>
Sign up to request clarification or add additional context in comments.

1 Comment

You can also use this to transfer you CSV data to DataGrid which allow grouping and filtering
0

Build an array that has the left most column as its index, and each entry in that array is the rest of the data in the row.

$result = array();
while (!feof($file)) {
  $data = fgetcsv($file, 1000, ",");
  $key = array_shift( $data);
  if( !isset( $result[ $key ])) {
    $result[ $key ] = array();
  }
  $result[ $key ][] = $data;
}

Now, to print it out, this is all you have to do:

foreach( $result as $priority => $entries) {
  echo $priority . ' priority tasks' . "\n";
  foreach( $entries as $i => $entry) {
      echo "$i. {$entry[0]} ({$entry[1]})\n";
  }
  echo "\n";
}

3 Comments

Dear nickb, now my program works perfect but I do not understand it fully. It seems the code can work without problem if I remove the statement "if( !isset( $result[ $key ])) { $result[ $key ] = array(); }" Can you elaborate what the purpose of the statement is?
@Sharam - It will work because PHP will automatically create an array for you, but issue a notice (or a warning, can't remember) of undefined variables. If you turn error_reporting all the way up, you'l see these notices. So, that if statement is there to prevent those notices/warnings.
Thank you I have already set error_reporting(-1); so I wonder why I am not seeing anything. I had suspicions that it is to suppress notices/errors, so what is wrong?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.