Test Foro de elhacker.net SMF 2.1

Programación => Desarrollo Web => PHP => Mensaje iniciado por: geshiro en 22 Enero 2016, 00:48 AM

Título: php variable indefinida
Publicado por: geshiro en 22 Enero 2016, 00:48 AM
hola buenas tardes en mi proyecto instale una libreria para hacer pdfs con php pero al momento de querer declarar la variable me dice variable indefinida

Código (php) [Seleccionar]

<?php
require(
'../fpdf/fpdf.php');
class PDF extends FPDF{
private $conexion;
public function __construct(){
require('conexion.php');
$this->conexion = new conexion();
$this->conexion->conectar();
}
}

$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial'''10);
$pdf->Image('../assests/img/tienda.gif' 10 ,810 13,'GIF');
$pdf->Cell(1810''0);
$pdf->Cell(15010'Store "Aron Imperial"'0);
$pdf->SetFont('Arial'''9);
$pdf->Cell(5010'Hoy: '.date('d-m-Y').''0);
$pdf->Ln(15);
$pdf->SetFont('Arial''B'11);
$pdf->Cell(708''0);
$pdf->Cell(1008'List product'0);
$pdf->Ln(23);
$pdf->SetFont('Arial''B',8);
$pdf->Cell(158'Code'0);
$pdf->Cell(508'Product'0);
$pdf->Cell(258'Price'0);
$pdf->Cell(258'Stock'0);
$pdf->Ln(8);
$pdf->SetFont('Arial','',8);
$record $product->query("SELECT * FROM product");
$pdf->Output();
?>



los errores


Notice: Undefined variable: product in C:\xampp\htdocs\store\fpdf\list_product.php on line 32

Fatal error: Call to a member function query() on null in C:\xampp\htdocs\store\fpdf\list_product.php on line 32
Título: Re: php variable indefinida
Publicado por: apuromafo CLS en 22 Enero 2016, 01:58 AM
aqui hay un ejemplo con sql, cuando invoca esa variable que tienes no definida antes la define conectando a la base de datos
http://phppot.com/php/generate-pdf-from-mysql-data-using-fpdf/

debe ser eso, quizas debas cambiar $this por $product o bien generar una nueva conexión ?
Título: Re: php variable indefinida
Publicado por: geshiro en 22 Enero 2016, 02:29 AM
no puedo cambiarla ya que todo el proyecto trabaja en POO y no puedo cambiarla
Título: Re: php variable indefinida
Publicado por: geshiro en 22 Enero 2016, 02:32 AM
cuando intento hacer esto

Código (php) [Seleccionar]

<?php
require(
'fpdf.php');
class PDF extends FPDF{
private $conexion;
public function __construct(){
require('../models/conexion.php');
$this->conexion = new conexion();
$this->conexion->conectar();
}
}

$pdf = new PDF();
$pdf->AddPage();
$pdf->SetFont('Arial'''10);
$pdf->Image('../assests/img/tienda.gif' 10 ,810 13,'GIF');
$pdf->Cell(1810''0);
$pdf->Cell(15010'Store "Aron Imperial"'0);
$pdf->SetFont('Arial'''9);
$pdf->Cell(5010'Hoy: '.date('d-m-Y').''0);
$pdf->Ln(15);
$pdf->SetFont('Arial''B'11);
$pdf->Cell(708''0);
$pdf->Cell(1008'List product'0);
$pdf->Ln(23);
$pdf->SetFont('Arial''B',8);
$pdf->Cell(158'Code'0);
$pdf->Cell(508'Product'0);
$pdf->Cell(258'Price'0);
$pdf->Cell(258'Stock'0);
$pdf->Ln(8);
$pdf->SetFont('Arial','',8);
$pdf->Output();
?>



me esta esto otro error


Warning: in_array() expects parameter 2 to be array, null given in C:\xampp\htdocs\store\fpdf\fpdf.php on line 501

Fatal error: Uncaught exception 'Exception' with message 'FPDF error: Undefined font: helvetica ' in C:\xampp\htdocs\store\fpdf\fpdf.php:271 Stack trace: #0 C:\xampp\htdocs\store\fpdf\fpdf.php(510): FPDF->Error('Undefined font:...') #1 C:\xampp\htdocs\store\fpdf\list_product.php(14): FPDF->SetFont('Arial', '', 10) #2 {main} thrown in C:\xampp\htdocs\store\fpdf\fpdf.php on line 271
Título: Re: php variable indefinida
Publicado por: geshiro en 22 Enero 2016, 03:32 AM
ahora si me deja acceder pero ahora query no puedo usarlo porque dice que no esta definido Fatal error: Call to undefined method PDF::query() in C:\xampp\htdocs\store\fpdf\list_product.php on line 33

Código (php) [Seleccionar]

<?php
require(
'fpdf.php');
class PDF extends FPDF{
private $conexion;
public function __construct(){
require('../models/conexion.php');
$this->conexion = new conexion();
$this->conexion->conectar();
parent::__construct();
}
}

$pdf = new PDF();
$pdf->AddPage();
$pdf->SetFont('Arial'''10);
$pdf->Image('../assests/img/tienda.gif' 10 ,810 13,'GIF');
$pdf->Cell(1810''0);
$pdf->Cell(15010'Store "Aron Imperial"'0);
$pdf->SetFont('Arial'''9);
$pdf->Cell(5010'Hoy: '.date('d-m-Y').''0);
$pdf->Ln(15);
$pdf->SetFont('Arial''B'11);
$pdf->Cell(708''0);
$pdf->Cell(1008'List product'0);
$pdf->Ln(23);
$pdf->SetFont('Arial''B',8);
$pdf->Cell(158'Code'0);
$pdf->Cell(508'Product'0);
$pdf->Cell(258'Price'0);
$pdf->Cell(258'Stock'0);
$pdf->Ln(8);
$pdf->SetFont('Arial','',8);
$pdf->query("SELECT * FROM product");
$pdf->Output();
?>


Título: Re: php variable indefinida
Publicado por: eLank0 en 22 Enero 2016, 10:31 AM
Te aconsejo que te leas bien los errores antes de continuar. Si pone undefined method es que no existe el método, ya que en este caso debería ser:

Código (php) [Seleccionar]
$sql_statement = "SELECT user.Host, user.User, user.Password, Select_priv as `Select priv`, Insert_priv as `Insert Priv`, Update_priv as `Update priv`, Delete_priv as `Delete priv`, Create_priv as `Create priv`, Reload_priv as `Reload priv`, Shutdown_priv as `Shutdown priv`, Process_priv as `Process priv`, File_priv as `File priv`, Grant_priv as `Grant priv`, References_priv as `References priv`, Index_priv as `Index priv` FROM user LEFT JOIN tables_priv ON user.User=tables_priv.User ORDER BY user.User" ;

// Generate report
$pdf->mysql_report($sql_statement, false, $attr );


/*!!! Very Important: after having done all the work of
  setting up the SQL don't forget to output the PDF else
  you just get a blank page !!!*/

$pdf->Output();


El código está extraido de la web oficial de fPDF.

http://www.fpdf.org/en/script/script21.php

Salu2