Export the test paper to see the effect first
$paper = Exam::where('id', $data['examid'])->field('paper_id,title')->find(); $bankIds = PaperStorehouse::where('id', $paper['paper_id'])->value('banks'); $field = 'type,rate,name,options,parsing,value,question_id,parsing_id'; $bankInfo = Banks::where('id', 'in', $bankIds)->field($field)->select() ->map(function ($item) { if ($item['question_id'] == 0) { $item['question_id'] = ''; } else { $item['question_id'] = getDomain(File::where('id', $item['question_id'])->value('filepath')); } if ($item['parsing_id'] == 0) { $item['parsing_id'] = ''; } else { $item['parsing_id'] = getDomain(File::where('id', $item['parsing_id'])->value('filepath')); } $item['rate'] = taskdegreeName($item['rate']); $item['typeTxt'] = bankTxt($item['type']); $options = json_decode($item['options'], true); $item['Aanswer'] = $options[0]['value'] ?: ''; $item['Banswer'] = $options[1]['value'] ?: ''; $item['Canswer'] = $options[2]['value'] ?: ''; $item['Danswer'] = $options[3]['value'] ?: ''; return $item; }); // $singles $multi $judge $operate $filling $read $sub $singles = []; $multi = []; $subjectivity = []; $judge = []; foreach ($bankInfo as $k => $v) { if ($v['type'] == 1) { $singles[] = $v; } elseif ($v['type'] == 2) { $multi[] = $v; } elseif ($v['type'] == 3) { $judge[] = $v; } else { $subjectivity[] = $v; } } $path = public_path(); $tmp = new TemplateProcessor($path . 'word.docx'); $singlesNum = count($singles); $multiNum = count($multi); $judgeNum = count($judge); $subjectivityNum = count($subjectivity); $tmp->setValue('examTitle', $paper['title']); $tmp->cloneRow('singles', $singlesNum); $tmp->cloneRow('multi', $multiNum); $tmp->cloneRow('judge', $judgeNum); $tmp->cloneRow('subjectivity', $subjectivityNum); for ($i = 0; $i < $singlesNum; $i++) { $tmp->setValue("singles#" . ($i + 1), $singles[$i]['typeTxt']); $tmp->setValue("singlesName#" . ($i + 1), $singles[$i]['name']); $tmp->setValue("AAanswer#" . ($i + 1), $singles[$i]['Aanswer']); $tmp->setValue("ABanswer#" . ($i + 1), $singles[$i]['Banswer']); $tmp->setValue("ACanswer#" . ($i + 1), $singles[$i]['Canswer']); $tmp->setValue("ADanswer#" . ($i + 1), $singles[$i]['Danswer']); } for ($i = 0; $i < $multiNum; $i++) { $tmp->setValue("multi#" . ($i + 1), $multi[$i]['typeTxt']); $tmp->setValue("multiName#" . ($i + 1), $multi[$i]['name']); $tmp->setValue("MAanswer#" . ($i + 1), $multi[$i]['Aanswer']); $tmp->setValue("MBanswer#" . ($i + 1), $multi[$i]['Banswer']); $tmp->setValue("MCanswer#" . ($i + 1), $multi[$i]['Canswer']); $tmp->setValue("MDanswer#" . ($i + 1), $multi[$i]['Danswer']); } for ($i = 0; $i < $judgeNum; $i++) { $tmp->setValue("judge#" . ($i + 1), $judge[$i]['typeTxt']); $tmp->setValue("judgeName#" . ($i + 1), $judge[$i]['name']); $tmp->setValue("Eanswer#" . ($i + 1), $judge[$i]['Aanswer']); $tmp->setValue("Fanswer#" . ($i + 1), $judge[$i]['Banswer']); } for ($i = 0; $i < $subjectivityNum; $i++) { $tmp->setValue("subjectivity#" . ($i + 1), $subjectivity[$i]['typeTxt']); $tmp->setValue("subjectivityName#" . ($i + 1), $subjectivity[$i]['name']); } $tmp->saveAs($path . 'target.docx'); //Browser download ob_clean(); ob_start(); $fp = fopen($path . 'target.docx', "r"); $file_size = filesize($path . 'target.docx'); Header("Content-type:application/octet-stream"); Header("Accept-Ranges:bytes"); Header("Accept-Length:" . $file_size); Header("Content-Disposition: attchment; filename=" . 'Test file.docx'); $buffer = 1024; $file_count = 0; while (!feof($fp) && $file_count < $file_size) { $file_con = fread($fp, $buffer); $file_count += $buffer; echo $file_con; } fclose($fp); ob_end_flush();
My $bankInfo variable looks like this
array:13 [ 0 => array:13 [ "type" => 1 "rate" => "低" "name" => "<p>11111</p>" "options" => "[{"name":"A","value":"111","attrid":"","attr":""},{"name":"B","value":"111","attrid":"","attr":""},{"name":"C","value":"111","attrid":"","attr":""},{"name":"D","value":"111","attrid":"","attr":""}]" "parsing" => "" "value" => 6 "question_id" => "" "parsing_id" => "" "typeTxt" => "Single-choice question" "Aanswer" => "111" "Banswer" => "111" "Canswer" => "111" "Danswer" => "111" ] 1 => array:13 [ "type" => 1 "rate" => "低" "name" => "<p>11111</p>" "options" => "[{"name":"A","value":"111","attrid":"","attr":""},{"name":"B","value":"111","attrid":"","attr":""},{"name":"C","value":"111","attrid":"","attr":""},{"name":"D","value":"111","attrid":"","attr":""}]" "parsing" => "" "value" => 6 "question_id" => "" "parsing_id" => "" "typeTxt" => "Single-choice question" "Aanswer" => "111" "Banswer" => "111" "Canswer" => "111" "Danswer" => "111" ] 2 => array:13 [ "type" => 1 "rate" => "低" "name" => "<p>RETYUIO</p>" "options" => "[{"name":"A","value":"WERTYUI","attrid":"","attr":""},{"name":"B","value":"FDGHJKL","attrid":"","attr":""}]" "parsing" => "<p>EWRTYUI</p>" "value" => 10 "question_id" => "http://127.0.0.1:99" "parsing_id" => "" "typeTxt" => "Single-choice question" "Aanswer" => "WERTYUI" "Banswer" => "FDGHJKL" "Canswer" => "" "Danswer" => "" ] 3 => array:13 [ "type" => 2 "rate" => "低" "name" => "<p>???</p>" "options" => "[{"name":"A","value":"1","attrid":"","attr":""},{"name":"B","value":"2","attrid":"","attr":""},{"name":"C","value":"3","attrid":"","attr":""}]" "parsing" => "<p>888</p>" "value" => 11 "question_id" => "" "parsing_id" => "" "typeTxt" => "multiple choice" "Aanswer" => "1" "Banswer" => "2" "Canswer" => "3" "Danswer" => "" ] 4 => array:13 [ "type" => 1 "rate" => "中" "name" => "flammable gas ( )." "options" => "[{"name":"A","value":" 5-15%","attrid":"","attr":""},{"name":"B","value":" 6-16%","attrid":"","attr":""},{"name":"C","value":" 5-16%","attrid":"","attr":""},{"name":"D","value":" 4-16%","attrid":"","attr":""}]" "parsing" => "This is a single-choice question parsing" "value" => 2 "question_id" => "" "parsing_id" => "" "typeTxt" => "Single-choice question" "Aanswer" => " 5-15%" "Banswer" => " 6-16%" "Canswer" => " 5-16%" "Danswer" => " 4-16%" ] 5 => array:13 [ "type" => 1 "rate" => "中" "name" => "flammable explosion limit two ( )." "options" => "[{"name":"A","value":" 5-15%","attrid":"","attr":""},{"name":"B","value":" 6-16%","attrid":"","attr":""},{"name":"C","value":" 5-16%","attrid":"","attr":""},{"name":"D","value":" 4-16%","attrid":"","attr":""}]" "parsing" => "This is a single-choice question parsing" "value" => 2 "question_id" => "" "parsing_id" => "" "typeTxt" => "Single-choice question" "Aanswer" => " 5-15%" "Banswer" => " 6-16%" "Canswer" => " 5-16%" "Danswer" => " 4-16%" ] 6 => array:13 [ "type" => 2 "rate" => "低" "name" => "What are the risk point source identifications for low-voltage power distribution room fire?" "options" => "[{"name":"A","value":"Open fire caused 11 burns","attrid":"","attr":""},{"name":"B ","value":"The transformer itself is prone to explosion","attrid":"","attr":""},{"name":"C","value":"Open flames may cause environmental pollution"," attrid":"","attr":""},{"name":"D","value":" None of the above is correct","attrid":"","attr":""}]" "parsing" => "This is multiple choice question parsing" "value" => 2 "question_id" => "" "parsing_id" => "" "typeTxt" => "multiple choice" "Aanswer" => "Open fire caused burns 11" "Banswer" => "The transformer itself is prone to explosion" "Canswer" => "Open flames may cause environmental pollution" "Danswer" => "None of the above are correct" ] 7 => array:13 [ "type" => 2 "rate" => "低" "name" => "Test 2 What are the two risk point source identifications for fires in low-voltage power distribution rooms?" "options" => "[{"name":"A","value":"Test 2 fire caused burns","attrid":"","attr":""},{"name":" B","value":"Test 2 The transformer itself is prone to explosion","attrid":"","attr":""},{"name":"C","value":"Test 2 open flame may cause Environmental pollution","attrid":"","attr":""},{"name":"D","value":"Test 2 and above are all wrong","attrid":"","attr" :""}]" "parsing" => "This is multiple choice question parsing" "value" => 1 "question_id" => "" "parsing_id" => "" "typeTxt" => "multiple choice" "Aanswer" => "Test 2 burns caused by open flame" "Banswer" => "Test 2 Transformer itself is prone to explosion" "Canswer" => "Test 2 Open fire may cause environmental pollution" "Danswer" => "Test 2 and above are all wrong" ] 8 => array:13 [ "type" => 3 "rate" => "高" "name" => "Test that flammable gas leakage will not cause suffocation and environmental pollution." "options" => "[{"name":"A","value":" 错误","attrid":"","attr":""},{"name":"B","value":" 正确","attrid":"","attr":""}]" "parsing" => "This is a true or false answer" "value" => 3 "question_id" => "" "parsing_id" => "" "typeTxt" => "True or False" "Aanswer" => "Error" "Banswer" => "Correct" "Canswer" => "" "Danswer" => "" ] 9 => array:13 [ "type" => 3 "rate" => "中" "name" => "Test judgment question two." "options" => "[{"name":"A","value":" 错误","attrid":"","attr":""},{"name":"B","value":" 正确","attrid":"","attr":""}]" "parsing" => "This is a true or false answer" "value" => 1 "question_id" => "" "parsing_id" => "" "typeTxt" => "True or False" "Aanswer" => "Error" "Banswer" => "Correct" "Canswer" => "" "Danswer" => "" ] 10 => array:13 [ "type" => 5 "rate" => "中" "name" => "Test this is fill in the blank question one ()" "options" => "" "parsing" => "Fill in the blank question analysis (if there is no analysis, this line can be left blank)" "value" => 3 "question_id" => "" "parsing_id" => "" "typeTxt" => "fill in the blank" "Aanswer" => "" "Banswer" => "" "Canswer" => "" "Danswer" => "" ] 11 => array:13 [ "type" => 4 "rate" => "高" "name" => "Test how to keep fit one?" "options" => "" "parsing" => "None (if there is no parsing, this line can be left blank)" "value" => 3 "question_id" => "" "parsing_id" => "" "typeTxt" => "Short Answer Question" "Aanswer" => "" "Banswer" => "" "Canswer" => "" "Danswer" => "" ] 12 => array:13 [ "type" => 6 "rate" => "中" "name" => "Test this is reading comprehension question one" "options" => "" "parsing" => "None (if there is no parsing, this line can be left blank)" "value" => 1 "question_id" => "" "parsing_id" => "" "typeTxt" => "Reading Comprehension Questions" "Aanswer" => "" "Banswer" => "" "Canswer" => "" "Danswer" => "" ] ]
Core code:
$tmp = new TemplateProcessor($path . 'word.docx'); Open a template
The template looks like this:
$tmp->setValue('examTitle', $paper['title']); The php program will recognize the examTitle in the template ${examTitle} in the template is equivalent to a placeholder and will be replaced when the program recognizes it (The small pit template is actually in the form of a table, but the color I set for the border here is the same as the background color) $tmp->cloneRow('singles', $singlesNum); The number of cloned rows (if it is not in the table, an error will be reported)
for ($i = 0; $i < $subjectivityNum; $i++) { $tmp->setValue("subjectivity#" . ($i + 1), $subjectivity[$i]['typeTxt']); $tmp->setValue("subjectivityName#" . ($i + 1), $subjectivity[$i]['name']); }
What should I do if I want to assign a value to a loop variable? Just add # after the placeholder tag
$tmp->saveAs($path . 'target.docx'); save the new file