Search code examples
phpmysqlpdfsession-variablesfpdf

I can see the array outside function, but no inside the function, how pass variable inside function?


Sorry for english, not my native language.

I am trying to pass an array into a function. This array contains the three months of a determinate trimester. This code is about FPDF, I am doing a PDF report.

This is the code:

<?php
include('../../../libreria/engine.php');

require_once('../fpdf.php');

$mes = $_GET["txtMes"];
$per = $_GET["txtPer"];

$_SESSION["tmpMes"] = $_SESSION["sqlDataPDF"]["titulo"];

$sql =  $_SESSION["sqlDataPDF"]["textoSQL"];
$mes = $_SESSION["sqlDataPDF"]["meses"];


echo $mes[0];

$rs = mysql_query($sql);

class Anexo03 extends FPDF
{


    var $ProcessingTable;   //Es la propiedad que indica si se esta procesando la tabla, para repetir nuevamente el encabezado.
    var $anchos;       // Son los anchos de las diferentes columnas
    var $left;        //Margen izquierdo inicial
    var $clasificador;



    function Header()
    {                        //0   1    2   3   4
        $this->anchos = array(100, 20, 30);
        $this->left = 15;
        $this->ProcessingTable = true;
        //Print the table header if necessary

    }

    function TableHeader($clasificador='x', $y=38, $c=false)
    {       





        if($c)
        {
            $clasificador = $this->clasificador;
        }

        $this->clasificador = $clasificador;
        $alto = 6;   //Es el alto de la linea
        $this->SetFillColor(209,211,223);

        $this->SetFont('Arial','',10);
        $this->SetXY($this->left,$y);
        $ancho = $this->anchos[0] + $this->anchos[1] + $this->anchos[2] + $this->anchos[3];
        //$this->Cell($ancho,$alto,"Clasificador: $clasificador", 'LRT',0,'L',1);

        //$this->SetXY($this->left,$y+4);
        $this->Cell($this->anchos[0],$alto,"Clasificador", 'LBT',0,'C',1);
        $this->Cell($this->anchos[1],$alto,'$mes[0]', 'RBT',0,'C',1);
        $this->Cell($this->anchos[1],$alto,'$mes[1]', 'RBT',0,'C',1);
        $this->Cell($this->anchos[1],$alto,'$mes[2]', 'RBT',0,'C',1);
        $this->Cell($this->anchos[2],$alto,"Total Trimestral", 'RBT',0,'C',1);


    }

This code is receiving the array. I can echo the variable $mes without problem, I can see the data, the problem comes when I put inside function TableHeader. I try to var_dump and echo the variable $mes but var_dump give me null and echo doesnt do nothing. Is like the data is missing when I put the array inside the function.

I was trying different ways to pass the variable $mes inside function, maybe I didnt do good.

What do you think can I do?


Solution

  • Make sure to make the array available in the class. Do not use a global unless really necessary. Inside your class, add the required property and method:

    class Anexo03 extends FPDF {
    
        public $mes;
    
        public function setMes($mes)
        {
            $this->mes = $mes;
        }
    

    Make sure to make the array available in the class.

    $mes = $_GET["txtMes"];
    $pdf = new Anexo03();
    $pdf->setMes($mes);
    

    In TableHeader, use $this->mes instead of $mes.

    $this->Cell($this->anchos[1], $alto, $this->mes[0], 'RBT', 0, 'C', 1);
    $this->Cell($this->anchos[1], $alto, $this->mes[1], 'RBT', 0, 'C', 1);
    $this->Cell($this->anchos[1], $alto, $this->mes[2], 'RBT', 0, 'C', 1);