title: PHPWord fill .docx template with mysql table data
body: <p>Thanks for visit my post i'm newbie in php programming and currently i'm trying to make a web application that allows the user to make gas coupons.
  This is possible thanks to multiple web forms and dynamic options list that store the data in a mysql database. What i'm trying to do is taking the information that will be located into each coupon from the database and replace the variables that i defined into the word template.</p>
  
  <p>Actually the applications allows the user to make one coupon (What it does is take the information from the forms and lists and witha POST method it replace the variables in the php code.)</p>
  
  <p>I really hope somebody could help me because thats all i need to finish my application</p>
  
  <p><a href="http://postimg.org/image/w78s4ca3h/" rel="nofollow">Here's a picture of the coupons</a> </p>
  
  <p>Here are my php and html codes.</p>
  
  <p>vales.html</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Creaci&amp;oacute;n de Vales&lt;/title&gt;
  &lt;script type="text/javascript" src="dynamicoptionlist.js"&gt;&lt;/script&gt;
  &lt;link rel="icon" type="image/png" href="favicon.png"&gt;
  &lt;link rel="stylesheet" href="estilos.css"&gt;
  &lt;/head&gt;
  &lt;body onLoad="initDynamicOptionLists()"&gt;
  &lt;header&gt;
  &lt;div class="menu"&gt;
      &lt;div class="contenedor"&gt;
          &lt;div class="logo"&gt;
              &lt;a href="index.html"&gt;Creaci&amp;oacute;n de vales&lt;/a&gt;
          &lt;/div&gt;
          &lt;nav&gt;
          &lt;ul&gt;
          &lt;li&gt;&lt;a href="index.html"&gt;Inicio&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href="altas.html"&gt;Creaci&amp;oacute;n de vales&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href="../graph2/index.php"&gt;Consulta de gr&amp;aacute;ficas&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href="extras.html"&gt;Vales Extras&lt;/a&gt;&lt;/li&gt;
          &lt;/ul&gt;
          &lt;/nav&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;/header&gt;
  &lt;center&gt;
      &lt;div class="Shield"&gt;
          &lt;img src="shield.png" width="150px" height="150px"&gt;
      &lt;/div&gt;
  &lt;div class="CV"&gt;
      &lt;h1&gt;Creaci&amp;oacute;n de Vales&lt;/h1&gt;
  &lt;/div&gt;
  &lt;div class="formu"&gt;
  &lt;form action="template.php" method="post"&gt;
  &lt;b&gt;Nombre:&amp;nbsp;Presidencia Municipal&lt;/b&gt;&lt;br&gt;&lt;br&gt;
  &lt;b&gt;Departamento:&lt;/b&gt;
  &lt;select name="dpto"&gt;
  &lt;!--&lt;option value="Default"&gt;&lt;/option&gt;--&gt;
  &lt;option value="Seguridad"&gt;Seguridad&lt;/option&gt;
  &lt;option value="Servicios Primarios"&gt;Servicios Primarios&lt;/option&gt;
  &lt;option value="Ecologia"&gt;Ecolog&amp;iacute;a&lt;/option&gt;
  &lt;option value="Desarrollo Rural"&gt;Desarrollo Rural&lt;/option&gt;
  &lt;option value="Desarrollo Social"&gt;Desarrollo Social&lt;/option&gt;
  &lt;option value="Presidencia"&gt;Presidencia&lt;/option&gt;
  &lt;option value="Fomento Civico"&gt;Fomento Civico&lt;/option&gt;
  &lt;option value="Regidores"&gt;Regidores&lt;/option&gt;
  &lt;option value="Recursos Humanos"&gt;Recursos Humanos&lt;/option&gt;
  &lt;option value="Tesoreria"&gt;Tesorer&amp;iacute;a&lt;/option&gt;
  &lt;option value="Gestoria"&gt;Gestor&amp;iacute;a&lt;/option&gt;
  &lt;option value="Parques y Jardines"&gt;Parques y Jard&amp;iacute;nes&lt;/option&gt;
  &lt;option value="Contraloria"&gt;Contralor&amp;iacute;a&lt;/option&gt;
  &lt;option value="Alumbrado Publico"&gt;Alumbrado P&amp;uacute;blico&lt;/option&gt;
  &lt;option value="Juridico"&gt;Jur&amp;iacute;dico&lt;/option&gt;
  &lt;option value="Obras Publicas"&gt;Obras P&amp;uacute;blicas&lt;/option&gt;
  &lt;option value="Comunicacion Social"&gt;Comuniaci&amp;oacute;n Social&lt;/option&gt;
  &lt;option value="DIF"&gt;DIF&lt;/option&gt;
  &lt;option value="Biblioteca"&gt;Biblioteca&lt;/option&gt;
  &lt;option value="Secretaria del Ayuntamiento"&gt;Secretar&amp;iacute;a del Ayuntamiento&lt;/option&gt;
  &lt;option value="Proteccion Civil"&gt;Protecci&amp;oacute;n Civil&lt;/option&gt;
  &lt;option value="Bomberos"&gt;Bomberos&lt;/option&gt;
  &lt;option value="Turismo Municipal"&gt;Turismo Municipal&lt;/option&gt;
  &lt;option value="Union de Pensionados"&gt;Uni&amp;oacute;n de Pensionados&lt;/option&gt;
  &lt;option value="SEDENA Ejercito Mexicano"&gt;SEDENA Ejercito Mexicano&lt;/option&gt;
  &lt;option value="Instituto de la Juventud"&gt;Instituto de la Juventud&lt;/option&gt;
  &lt;option value="Centro de Salud"&gt;Centro de Salud&lt;/option&gt;
  &lt;option value="Apoyo Cam 10"&gt;Apoyo Cam 10&lt;/option&gt;
  &lt;option value="Apoyo estudiantes Ejido La Cecilia CBTA"&gt;Apoyo estudiantes Ejido La Cecilia CBTA&lt;/option&gt;
  &lt;option value="Apoyo Atencion Medica Oficial de Seguridad"&gt;Apoyo Atenci&amp;oacute;n M&amp;eacute;dica Oficial de Seguridad&lt;/option&gt;
  &lt;option value="Apoyo EMSAD 28 de Agosto"&gt;Apoyo EMSAD 28 de Agosto&lt;/option&gt;
  &lt;option value="Apoyo estudiantes ejido el Durazno"&gt;Apoyo estudiantes ejido el Durazno&lt;/option&gt;
  &lt;option value="Apoyo estudiantes ejido Ganivete"&gt;Apoyo estudiantes ejido Ganivete&lt;/option&gt;
  &lt;option value="Apoyo estudiantes ejido Estacion Madero"&gt;Apoyo estudiantes ejido Estaci&amp;oacute;n Madero&lt;/option&gt;
  &lt;/select&gt;&lt;br&gt;&lt;br&gt;
  &lt;b&gt;Responsable:&lt;/b&gt;
  &lt;select name="rsp"&gt;
  &lt;/select&gt;&lt;br&gt;&lt;br&gt;
  &lt;b&gt;Fecha:&lt;/b&gt;&lt;br&gt;&lt;br&gt;
  D&amp;iacute;a:
  &lt;input type="text" size="5" placeholder="15" name="dia"&gt;&amp;nbsp;&amp;nbsp;
  Mes:
  &lt;input type="text" size="5" placeholder="9" name="mes"&gt;&amp;nbsp;&amp;nbsp;
  A&amp;ntilde;o
  &lt;input type="text" size="5" placeholder="2015" name="year"&gt;&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;
  &lt;b&gt;Cantidad:&lt;/b&gt;
  &lt;input type="text" size="5" placeholder="40" name="cantn"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
  &lt;b&gt;Cantidad Letra:&lt;/b&gt;
  &lt;input type="text" size="12" placeholder="Cuarenta" name="cantl"&gt;&lt;br&gt;&lt;br&gt;
  &lt;b&gt;Art&amp;iacute;culo:&lt;/b&gt;&amp;nbsp;&amp;nbsp;&lt;i&gt;Litros&lt;/i&gt;&amp;nbsp;&amp;nbsp;
  &lt;b&gt;Importe:&lt;/b&gt;
  &lt;select name="imp"&gt;
      &lt;option&gt;Magna
      &lt;option&gt;Premium
      &lt;option&gt;Diesel
  &lt;/select&gt;&amp;nbsp;&amp;nbsp;
  &lt;b&gt;Precio:&lt;b&gt;
  &lt;select name="precio"&gt;
  &lt;/select&gt;&lt;br&gt;&lt;br&gt;
  &lt;b&gt;Seleccione o ingrese Unidad:&lt;/b&gt;&lt;br&gt;&lt;br&gt;
  &lt;select name="uni"&gt;
  &lt;/select&gt;&lt;br&gt;&lt;br&gt;
  &lt;input type="text" size="25" placeholder="Camion de pasajeros" name="uni2"&gt;&lt;br&gt;&lt;br&gt;
  &lt;b&gt;Seleccione o ingrese placas:&lt;/b&gt;&lt;br&gt;&lt;br&gt;
  &lt;select name="placas"&gt;
  &lt;/select&gt;&lt;br&gt;&lt;br&gt;
  &lt;input type="text" size="12" placeholder="EXO9843" name="placas2"&gt;&lt;br&gt;&lt;br&gt;
  &lt;input type="submit" value="Registrar"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
  &lt;input type="reset" value="Limpiar Formularios"&gt;
  &lt;/div&gt;
  //This is the code for the dynamicoptions select inputs
  &lt;script type="text/javascript"&gt;
  var names = new DynamicOptionList();
  var precio = new DynamicOptionList();
  
  precio.addDependentFields("imp","precio");
  
  precio.forValue("Magna").addOptions("13.57");
  precio.forValue("Premium").addOptions("14.50");
  precio.forValue("Diesel").addOptions("14.2");
  
  names.addDependentFields("dpto","rsp","uni","placas");
  names.forValue("Seguridad").addOptions("Lic. xxxxxx xxxxx xxxx","xxxxx xxxxxxx","xxxxx xxxxx xxxxx","xxxxx xxxxx xxxxx","xxxxx xxxxx xxxxx","xxxxx xxxxx xxxxx","xxxxx xxxxx xxxxx","xxxxx xxxxx xxxxx","xxxxx xxxxx xxxxx","xxxxx xxxxx xxxxx","xxxxx xxxxx xxxxx","xxxxx xxxxx xxxxx");
  names.forValue("Servicios Primarios").addOptions("xxxxx xxxxx xxxxx");
  names.forValue("Ecologia").addOptions("xxxxx xxxxx xxxxx","xxxxx xxxxx xxxxx");
  names.forValue("Desarrollo Rural").addOptions("xxxxx xxxxx xxxxx");
  names.forValue("Desarrollo Social").addOptions("xxxxx xxxxx xxxxx");
  names.forValue("Presidencia").addOptions("xxxxx xxxxx xxxxx","xxxxx xxxxx xxxxx","xxxxx xxxxx xxxxx","xxxxx xxxxx xxxxx","xxxxx xxxxx xxxxx");
  names.forValue("Fomento Civico").addOptions("xxxxx xxxxx xxxxx","xxxxx xxxxx xxxxx","xxxxx xxxxx xxxxx");
  
  &lt;/script&gt;
  </code></pre>
  
  <p>template.php</p>
  
  <pre><code>&lt;?php
  require_once dirname(__FILE__).'/PHPWord-master/src/PhpWord/Autoloader.php';
  \PhpOffice\PhpWord\Autoloader::register();
  
  use PhpOffice\PhpWord\TemplateProcessor;
  
  
  $total = $_REQUEST[cantn] * $_REQUEST[precio];
  
  
  $conexion=mysql_connect("localhost","root","") or
    die("Problemas en la conexion");
  mysql_select_db("presi",$conexion) or
    die("Problemas en la seleccion de la base de datos");
  mysql_query("insert into vales(dpto,rsp,cantn,imp,precio,uni,placas,placas2) values ('$_REQUEST[dpto]','$_REQUEST[rsp]','$_REQUEST[cantn]','$_REQUEST[imp]','$total','$_REQUEST[uni]','$_REQUEST[placas]','$_REQUEST[placas2]')", $conexion) or
    die("Problemas en el select".mysql_error());
  mysql_close($conexion);
  
  $templateWord = new TemplateProcessor('plantilla.docx');
  
  //$nombre = $_REQUEST['nombre'];
  $folio = $_GET['id'];
  $departamento = $_REQUEST['dpto'];
  $responsable = $_REQUEST['rsp'];
  $dia = $_REQUEST['dia'];
  $mes = $_REQUEST['mes'];
  $year = $_REQUEST['year'];
  $cantn = $_REQUEST['cantn'];
  $cantl = $_REQUEST['cantl'];
  //$articulo = $_REQUEST['articulo'];
  $importe = $_REQUEST['imp'];
  $unidad = $_REQUEST['uni'];
  $placas = $_REQUEST['placas'];
  
  
  // --- Asignamos valores a la plantilla
  //$templateWord-&gt;setValue('nombre',$nombre);
  $templateWord-&gt;setValue('folio',$folio);
  $templateWord-&gt;setValue('departamento',$departamento);
  $templateWord-&gt;setValue('responsable',$responsable);
  $templateWord-&gt;setValue('dia',$dia);
  $templateWord-&gt;setValue('mes',$mes);
  $templateWord-&gt;setValue('year',$year);
  $templateWord-&gt;setValue('cantn',$cantn);
  $templateWord-&gt;setValue('cantl',$cantl);
  //$templateWord-&gt;setValue('articulo',$articulo);
  $templateWord-&gt;setValue('importe',$importe);
  $templateWord-&gt;setValue('unidad',$unidad);
  $templateWord-&gt;setValue('placas',$placas);
  
  
  // --- Guardamos el documento
  ob_clean();
  $templateWord-&gt;saveAs('Vales.docx');
  
  header("Content-Disposition: attachment; filename=Vales.docx; charset=iso-8859-1");
  echo file_get_contents('Vales.docx');
  ?&gt;
  </code></pre>
  
  <p>Thaks for the time and sorry for my bad english.</p>