0

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?

rawathemant
  • 744
  • 1
  • 4
  • 16
Ivan J.
  • 87
  • 1
  • 1
  • 8
  • See: [variable scope](http://php.net/manual/en/language.variables.scope.php) ... also the `mysql_` extension is defunct - don't use it. – CD001 Jun 21 '18 at 14:16
  • @CD001 i will read right now! Yes, i will use mysqli. Thank you – Ivan J. Jun 21 '18 at 14:22

2 Answers2

2

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);
Terry
  • 304
  • 1
  • 7
1

Use global $mes inside your function then echo the variable