Search code examples
phpdatabasecodeigniterphpword

how to fill in table on template file from phpword?


source/template

How to insert table from database employee to my template document as above? I successfully filled company field.

code like bellow

require_once APPPATH.'libraries/autoload.php';
use PhpOffice\PhpWord\PhpWord;
public function cetaksurat()
{
   $templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('source.docx');
   $templateProcessor->setValues([
        'company' => 'my company',
        //how to insert employee table from my database array result?
        //.....
   ]);
   header("Content-Disposition: attachment; filename=result.docx");
   $templateProcessor->saveAs('php://output');
}

thank you


Solution

  • in my case I would use:

    $news2 = $this->phpword_model->get_employe(); //

            foreach($news2 as $key=>$values) {
        $array_values1 = $values['id'];
        $array_values2 = $values['name'];  
    }
    
    $templateProcessor->setValue('id', $array_values1);
    $templateProcessor->setValue('name', $array_values2);
    
    
        $templateProcessor->saveAs($filename);
    

    /// the first example is for a single record. now this example is for multiple records.

            $news7 = $this->phpword_model->get_employes();
            $templateProcessor->cloneRow('id', count($news7));
    
             $i=1;
    foreach($news7 as $key=>$values) {
        $templateProcessor->setValue('id#'.$i++, $values['id']); 
    }
    
                 $i2=1;
    foreach($news7 as $key=>$values) {
        $templateProcessor->setValue('name#'.$i2++, $values['name']);
    }
    
        $templateProcessor->saveAs($filename);