1

I’m working on a page that keeps record of all the data and documents of employees in a company, part of its function is generating ID cards, which is the part i’m struggling with.

I downloaded the PHP Word by first installing the composer and using it to install the PHPWord library in the root of the project (where the php pages are) and it installed the following files and folder:

-composer.lock -composer.json -vendor (this one is a folder containing all the library)

“vendor” subfolders and files: -autoload.php -composer (folder) -pclzip (folder) -phpoffice (folder) -zendframework (folder)

I assumed that was all there was to do for it to be installed so I proceeded to add the code, the problem is that it doesn’t work… here’s the code, i added comments to explain further:

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    require_once 'conector_superadmin.php';
    $link = connect($conn);

    //Retrieving the id number sent via url to get the employee number
    $idSelectedEmployee = $_GET['SelectedEmployee'];

    //Data taken from small form with extra information for the ID Card
    $idType = $_POST['idType'];
    $ValidFrom = $_POST['ValidFrom'];
    $ExpiresOn = $_POST['ExpiresOn'];

    //Retrieving employee's information from "employees" table in db
    $sql = "SELECT * FROM employees WHERE idEmployee = '$idSelectedEmployee'";
    $result = mysqli_query($link, $sql);
    while($row = mysqli_fetch_array($result)){
        $FullName = $row['FullName'];
        $Position = $row['Position'];
        $EmployeeNum = $row['EmployeeNum'];
        $BloodType = $row['BloodType'];
        $Alergies = $row['Alergies'];
        $PhoneNum = $row['PhoneNum'];
    }

    //The company uses two kinds of id cards, so i use these conditionals to get the type from a “select” field, it does work, i tested it
    if($idType == "Plastic"){
        //This code inside however is what isn’t working at all, it won't do anything despite taking this code from their template
        include_once 'Sample_Header.php';

        $templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('IDCards/Templates/ID_TEST.docx');

        //I am using in the docx file the “${Variable}” format as the template and other documentation explains

        $templateProcessor->setValue('EmployeeName', $FullName);
        $templateProcessor->setValue('PhoneNum', $PhoneNum);
        $templateProcessor->setValue('BloodType', $BloodType);
        $templateProcessor->setValue('Alergies', $Alergies);
        $templateProcessor->setValue('EmployeeNum', $EmployeeNum);
        $templateProcessor->setValue('Position', $Position);
        $templateProcessor->setValue('ValidFrom', $ValidFrom);
        $templateProcessor->setValue('ExpiresOn', $ExpiresOn);

        $templateProcessor->saveAs('IDCards/PlasticID.docx');

    }

    if($idType == "Paper"){
        //Still empty
    }

    //I created this table to insert the data taken from the “employees’ table, just for the purpose of testing if all the information was retrieved correctly, needless to say, it works fine
    $CredencialQuery = "INSERT INTO `idCards` (`idID`, `FullName`, `PhoneNum`, `BloodType`, `Alergies`, `EmployeeNum`, `Position`, `ValidFrom`, `ExpiresOn`, `idType`) VALUES (NULL, '$FullName', '$PhoneNum', '$BloodType', '$Alergies', '$EmployeeNum', '$Position', '$ValidFrom', '$ExpiresOn', '$idType');";

    mysqli_query($link, $CredencialQuery);

}
?>

So yeah I realized the problem itself might be the PHPWord code, either something in my code is wrong or the library is not properly installed, i tried to run the php template itself to test if that one worked but it doesn’t work either

ChBgt909
  • 23
  • 6
  • I'm almost certain the problem itself is the way i'm calling the library, I tried being more specific with the folders to no avail. Any help would be greatly appreciated. – ChBgt909 Jul 18 '18 at 23:26
  • Can you show is the error message? – Liora Haydont Jul 19 '18 at 15:28
  • or the current result vs expected result if there is no error – Liora Haydont Jul 19 '18 at 15:28
  • There's no error message, the PHPWord code inside the "if plastic" conditional just doesn't do anything plus, unless i comment it out, it keeps the rest of the code below it from working. – ChBgt909 Jul 19 '18 at 16:12

0 Answers0