0

I am using php laravel. I'm trying to export the data into excel .csv file with unique data or leaving the cell empty instead of duplicating the data. What filter should I use to do that? To be clear I will show some illustration
This is an example of the Excel View with the data from the array which is exported using php:

+--------+------------+-----------+
| Group  | First Name | Last Name |
+--------+------------+-----------+
| Group1 | John       | Doe       |
+--------+------------+-----------+
| Group1 | Jane       | Doe       |
+--------+------------+-----------+
| Group2 | John       | Smith     |
+--------+------------+-----------+
| Group2 | Jane       | Smith     |
+--------+------------+-----------+

This is an example of Excel View that I need. You can see that the duplicating groups are hidden:

+--------+------------+-----------+
| Group  | First Name | Last Name |
+--------+------------+-----------+
| Group1 | John       | Doe       |
+--------+------------+-----------+
|        | Jane       | Doe       |
+--------+------------+-----------+
| Group2 | John       | Smith     |
+--------+------------+-----------+
|        | Jane       | Smith     |
+--------+------------+-----------+

And this is the code that I've been using:

public function getDashboardBookingsReport2($account_alias, $group_alias){
    header       (...)

        $file = fopen('php://output', 'w');


        fputcsv($file, array('Group Name', 'First Name', 'Last Name'));

        $table = (...)

        $rowed[] = array(
            $egroup->name,     // Group
            $user->first_name, // First Name
            $user->last_name,  // Last Name
                        );
                    }

                }
            }
                (...)

        $data = $rowed;
        uasort($data, function($a, $b) {
            return strcmp($a[0], $b[0]);
        });
        foreach ($data as $row)
        {
            fputcsv($file, $row);
        }

        exit();
    return Redirect::to( $group_alias.'/app/dash' );
}
4
  • Not sure what you asking. You don't want to write the first cell is the same while writing the PHP File? If so how is even excel involved in this? You are just writing a file from PHP correct? Commented Jan 14, 2017 at 3:37
  • @dgorti I include excel there because that code exports as excel .csv. The diagram above is an example of an excel with duplicating group name Commented Jan 14, 2017 at 3:47
  • No it is not. Your code is not exporting anything as excel. It is exporting to a CSV that can be opened by excel. But csv is a general format and your code does not do anything with excel. SO there are no filters to be applied. You need to decide how you want to write the csv file. Commented Jan 14, 2017 at 6:43
  • @dgorti I'm telling you it is. All im asking is how to remove the duplicates because exporting it is already working Commented Jan 15, 2017 at 7:13

1 Answer 1

1

Add this code just before foreach ($data as $row) and after uasort($data, function($a, $b):

for($i = count($data) - 1; $i > 0; $i--) {
    if($data[$i][0] === $data[$i - 1][0]) {
        $data[$i][0] = "";
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.