Friday, June 29, 2012

Query and exporting to csv

I'm trying to export certain information to a CSV file. I have a drop down
list to select the drug you want to generate the information for and it
sends the id to the function to generate the csv(That is sending the correct
ID selected). The query runs and gets the information I want, I use it in
another view, but i'm having issues getting the data to get put into the
csv. I'm not sure what else to try. It doesn't put anything into the csv
despite accessing it the same way I do in the view. Any help is appreciated.

Functions:
function report(){
$this->set('alldrugs', $this->Drug->find('list', array(
'cache' => 'drugList',
'cacheConfig' => 'sql',
'fields' => array('id', 'generic'),
'order' => 'Drug.generic',
'recursive' => -1,
)));
if (!empty($this->data['DrugCalls'])) {

$this->redirect(array('controller'=>'drugs','action'=>'generatePatientInfo',$this->data['DrugCalls']['DrugList']['id']));
}
}
function generatePatientInfo($id=null) {
$this->layout = 'ajax';
// get drug info
$drug = $this->Drug->read(
array(
'Drug.id', 'Drug.generic'
),
$id
);
$this->set('drug',$drug);
$patients = $this->Drug->query('SELECT patients.id, calls.id call_id,
patients.first_name, patients.last_name, calls.created '
. 'FROM drugs, drug_lactation_links, lactation, calls, patients '
. 'WHERE drugs.id = ' . $id . ' AND drugs.id =
drug_lactation_links.drug_id AND '
. 'drug_lactation_links.lactation_id = lactation.id AND
lactation.call_id = calls.id AND calls.patient_id = patients.id '
. 'UNION '
. 'SELECT patients.id, calls.id call_id, patients.first_name,
patients.last_name, calls.created '
. 'FROM drugs, drug_pregnancy_links, pregnancies, calls, patients '
. 'WHERE drugs.id = ' . $id . ' AND drugs.id =
drug_pregnancy_links.drug_id AND '
. 'drug_pregnancy_links.pregnancy_id = pregnancies.id AND
pregnancies.call_id = calls.id AND calls.patient_id = patients.id '
. 'ORDER BY created DESC'
);
$this->set(compact('patients'));
}


View:
<?php
// add the header row
$header = array('First Name','Last Name');

$csv->addRow(array('Enter Generic: ' . $drug['Drug']['generic']));
$csv->addRow($header);

// add all the data
foreach($patient_list as $patient) {
$csv->addRow(array(
$patient[0]['first_name'],
$patient[0]['last_name'],
));
}
$csv->addRow(array());

// render the CSV file
echo $csv->render('Patients-list.csv');
?>


--
View this message in context: http://cakephp.1045679.n5.nabble.com/Query-and-exporting-to-csv-tp5709159.html
Sent from the CakePHP mailing list archive at Nabble.com.

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: