PHPWord用mysql表数据填充.docx模板

时间:2015-07-27 14:18:31

标签: php mysql while-loop phpword

感谢您访问我的帖子我是PHP编程的新手,目前我正在尝试制作一个允许用户制作燃气优惠券的网络应用程序。 这可以归功于多个Web表单和动态选项列表,它们将数据存储在mysql数据库中。我要做的是从数据库中获取每个优惠券中的信息,并将我定义的变量替换为单词模板。

实际上,应用程序允许用户制作一张优惠券(它的作用是从表格和列表中获取信息,而POST方法则替换PHP代码中的变量。)

我真的希望有人可以帮助我,因为我只需要完成我的申请

Here's a picture of the coupons

这是我的php和html代码。

vales.html

<html>
<head>
<title>Creaci&oacute;n de Vales</title>
<script type="text/javascript" src="dynamicoptionlist.js"></script>
<link rel="icon" type="image/png" href="favicon.png">
<link rel="stylesheet" href="estilos.css">
</head>
<body onLoad="initDynamicOptionLists()">
<header>
<div class="menu">
    <div class="contenedor">
        <div class="logo">
            <a href="index.html">Creaci&oacute;n de vales</a>
        </div>
        <nav>
        <ul>
        <li><a href="index.html">Inicio</a></li>
        <li><a href="altas.html">Creaci&oacute;n de vales</a></li>
        <li><a href="../graph2/index.php">Consulta de gr&aacute;ficas</a></li>
        <li><a href="extras.html">Vales Extras</a></li>
        </ul>
        </nav>
    </div>
</div>
</header>
<center>
    <div class="Shield">
        <img src="shield.png" width="150px" height="150px">
    </div>
<div class="CV">
    <h1>Creaci&oacute;n de Vales</h1>
</div>
<div class="formu">
<form action="template.php" method="post">
<b>Nombre:&nbsp;Presidencia Municipal</b><br><br>
<b>Departamento:</b>
<select name="dpto">
<!--<option value="Default"></option>-->
<option value="Seguridad">Seguridad</option>
<option value="Servicios Primarios">Servicios Primarios</option>
<option value="Ecologia">Ecolog&iacute;a</option>
<option value="Desarrollo Rural">Desarrollo Rural</option>
<option value="Desarrollo Social">Desarrollo Social</option>
<option value="Presidencia">Presidencia</option>
<option value="Fomento Civico">Fomento Civico</option>
<option value="Regidores">Regidores</option>
<option value="Recursos Humanos">Recursos Humanos</option>
<option value="Tesoreria">Tesorer&iacute;a</option>
<option value="Gestoria">Gestor&iacute;a</option>
<option value="Parques y Jardines">Parques y Jard&iacute;nes</option>
<option value="Contraloria">Contralor&iacute;a</option>
<option value="Alumbrado Publico">Alumbrado P&uacute;blico</option>
<option value="Juridico">Jur&iacute;dico</option>
<option value="Obras Publicas">Obras P&uacute;blicas</option>
<option value="Comunicacion Social">Comuniaci&oacute;n Social</option>
<option value="DIF">DIF</option>
<option value="Biblioteca">Biblioteca</option>
<option value="Secretaria del Ayuntamiento">Secretar&iacute;a del Ayuntamiento</option>
<option value="Proteccion Civil">Protecci&oacute;n Civil</option>
<option value="Bomberos">Bomberos</option>
<option value="Turismo Municipal">Turismo Municipal</option>
<option value="Union de Pensionados">Uni&oacute;n de Pensionados</option>
<option value="SEDENA Ejercito Mexicano">SEDENA Ejercito Mexicano</option>
<option value="Instituto de la Juventud">Instituto de la Juventud</option>
<option value="Centro de Salud">Centro de Salud</option>
<option value="Apoyo Cam 10">Apoyo Cam 10</option>
<option value="Apoyo estudiantes Ejido La Cecilia CBTA">Apoyo estudiantes Ejido La Cecilia CBTA</option>
<option value="Apoyo Atencion Medica Oficial de Seguridad">Apoyo Atenci&oacute;n M&eacute;dica Oficial de Seguridad</option>
<option value="Apoyo EMSAD 28 de Agosto">Apoyo EMSAD 28 de Agosto</option>
<option value="Apoyo estudiantes ejido el Durazno">Apoyo estudiantes ejido el Durazno</option>
<option value="Apoyo estudiantes ejido Ganivete">Apoyo estudiantes ejido Ganivete</option>
<option value="Apoyo estudiantes ejido Estacion Madero">Apoyo estudiantes ejido Estaci&oacute;n Madero</option>
</select><br><br>
<b>Responsable:</b>
<select name="rsp">
</select><br><br>
<b>Fecha:</b><br><br>
D&iacute;a:
<input type="text" size="5" placeholder="15" name="dia">&nbsp;&nbsp;
Mes:
<input type="text" size="5" placeholder="9" name="mes">&nbsp;&nbsp;
A&ntilde;o
<input type="text" size="5" placeholder="2015" name="year"><br><br>&nbsp;&nbsp;
<b>Cantidad:</b>
<input type="text" size="5" placeholder="40" name="cantn">&nbsp;&nbsp;&nbsp;&nbsp;
<b>Cantidad Letra:</b>
<input type="text" size="12" placeholder="Cuarenta" name="cantl"><br><br>
<b>Art&iacute;culo:</b>&nbsp;&nbsp;<i>Litros</i>&nbsp;&nbsp;
<b>Importe:</b>
<select name="imp">
    <option>Magna
    <option>Premium
    <option>Diesel
