Tutorial 6 : Repeat the line of a table - neeftarah/odtphp GitHub Wiki

The purpose of this tutorial is to show how to repeat a line of table with odtPHP segment. We can do that by the same way as handling ordinary segments, except that the tags in our template must be prefixed by "row.".

The PHP code

<?php
require_once('../library/odf.php');

$odf = new odf("tutoriel6.odt");

$odf->setVars('titre', 'Quelques articles de l\'encyclopédie Wikipédia');

$message = "La force de cette encyclopédie en ligne réside...";

$odf->setVars('message', $message);

$listeArticles = array(
    array('titre' => 'PHP',
          'texte' => 'PHP est un langage de scripts (...)',
    ),
    array('titre' => 'MySQL',
          'texte' => 'MySQL est un système de gestion de base de données (...)',
    ),
    array('titre' => 'Apache',
          'texte' => 'Apache HTTP Server, souvent appelé Apache (...)',
    ),        
);

$article = $odf->setSegment('articles');
foreach($listeArticles AS $element) {
    $article->titreArticle($element['titre']);
    $article->texteArticle($element['texte']);
    $article->merge();
}
$odf->mergeSegment($article);

$odf->exportAsAttachedFile();
 
?>
  1. We create an Odf object by specifying the path to our template. Then, we call two times the method setVars() of the created object in order to replace {titre} and {message} tags by text content.

  2. For the second step, we initialize in an array a data set that we want to fully display. The array provides a list of articles consisting of a title and a text.

  3. We initialize the segment "articles" with a call to the method setSegment() which takes as argument the name of the segment (without the prefix "row.") and returns an object of type Segment.

  4. Then, We can loop on our data array. At each loop, we replace the tags {titreArticle} and {texteArticle} of segment by content of the current line of the array. For that, we directly access to the methods titreArticle() and texteArticle() of the object $article. You can also perform this replacement in a classic way by calling the method setVar() of the object $article.

  5. Eventually, when we have added all our desired data, we can add the segment to the document by calling the method mergeSegment() of the object $odf which takes as parameter our segment $article. The OpenOffice model (tutoriel6.odt) tutorial odtPHP

To run this example, copy this template within an OpenOffice document named "tutoriel6.odt". This template requires to create a table under OpenOffice. Nmaes of segments within a line of array must be prefixed by "row." in order to be handled correctly.

The result

tutorial odtPHP

Note : the texts in the PHP code are shorter than the result in order to make it more readable. Styles (colors, layouts) of this result come from our template.