Yes, there are many titles but I chose to explain the problem under this title because I could not find a solution in other titles :)
The database contains survey questions and records of the answers to this survey. I want to print the question titles of these records in the first column, and the answers under the columns.
For example,
question table id question_id question
answer table id question_id answer
I tried many methods but failed to show the answers in each line under the question headings.
Excel,
A - B - C .. etc.
1 - question_title question_title question_title .. etc.
2 - answer_title answer_title answer_title .. etc.
3 - answer_title answer_title answer_title .. etc.
4 - answer_title answer_title answer_title .. etc.
what have i done :)
$objPHPExcel = new PHPExcel();
$quest = $db->query("SELECT * FROM question WHERE question_id =1");
$answer = $db->query("SELECT * FROM answer WHERE question_id =1");
$objPHPExcel->setActiveSheetIndex(0);
$column = 'A';
foreach($quest as $rows){
$objPHPExcel->getActiveSheet()->SetCellValue($column.'1', $rows['question'],'UTF-8');
$column++;
}
$rowCount = 2;
$columns = 'A';
foreach($answer as $row){
$objPHPExcel->getActiveSheet()->SetCellValue($columns.'2', $row['answer'],'UTF-8');
$rowCount++;
$columns++;
}
i need to list the answers below the question
result :
My Answer:
>fetchAll("SELECT * FROM question");_$answers = $db->fetchAll("SELECT * FROM answer");_$row = 1;_$col = 0;_foreach($quest as $q) {_$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $q['question_title']);_$row++;_foreach($answers as $a) {_$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $a['answer_title']);_$row++;_$col++;_}$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');_$objWriter->save('survey_results.xlsx');_
This code snippet fetches the question titles and answer titles from the database and populates them in an Excel file. Each question title is printed in the first column, and the corresponding answer titles are printed in subsequent columns under each question title. The Excel file is then saved as 'survey_results.xlsx'.
Rate this post
5 of 5 based on 2346 votesComments