Link PHPWord with MySQL data

Oct 2, 2012 at 1:10 PM

I am trying to link data from MySQL Database into a Word Document using PHPWord. I've set up the package on my server and examples are working fine. When I use the Simple example Text.php:

<?php
require_once 'PHPWord.php';

// New Word Document
$PHPWord = new PHPWord();

// New portrait section
$section = $PHPWord->createSection();

// Add footer
$footer = $section->createFooter();
$footer->addPreserveText('Page {PAGE} of {NUMPAGES}.', array('align'=>'center'));

 

 


// Add text elements
$section->addText('Hello World!');
$section->addTextBreak(2);

$section->addText('I am inline styled.', array('name'=>'Verdana', 'color'=>'006699'));
$section->addTextBreak(2);

$PHPWord->addFontStyle('rStyle', array('bold'=>true, 'italic'=>true, 'size'=>16));
$PHPWord->addParagraphStyle('pStyle', array('align'=>'center', 'spaceAfter'=>100));
$section->addText('I am styled by two style definitions.', 'rStyle', 'pStyle');
$section->addText('I have only a paragraph style definition.', null, 'pStyle');

 

// Save File
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('Text.docx');
?>

 

And try to add just before that:

 

include("config.inc.php3");
include("session.inc.php3");

$query="SELECT Opt1,Current_Exm FROM System WHERE Opt1='1'";
$rbsult=MYSQL_QUERY($query) or redirect("http://www.cspsopcs.com/portal/eng/error.php3?err=4682");
if(!$rbw=MYSQL_FETCH_ARRAY($rbsult))
  {
  redirect("error.php3?err=4682");
  }

 

The word document is not generated. I'd like to simply replace "HELLO WORLD" by a variable $rbw[1]

Is there a way or something I dont understand?

 

Thanks!

 

Oct 5, 2012 at 12:43 AM

Actually mu code looks like this in that order:

<!--p

include("config.inc.php3");
include("session.inc.php3");

$query="SELECT Opt1,Current_Exm FROM System WHERE Opt1='1'";
$rbsult=MYSQL_QUERY($query) or redirect("http://www.cspsopcs.com/portal/eng/error.php3?err=4682");
if(!$rbw=MYSQL_FETCH_ARRAY($rbsult))
  {
  redirect("error.php3?err=4682");
  }






require_once 'PHPWord.php';

//PHP Creation Written English


// New Word Document
$PHPWord = new PHPWord();

// New portrait section
$section = $PHPWor-->createSection();


$section->addTextBreak(8);
$section->addImage('../common_web_pictures/logo.jpg', array('width'=>210, 'height'=>210, 'align'=>'center'));

$PHPWord->addFontStyle('rStyle', array('bold'=>false, 'italic'=>false, 'size'=>16));
$PHPWord->addParagraphStyle('pStyle', array('align'=>'center', 'spaceAfter'=>'0'));

$section->addText('Canadian Ski Patrol', 'rStyle', 'pStyle');
$section->addText('Advanced First Aid (AFA)', 'rStyle', 'pStyle');
$section->addText('National Written Exam', 'rStyle', 'pStyle');
$section->addTextBreak(2);

//This is the data from my database and table System
$section->addText($rbw[1]);

// Add footer
$footer = $section->createFooter();
$footer->addPreserveText('Modifications to examinations are not permitted without written approval of the National Vice President of Education.', array('size'=>8, 'align'=>'center'));



// Save File
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('Written.docx');
?>
So it isnt working, no file, just nothing. I wonder how it can go and get data into a MySQL database.
Thanks!
Oct 9, 2012 at 12:05 AM

Found IT......

<!--p

$host = "Myserver"; 
$user = "MyUsername"; 
$pass = "MyPassword"; 
$database = "Mydatabase"; 

$linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host."); 
mysql_select_db($database, $linkID) or die("Could not find database."); 

$query = "SELECT * FROM System ORDER BY Opt1";
$resultID = mysql_query($query, $linkID) or die("Data not found."); 
$raw = mysql_fetch_assoc($resultID); 


require_once 'PHPWord.php';

$PHPWord = new PHPWord();

$document = $PHPWor-->loadTemplate('Template.docx');

$document->setValue('Value1', $raw['Current_Exm']);

for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++)
	{

 

It get to my databae.

Oct 9, 2012 at 5:02 AM

You just use 'Value1'?