PHPWord Template looping through query results not populating Values

Jan 14, 2014 at 6:00 PM
I'm getting this error: Fatal error: Uncaught exception 'Exception' with message 'Could not close zip file.' in /home/dyken/public_html/PHPWord/Template.php:109 Stack trace: #0 /home/dyken/public_html/lettersWord.php(40): PHPWord_Template->save('MemberLetters.d...') #1 {main} thrown in /home/dyken/public_html/PHPWord/Template.php on line 109

I have this code:

$sql = "SELECT distinct p.person_id, fname_mi, lname, company_name, addr1, addr2, city, st, zip, greeting, email, fullName FROM person p INNER JOIN person_category c on c.person_id = p.person_id WHERE category_id = 1 order by lname, fname_mi, company_name";

$result = mysqli_query($dlink,$sql);

require_once 'PHPWord.php';

while ($row = mysqli_fetch_array($result)) {

$PHPWord = new PHPWord();
$document = $PHPWord->loadTemplate('letters/testLetter.docx');

$document->setValue('Value1', $row[1]);
$document->setValue('Value2', $row[2]);
$document->setValue('Value3', $row[3]);
$document->setValue('Value4', $row[4]);
$document->setValue('Value5', $row[5]);
$document->setValue('Value6', $row[6]);
$document->setValue('Value7', $row[7]);
$document->setValue('Value8', $row[8]);
$document->setValue('Value9', $row[9]);
$document->setValue('Value10', $row[10]);
$document->setValue('Value11', $row[11]);
$file = 'MemberLetters.docx' . $row[0];
$document->save($file);

if(!$file) {
// File doesn't exist, output error
die('file not found');
}
else {
 header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document");
header("Content-Transfer-Encoding: binary");

readfile($file);
}

unlink($file);

exit;
}




I'm getting this error: Fatal error: Uncaught exception 'Exception' with message 'Could not close zip file.' in /home/dyken/public_html/PHPWord/Template.php:109 Stack trace: #0 /home/dyken/public_html/lettersWord.php(40): PHPWord_Template->save('MemberLetters.d...') #1 {main} thrown in /home/dyken/public_html/PHPWord/Template.php on line 109

I have this code:

$sql = "SELECT distinct p.person_id, fname_mi, lname, company_name, addr1, addr2, city, st, zip, greeting, email, fullName FROM person p INNER JOIN person_category c on c.person_id = p.person_id WHERE category_id = 1 order by lname, fname_mi, company_name";

$result = mysqli_query($dlink,$sql);

require_once 'PHPWord.php';

while ($row = mysqli_fetch_array($result)) {

$PHPWord = new PHPWord();
$document = $PHPWord->loadTemplate('letters/testLetter.docx');

$document->setValue('Value1', $row[1]);
$document->setValue('Value2', $row[2]);
$document->setValue('Value3', $row[3]);
$document->setValue('Value4', $row[4]);
$document->setValue('Value5', $row[5]);
$document->setValue('Value6', $row[6]);
$document->setValue('Value7', $row[7]);
$document->setValue('Value8', $row[8]);
$document->setValue('Value9', $row[9]);
$document->setValue('Value10', $row[10]);
$document->setValue('Value11', $row[11]);
$file = 'MemberLetters.docx' . $row[0];
$document->save($file);

if(!$file) {
// File doesn't exist, output error
die('file not found');
}
else {
 header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document");
header("Content-Transfer-Encoding: binary");

readfile($file);
}

unlink($file);

exit;
}

testLetter.docx contains this:

${Value1} ${Value2}
${Value3}
${Value4}
${Value5}
${Value6}, ${Value7} ${Value8}

I cannot get the query to populate the Values in the letter.

The query produces results elsewhere in the website, so I know that works.

$dlink is my $host, $user, $password and $dbname connection

I am getting documents in the letters folder, but not as many as I expect and they are all exact copies of testLetter.docx with the ${Value1}, etc. and not the data from my query.

I have tried different variations of the code, but I can never get any of my query results in place of the $Values. Can someone please help me with this? I'm new to PHPWord.