</select>&nbsp;&nbsp;
<b>Precio:<b>
<select name="precio">
</select><br><br>
<b>Seleccione o ingrese Unidad:</b><br><br>
<select name="uni">
</select><br><br>
<input type="text" size="25" placeholder="Camion de pasajeros" name="uni2"><br><br>
<b>Seleccione o ingrese placas:</b><br><br>
<select name="placas">
</select><br><br>
<input type="text" size="12" placeholder="EXO9843" name="placas2"><br><br>
<input type="submit" value="Registrar">&nbsp;&nbsp;&nbsp;
<input type="reset" value="Limpiar Formularios">
</div>
//This is the code for the dynamicoptions select inputs
<script type="text/javascript">
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");

</script>

的template.php

<?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->setValue('nombre',$nombre);
$templateWord->setValue('folio',$folio);
$templateWord->setValue('departamento',$departamento);
$templateWord->setValue('responsable',$responsable);
$templateWord->setValue('dia',$dia);
$templateWord->setValue('mes',$mes);
$templateWord->setValue('year',$year);
$templateWord->setValue('cantn',$cantn);
$templateWord->setValue('cantl',$cantl);
//$templateWord->setValue('articulo',$articulo);
$templateWord->setValue('importe',$importe);
$templateWord->setValue('unidad',$unidad);
$templateWord->setValue('placas',$placas);


// --- Guardamos el documento
ob_clean();
$templateWord->saveAs('Vales.docx');

header("Content-Disposition: attachment; filename=Vales.docx; charset=iso-8859-1");
echo file_get_contents('Vales.docx');
?>

当时的时间,并抱歉我的英语不好。

1 个答案:

答案 0 :(得分:1)

好的 - 如果我这次正确理解了这个问题,即你需要将所有优惠券打印成单个文件文件(?)如果你想为每个优惠券创建多个word文件,那么你只需要获取所有优惠券db数据并使用模板创建单个单词文件(正如您已经在做的那样),将所有创建的优惠券文件存储在服务器中的某个位置,将文件打包成单个文件(zip或其他)并将该文件提供给用户。

因此,对于在单个单词文件中创建优惠券的情况:

首先,您需要稍微修改单词模板:在您希望为每个优惠券克隆的区域周围添加所需的模板标签(在此示例中命名为CLONEME的开始和结束标记),例如:

${CLONEME}
Here is your normal single coupon template fields, i.e.
${folio}
${departamento}
...
$(/CLONEME}

您可以使用以下内容处理数据插入到Word模板中:

// first fetch your coupons from the db
$result = mysql_query("SELECT * FROM vales", $conexion);
if(!$result) 
{
    die("Database query failed: " . mysql_error());
}

// then get your template and clone the coupon block for each of the results
$templateWord = new TemplateProcessor('plantilla.docx');
$templateWord->cloneBlock('CLONEME', mysql_num_rows($result));

// loop over your results and fill the template values
while ($row = mysql_fetch_array($result))
{
    // NOTE! add the 1 parameter to each setValue so that it replaces only
    // one template field at time (after cloning the block the template
    // contains several same named template fields - for each coupon that is)
    $templateWord->setValue("departamento", $row["dpto"], 1);
    // ... similarly the other values...
}

// and last, store the word result etc