PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, November 10, 2022

[FIXED] How to PHPWord foreach codeBlock

 November 10, 2022     php, phpword     No comments   

Issue

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



Answered By - Akbarali
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing