??????????????? ??????????????? 0) { while($result = mysqli_fetch_array($query)) $idSTR .= $result['id'].'||'; return $idSTR; } return array(); } function getQuestionOptions($tableName, $questionId=0) { global $myCon2; if(!empty($tableName) && $questionId > 0) { $query = mysqli_query($myCon2,"select * from question_board_option where ques_board_id='".mysqli_real_escape_string($myCon2,$questionId)."' order by id asc"); if(mysqli_num_rows($query) > 0) { $optionsArray = array(); while($result = mysqli_fetch_array($query)) { $optionsArray[] = $result; } return $optionsArray; } } return array(); } $mockBoardNum = getRandomCategory(23); // 2 for AM, //23 for PM $catID = explode('||',$mockBoardNum); $ccStr = ""; foreach($catID as $value) { if(!empty($value)) $ccStr .= " category_id='".$value."' or"; } $ccStr = rtrim($ccStr," or"); $query = mysqli_query($myCon2,"select * from question_board where ".$ccStr); // $query = mysqli_query($myCon2,"select * from question_board"); // Create spreadsheet $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setTitle('Questions'); // Header row $headers = ['Question ID', 'Question', 'Option 1', 'Option 2', 'Option 3', 'Option 4', 'Option 5', 'Option 6', 'Correct Answer', 'Rationale']; $sheet->fromArray($headers, null, 'A1'); $correctAnsOptions = ["A","B","C","D","E","F"]; // Fill in data $row = 2; while($result = mysqli_fetch_array($query)) { $loadQuestionOptions = getQuestionOptions('question_board_option',$result['id']); $sheet->setCellValue("A$row", $result['id']); $sheet->setCellValue("B$row", $result['question_text']); $opt = 0; $rationale = ""; $correctAns = ""; foreach ($loadQuestionOptions as $optionsArray) { $col = chr(ord('C') + $opt); // B, C, D, E $sheet->setCellValue("{$col}{$row}", $optionsArray['option_name']); if($result['correct_option_no'] == $optionsArray['id']) { $rationale = $optionsArray['option_description']; $correctAns = $correctAnsOptions[$opt]; } $opt++; } $sheet->setCellValue("I{$row}", $correctAns); $sheet->setCellValue("J{$row}", $rationale); $row++; // if ($row == 10) break; } // Output file to browser header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename="questions_with_options.xlsx"'); header('Cache-Control: max-age=0'); $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet); $writer->save('php://output'); exit; ?>