Forum


Replies: 1   Views: 805
How do i loop to create a table with multiple value
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by ElodieMllr  · 23-11-2022 - 09:22

 I am using phpdocx to generate an array with my data in a docx format.

$contact is an array of multiple object. Sometimes $contact contain 1 object ([0]), and sometimes more.

I want to make a loop, to add contact as much as I need.

My problem : For exemple, If I am doing this I will get an error like "Undefined array key 3" if my contact data only contain 3 object  ([0, 1, 2 ]) or less.

important : Here, if my datas contain 4 object (from 0 to 3 ) it will work but doesn't work when i have 2 objects.

what i am actually trying with no success for the moment : https://www.phpdocx.com/en/forum/default/topic/1773

 $contact= array(
      array(
        'name' => $request->get('contact')[0]['name'],
        'userName' =>  $request->get('contact')[0]['userName'],
        'number' =>  $request->get('contact')[0]['number'],
        'mail' =>  $request->get('contact')[0]['mail'],
      ),
        array(
        'name' => $request->get('contact')[1]['name'],
        'userName' =>  $request->get('contact')[1]['userName'],
        'number' =>  $request->get('contact')[1]['number'],
        'mail' =>  $request->get('contact')[1]['mail'],
      ),    
      array(
        'name' => $request->get('contact')[2]['name'],
        'userName' =>  $request->get('contact')[2]['userName'],
        'number' =>  $request->get('contact')[2]['number'],
        'mail' =>  $request->get('contact')[2]['mail'],
      ), 
      array(
        'name' => $request->get('contact')[3]['name'],
        'userName' =>  $request->get('contact')[3]['userName'],
        'number' =>  $request->get('contact')[3]['number'],
        'mail' =>  $request->get('contact')[3]['mail'],
      ),

    );


$docx->replaceTableVariable($contact, array('parseLineBreaks' => true));

Posted by admin  · 23-11-2022 - 09:32

Hello,

Please note that this question is not related to phpdocx but to PHP. PHP throws a warning if you try to read a non-existing array key/position/value.

You need to generate a multidimensional PHP array from your data. For example, using a $dataValues array as data source:

$tableData = array();
foreach ($dataValues as $dataValue) {
    $rowData = array(
        'VAR_1' => $dataValue['val_1'],
        'VAR_2' => $dataValue['val_2'],
        'VAR_3' => $dataValue['val_3'],
    );

    $tableData[] = $rowData;
}

How to do the exact loop (iteration) depends on the data you are working with. The problem with your code is that you are accesing fixed array keys, and you should get them dynamically to know the exact number of rows to do the loop and generate the correct array.

On https://www.php.net/manual/en/language.types.array.php you can read PHP documentation about arrays.

Regards.