Reputation: 5101
I am trying to get a PDF document from PHP. So far it is working fine, but now I am fighting at this point:
while($row = mysqli_fetch_array($result))
{
$nombre_plato ="";
$nombre_plato .= " ".recuperar_nombre_plato($row['plato_pedido'])."";
$pdf->writeHTML($nombre_plato, true, 0, true, 0);
}
recuperar_nombre_plato($variable) is a function called from another file.
If I write:
$nombre_plato .= " ".$row['plato_pedido']."";
then, the output is as expected ($row['plato_pedido']=1), there are 4 rows:
But using the desired code
$nombre_plato .= " ".recuperar_nombre_plato($row['plato_pedido'])."";
the output is a blank space, no output. The function works fine on other files.The output string is shown on other files, but not here.
Any help is welcome...
EDIT:
function recuperar_nombre_plato ($recibido) {
/*** mysql hostname ***/
$hostname = 'XXX';
/*** mysql username ***/
$username = 'XXX';
/*** mysql password ***/
$password = 'XXX';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=XXX", $username, $password);
/*** echo a message saying we have connected ***/
$sql = "SELECT * FROM tb_platos WHERE id_plato = '".$recibido."'";
foreach ($dbh->query($sql) as $row)
{
return $row['nombre_plato'];
}
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
COMPLETE SOURCE CODE:
<?php session_start();
include "funciones.php";
include "definiciones.php";
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Dario Balas');
$pdf->SetTitle('PDF');
$pdf->SetSubject('PDF');
$pdf->SetKeywords('PDF');
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
//set some language-dependent strings
$pdf->setLanguageArray($l);
// ---------------------------------------------------------
// set default font subsetting mode
$pdf->setFontSubsetting(true);
// Set font
// dejavusans is a UTF-8 Unicode font, if you only need to
// print standard ASCII chars, you can use core fonts like
// helvetica or times to reduce file size.
$pdf->SetFont('helvetica', 'B', 12);
// Add a page
// This method has several options, check the source code documentation for more information.
$pdf->AddPage();
// Set some content to print
$pdf->Image('imagenes/logo_restaurante.gif',20, 20, 20, 25, '', '', 'T', false, 72, '', false, false, 1, false, false, false);
$pdf->SetXY(20, 42);
$pdf->writeHTML($nombre_restaurante,'<br>');
$pdf->SetXY(20, 47);
$pdf->writeHTML($direccion_restaurante,'<br>');
$pdf->SetXY(20, 52);
$pdf->writeHTML($ciudad_restaurante,'<br>');
$pdf->SetXY(130, 12);
$pdf->writeHTML("Orden ".$_SESSION['pedido'],'<br>');
$pdf->SetFont('helvetica', 'B', 12);
$pdf->SetXY(130, 17);
$pdf->writeHTML("Fecha/Hora inicio: ",'<br>');
$pdf->SetFont('helvetica', '', 12);
$pdf->SetXY(130, 22);
$pdf->writeHTML(recuperar_fecha_pedido($_SESSION['pedido']),'<br>');
$pdf->SetFont('helvetica', 'B', 12);
$pdf->SetXY(130, 27);
$pdf->writeHTML("Le atendió: ",'<br>');
$pdf->SetXY(130, 32);
$pdf->SetFont('helvetica', '', 12);
$pdf->writeHTML(recuperar_nombre_usuario(recuperar_atendio_pedido($_SESSION['pedido'])),'<br>');
$pdf->SetFont('helvetica', 'B', 12);
$pdf->SetXY(130, 37);
$pdf->writeHTML("Mesa: ",'<br>');
$pdf->SetFont('helvetica', '', 12);
$pdf->SetXY(130, 42);
$pdf->writeHTML(recuperar_nombre_mesa(recuperar_mesa_pedido($_SESSION['pedido'])),'<br>');
$con=mysqli_connect("localhost","XXX","XXX","XXX");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$pdf->SetXY(15, 72);
$x= 72;
$nombre_plato ="";
$result = mysqli_query($con,"SELECT * FROM tb_lineas_comanda WHERE id_pedido = '".$_SESSION['pedido']."'");
while($row = mysqli_fetch_array($result))
{
$nombre_plato .= " ".recuperar_nombre_plato($row['plato_pedido'])."";
}
$pdf->writeHTML($nombre_plato, true, 0, true, 0);
// ---------------------------------------------------------
// Close and output PDF document
// This method has several options, check the source code documentation for more information.
$pdf->Output('', 'I');
//============================================================+
// END OF FILE
//============================================================+
?>
Upvotes: 0
Views: 340
Reputation: 94672
You cannot return more than one thing from a function, so a return statement in a loop makes no sense. So build a temporary string in this loop and then return that string.
function recuperar_nombre_plato ($recibido) {
$hostname = 'XXX';
$username = 'XXX';
$password = 'XXX';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=XXX", $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM tb_platos WHERE id_plato = '".$recibido."'";
$str = '';
foreach ($dbh->query($sql) as $row) {
$str .= $row['nombre_plato'];
}
return $str;
// nothing happens after a return so this will never get run
//$dbh = null;
}
catch(PDOException $e) {
echo $e->getMessage();
}
}
Or if the query can only return one row because id_plato
is a unique key then change the code to reflect that fact.
function recuperar_nombre_plato ($recibido) {
$hostname = 'XXX';
$username = 'XXX';
$password = 'XXX';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=XXX", $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM tb_platos WHERE id_plato = '".$recibido."'";
$stmt = $dbh->query($sql);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
return $row['nombre_plato'];
// nothing happens after a return so this will never get run
//$dbh = null;
}
catch(PDOException $e) {
echo $e->getMessage();
}
}
Also in your main function as you are building a text string from multiple calls to recuperar_nombre_plato()
do you not want to move the call to $pdf->writeHTML
outside the loop. And the initialization of $nombre_plato
before the loop like this :-
$nombre_plato ="";
while($row = mysqli_fetch_array($result)) {
$nombre_plato .= " ".recuperar_nombre_plato($row['plato_pedido']);
}
$pdf->writeHTML($nombre_plato, true, 0, true, 0);
Upvotes: 2