Global variable does not work php (Empty)

0

I have the following code where I assign the value of a variable with POST and I try to show it in a function:

   <?php


$orden=$_POST['dato'];

$tama=$_POST['size'];
for($i=0;$i<$tama;$i++){
   $titulo[$i] = $_POST['titulo'.$i];
}


ob_end_clean(); //    the buffer and never prints or returns anything.
ob_start(); // it starts buffering
class PDF extends FPDF
{
// Cabecera de página



function Header()
{
    global $ordenTrabajo;
    print_r($ordenTrabajo);
    global $titulo;
    // Logo
    //$this->Image('../dist/img/logo.jpg',10,8,33);
    // Arial bold 15
    $this->SetFont('Arial','B',15);
    // Movernos a la derecha
    $this->Cell(80);
    // Título
    $this->Cell(120,10,'Control de Empaques IKOR PUNTARENAS SA',0,1,'C');

    $this->SetFont('Arial','B',12);
    // Salto de línea
    $this->Ln(10);
    $this->SetTextColor(0,0,0);
    $this->Cell(40,10,'Orden de Trabajo: ',0,0);
    $this->Cell(40, 10, $orden, 0, 1);

}

// Pie de página
function Footer()
{
    // Posición: a 1,5 cm del final
    $this->SetY(-15);
    // Arial italic 8
    $this->SetFont('Arial','I',8);
    // Número de página
    //$this->Cell(300,10,'7FMIKO-022 REV.02',0,0,'C');
    $this->Cell(100,10,date('d/m/Y'),0,0,'L');
    $this->Cell(70,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
    $this->Cell(200,10,'7FMIKO-022 REV.02',0,0,'C');
}
}

// Creación del objeto de la clase heredada
$pdf = new PDF('L');
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times','',12);
for($j=0;$j<$tama;$j++){
    $pdf->SetFont('Arial', 'B');
    $pdf->MultiCell(0,10,$titulo[$j],0,1);
}
$pdf->Output();
ob_end_flush(); // It's printed here, because ob_end_flush "prints" what's in
              // the buffer, rather than returning it
              //     (unlike the ob_get_* functions)
?>

The problem is that no matter how much I use global or $GLOBALS['orden'] , the field comes out empty, if I use the data outside the function, if it shows me well, how can I show that data correctly?

Thank you.

    
asked by Baker1562 25.02.2018 в 18:38
source

1 answer

0

I solved the problem by declaring the variables as global, before assigning them the value.

<?php

global $oT;
global $oC;

$ot=$_POST['algo'];

function x(){

   global $oT;
   //Se realiza lo que se tenga que hacer

}

?>
    
answered by 25.02.2018 / 20:57
source