Search code examples
phpphpword

How to PHPWord foreach codeBlock


I need to convert the data in the MYSQL database to Word.

Word fileenter image description here

My code

require './modules/vendor/autoload.php';
require 'core.php';
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor(__DIR__ . '/wordfile.docx');
$topshiriqnoma = $connection->table('topshiriqnoma')->where('chiqinn', $id)->orderBy('id', 'desc')->paginate(50);
$nomi = $connection->table('topshiriqnoma')->where('chiqinn', $id)->orderBy('id', 'desc')->first();

unlink(UPLOAD_PATH . '/porucheniya/' . $item->chiqnm." ".shaklqisqa($item->chiqsh, $item->chiqnm) . '.docx');

foreach ($topshiriqnoma as $item) {
    $summahamma = str_replace(" ", "", $item->sum); // 70
    if (strpos($summahamma, ',') == true) {
        $mas = explode(',', $summahamma);
        $summasoz = convertNumberToWord($mas['0']) . " сўм " . $mas['1'] . " тийин";
        $summaraqam = number_format((float)$summahamma, 2, '.', ' ');
    } elseif (strpos($summahamma, '.') == true) {
        $mas = explode('.', $summahamma);
        $summasoz = convertNumberToWord($mas['0']) . " сўм " . $mas['1'] . " тийин";
        $summaraqam = number_format((float)$summahamma, 2, '.', ' ');
    } else {
        $summasoz = convertNumberToWord($item->sum) . " сўм";
        $summaraqam = number_format((float)$summahamma, 2, ',', ' ');
    }

    $replacements[] = [
        'no' => $item->no,
        'sana' => $item->sana,
        'tashkilot' => $item->chiqnm." ".shaklqisqa($item->chiqsh, $item->chiqnm),
        'chiqxr' => $item->chiqxr,
        'chiqinn' => $item->chiqinn,
        'chiqbk' => $item->chiqbk,
        'chiqmfo' => $item->chimfo,
        'summa' => $summaraqam,
        'kirtash' => $item->kirnm." ".shaklqisqa($item->kirsh, $item->kirnm),
        'kirxr' => $item->kirxr,
        'kirinn' => $r = (0 != $item->kirinn) ? $item->kirinn : '',
        'kirbk' => $item->kirbk,
        'kirmfo' => $item->kirmfo,
        'summasoz' => $summasoz,
        'maqsad' => $item->maqsad,
        'boshliq' => $item->bosh,
        'xisobchi' => $item->bux
    ];
}
$templateProcessor->cloneRow('nameblock', count($replacements));
$templateProcessor->saveAs(UPLOAD_PATH . '/porucheniya/' . $item->chiqnm." ".shaklqisqa($item->chiqsh, $item->chiqnm) . '.docx');
header('Location: ' . $home . '/upload/porucheniya/' . $item->chiqnm." ".shaklqisqa($item->chiqsh, $item->chiqnm) . '.docx');

enter image description here

But if the Word file is like this, everything works fine. The problem is that it has to work in Table mode (first picture) mode for me.


Solution

  • It didn't work because I used a much older PHPWord. I have installed a new PHPword and everything is working fine