Alexc
(Alexclaidlaw)
June 20, 2016, 11:27am
1
I’m currently running a gridview which exports to CSV.
The problem is I’m defining it like this. We are going to have custom variables grabbing names so the headers will change, so I’m trying to automate it to grab everything.
// $headers = array(
// 'Forename',
// 'Surname',
// 'user.email',
// 'user.telephone',
// 'user.company',
// 'user.company_role',
// 'event.name',
// 'status_id',
// 'checkin_status_id',
// 'Question1',
// 'Question2',
// 'Question3',
// 'Question4',
// 'Question5',
// 'Question6',
// );
$row = array();
foreach($headers as $header) {
$row[] = EventAttendees::model()->getAttributeLabel($header);
}
fputcsv($fp,$row);
And I was trying to automate it to grab everything, I tried this
$models = EventAttendees::model()->findAll();
$headers = CHtml::listData($models);
$row = array();
foreach($headers as $header) {
$row[] = EventAttendees::model()->getAttributeLabel($header);
}
fputcsv($fp,$row);
but it’s bringing up a 500 error.
hrnair
(Harikrishnan Hr)
June 20, 2016, 12:45pm
2
Try your code to render in a view file, so you can trouble shoot using debug feature.
Also, define $fp.
Alexc
(Alexclaidlaw)
June 20, 2016, 1:07pm
3
$fp = fopen(‘php://temp’, ‘w’); is defined.
Got no more information regarding the error when viewing as a view.
hrnair
(Harikrishnan Hr)
June 20, 2016, 1:29pm
4
$fp = fopen('php://temp', 'w');
foreach ($row as $r) {
fputcsv($fp, $r);
}
fclose($fp);
Alexc
(Alexclaidlaw)
June 20, 2016, 1:35pm
5
my entire function is
public function actionExport()
{
$fp = fopen('php://temp', 'w');
/*
* Write a header of csv file
*/
// $headers = array(
// 'user.forename',
// 'user.surname',
// 'user.email',
// 'user.telephone',
// 'user.company',
// 'user.company_role',
// 'event.name',
// 'status_id',
// 'checkin_status_id',
// 'Question1',
// 'Question2',
// 'Question3',
// 'Question4',
// 'Question5',
// 'Question6',
// );
$headers = array(
'Forename',
'Surname',
'Email Address',
'Mobile Number',
'checked in',
'Question1',
'Question2',
'Question3',
'Question4',
'Question5',
'Question6',
);
// $headers = CHtml::listData($modelHeader);
$row = array();
foreach($headers as $header) {
$row[] = EventAttendees::model()->getAttributeLabel($header);
}
fputcsv($fp,$row);
/*
* Init dataProvider for first page
*/
$model = new EventAttendees('exportSearch');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['EventAttendees'])) {
$model->attributes=$_GET['EventAttendees'];
}
$dp = $model->exportSearch($event_id);
$dp->setPagination(false);
/*
* Get models, write to a file
*/
$models = $dp->getData();
foreach($models as $model) {
$row = array();
foreach($headers as $head) {
$row[] = CHtml::value($model,$head);
}
fputcsv($fp,$row);
}
/*
* save csv content to a Session
*/
rewind($fp);
Yii::app()->user->setState('export',stream_get_contents($fp));
fclose($fp);
}
It grabs all rows, but falls over when grabbing the headers.
hrnair
(Harikrishnan Hr)
June 20, 2016, 1:41pm
6
I think the array fields need to be given separately.
instead of
fputcsv($fp,$row);
try
foreach ($row as $r) {
fputcsv($fp, $r);
}
or in your case
foreach($headers as $head) {
fputcsv($fp,CHtml::value($model,$head));
}
I think I am wrong. I hope you get expert help from somebody else.
hrnair
(Harikrishnan Hr)
June 20, 2016, 3:53pm
7
Alexc:
my entire function is
$row = array();
foreach($headers as $header) {
$row[] = EventAttendees::model()->getAttributeLabel($header);
}
fputcsv($fp,$row);
It grabs all rows, but falls over when grabbing the headers.
Since it only the problem with the headers in your csv, this might be a the solution. in CHtml, it is used in the following way.
$row = array();
$model = new EventAttendees();
foreach($headers as $header) {
$row[] = $model->getAttributeLabel($header);
}
fputcsv($fp,$row);
Sorry to end the previous post in a wrong note.
Alexc
(Alexclaidlaw)
June 21, 2016, 8:44am
8
The problem iis with
$headers = array(
'Forename',
'Surname',
'Email Address',
'Mobile Number',
'checked in',
'Question1',
'Question2',
'Question3',
'Question4',
'Question5',
'Question6',
);
I can no longer define these above, so I’ve been triyn gto grab the attributes like so , but no luck
$headers = EventAttendees::model()->findall();
hrnair
(Harikrishnan Hr)
June 21, 2016, 1:09pm
9
Alexc:
The problem iis with
$headers = array(
'Forename',
'Surname',
'Email Address',
'Mobile Number',
'checked in',
'Question1',
'Question2',
'Question3',
'Question4',
'Question5',
'Question6',
);
Why don’t you try lower case for all these items as we declare in the model?
Alexc
(Alexclaidlaw)
June 21, 2016, 2:31pm
10
It does work when it is declared such as this, but that is not what I want. As we have dynamic data we cannot add every field we have to controller function. What we need is a findall ()and what I’d assume is a chtml::listdata) to grab all the attributes. The trouble I’m having is that every time I’m grabbing all the attributes my CSV is coming back blank
hrnair
(Harikrishnan Hr)
June 22, 2016, 2:00pm
11
Alexc:
It does work when it is declared such as this, but that is not what I want. As we have dynamic data we cannot add every field we have to controller function. What we need is a findall ()and what I’d assume is a chtml::listdata) to grab all the attributes. The trouble I’m having is that every time I’m grabbing all the attributes my CSV is coming back blank
There is another way to achieve that objective with a little tweaking of the below code.
$headers = Yii::app()->db->createCommand('SHOW COLUMNS FROM `table_event_attendess`')->queryAll();
$row = array();
foreach($headers as $header) {
$row[] = $header['Field'];
}
fputcsv($fp,$row);