Create a product with inserted tabel

Oct 14, 2012 at 4:09 PM

Hello I am trying to create a doc with a table in it. Both document are working fine in different docs. I combined them and it stopped to word. It is combination of Template.php and Advancedtable.php.

Could you please have a look and advice what I should correct or where to start?

thnaks

mpol_ch

<?php

mysql_connect("localhost", "read566", "*****") or die(mysql_error());
echo "Connected to MySQL<br />";
mysql_select_db("testdb") or die(mysql_error());
echo "Connected to Database";
echo getcwd() . "\n";
require_once("C:/xampp/htdocs/9/tests/free_phpword/phpword/PHPWord.php");

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

$document = $PHPWord->loadTemplate('vorlage.docx');

$document->setValue('Value1', 'Sun');
$document->setValue('Value2', 'Mercury');
$document->setValue('Value3', 'Venus');
$document->setValue('Value4', 'Earth');
$document->setValue('Value5', 'Mars');
$document->setValue('Value6', 'Jupiter');
$document->setValue('Value7', 'Saturn');
$document->setValue('Value8', 'Uranus');
$document->setValue('Value9', 'Neptun');
$document->setValue('Value10', 'Pluto');

$document->setValue('weekday', date('l'));
$document->setValue('time', date('H:i'));



// New portrait section
$section = $PHPWord->createSection();
$section->addText('I am placed on a default section.');

// Define table style arrays
$styleTable = array('borderSize'=>6, 'borderColor'=>'006699', 'cellMargin'=>80);
$styleFirstRow = array('borderBottomSize'=>18, 'borderBottomColor'=>'0000FF', 'bgColor'=>'66BBFF');

// Define cell style arrays
$styleCell = array('valign'=>'center');
$styleCellBTLR = array('valign'=>'center', 'textDirection'=>PHPWord_Style_Cell::TEXT_DIR_BTLR);

// Define font style for first row
$fontStyle = array('bold'=>true, 'align'=>'center');

// Add table style
$PHPWord->addTableStyle('myOwnTableStyle', $styleTable, $styleFirstRow);

// Add table
$table = $section->addTable('myOwnTableStyle');
// Add row
$table->addRow(500);

// Add cells
$table->addCell(100, $styleCell)->addText('Id', $fontStyle);
$table->addCell(5000, $styleCell)->addText('Titel', $fontStyle);
$table->addCell(300, $styleCell)->addText('Kunde', $fontStyle);
$table->addCell(5000, $styleCell)->addText('Strasse', $fontStyle);
$table->addCell(1000, $styleCellBTLR)->addText('Plz', $fontStyle);


$abfrage = "SELECT * FROM vertraege ";
$ergebnis = mysql_query($abfrage);
$menge = mysql_num_rows($ergebnis);
echo "Anzahl der Zeilen $menge <br>";
while($row = mysql_fetch_array($ergebnis))
   {
       $i=1;
   $table->addRow();
    $table->addCell(100)->addText("$row[Id]");
    $table->addCell(5000)->addText("$row[Titel]");
    $table->addCell(2000)->addText("$row[Kunde]");
    $table->addCell(2000)->addText("$row[Strasse]");
   
    $text = ($i % 2 == 0) ? '$row[Ort]' : '';
    $table->addCell(500)->addText($text);
  $i++;         
   }



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