Search code examples
phpmysqlphpword

Replacing content of block after generating clones using cloneBlock - PHPWord


Please can somebody be so kind to show me the syntax for using cloneblock in phpword.

So Ive got data in a MySQL DB, and for the single rows that I need to import into my word doc via phpword it works fine....to run my query and search and replace with template processor. BUT, now I want to insert multiple rows into my word document. I've researched and found that the cloneblock method is the answer. However I cannot get it working....currently my code runs but it doesn't seem to get to the second row.

I actually dnt get any error messages. My code executes fine...but the end display word file doesn't display fine....and if you see my code I got an echo statement...which echo's out in my browser exactly what I want "damaged" &"good", (as an example given of one of the row data) but that data doesn't get pulled into my word doc like that...it duplicates "damaged" , "damaged". .

$group_key=1;

do {    

  //loop to increase my uuid  - ($repeatgroup')
  $repeatgroup = $id."/"."trailer_repeat_group"."[".$group_key."]";

// query string
  $trailer_repeat_grouping = mysqli_query($connect, "SELECT * FROM trailer_repeat_group LEFT JOIN main on trailer_repeat_group.PARENT_KEY = main.metainstanceID WHERE trailer_repeat_group.KEY_id = '$repeatgroup'");

  $templateProcessor->cloneBlock('CLONEME', $trailer_count);

  while ($row1 = mysqli_fetch_array($trailer_repeat_grouping)) {    

    //this echo below I am using to test exactly what happends – independent of 
    //PHPword/templateprocessor
    echo $rttc =  $row1['right_trailer_tyre_condition'];

    //inserting  / searching / inserting values
    $templateProcessor->setValue("right_trailer_tyre_condition", $rttc);
  }

  // ending of loop / checking loop

  $group_key++;

} while ($group_key <= $trailer_count);

Solution

  • I've done investigation and found the solution.

    You're cloning same blocks N times:

    $templateProcessor->cloneBlock('CLONEME', $trailer_count);
    

    and then by doing fetch You're trying to replace right_trailer_tyre_condition with some value:

    $templateProcessor->setValue("right_trailer_tyre_condition", $rttc);
    

    Issue is that You're replacing all placeholders.

    But in fact You need to replace them one by one with different values.

    Solution is to define 3rd argument that means count of items to replace.

    Simply change it to be:

    $templateProcessor->setValue("right_trailer_tyre_condition", $rttc, 1);