$PHPWord = new PHPWord();
$section = $PHPWord->createSection();
$table = $section->addTable();
$i = 1;
$document = $PHPWord->loadTemplate('/var/sitLims/web/uploads/lmsSyllabus/lmsSyllabus.docx');
$document->setValue('Value1', $course_number);
$document->setValue('Value2', $course_name);
$document->setValue('Value3', $course_en_name);
$document->setValue('Value4', $course_summary);
$document->setValue('Value5', $course_purposes);
$document->setValue('Value6', $course_content);
$document->setValue('Value7', $course_exam);
$document->setValue('Value8', $course_description);
$document->setValue('Value9', $syllabus_person);
$document->setValue('Value10', $syllabus_academy_opinion);
foreach($syllabus_experiment as $a) {
$table->addRow();
$table->addCell(30)->addText($i);
$table->addCell(118)->addText($a->lmsExperiment->getExperimentName());
$table->addCell(118)->addText('');
$table->addCell(50)->addText($a->lmsExperiment->getExperimentExperimental());
$table->addCell(50)->addText($a->lmsExperiment->getThisExperimentHours());
$table->addCell(50)->addText($a->lmsExperiment->getExperimentEachNumber());
$table->addCell(50)->addText($a->lmsExperiment->getExperimentLab());
$table->addCell(50)->addText($a->lmsExperiment->getExperimentProjectArrange());
$i ++;
}
$document->save('/var/sitLims/web/uploads/lmsSyllabus/' . $syllabus_name . '.docx');`
I am not sure how to insert the table when creating a word file Shall i insert the table in the template anywhere?
You can use it: https://github.com/Arisse/PHPWord_CloneRow
It's new version of CloneRow() and setValue(). Stable and easy to use.
The bad news is: this won't work.
The
setValue
function expects the parameter to be plain text, while$table
contains an object. Currently there's no solution to have the$table
object return something that can be used withsetValue
.As I had the same issue, I've created a work around. To get this working you've to add the table you would like to use in your document inside your template. Create one single row inside this table with a variable name inside each cell.
Call the
cloneRow
function from the patched version of PHPWord to receive the required number of table rows. After that just usesetValue
appending the row number to each variable.You can get the code and full explanation on my site: http://jeroen.is/phpword-templates-with-repeating-rows/
Add it to PHPWord\Writer\Word2007\Document.php
Insert table: