【问题标题】:I can see the array outside function, but no inside the function, how pass variable inside function?我可以看到函数外部的数组,但函数内部没有,如何在函数内部传递变量?
【发布时间】:2018-06-21 14:13:35
【问题描述】:

对不起,英语不是我的母语。

我正在尝试将数组传递给函数。该数组包含确定的三个月的三个月。这段代码是关于FPDF的,我在做PDF报告。

这是代码:

<?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);


    }

此代码正在接收数组。我可以毫无问题地回显变量$mes,我可以看到数据,当我放入function TableHeader时问题就来了。我尝试var_dumpecho 变量$mesvar_dump 给我null 和echo 什么都不做。就像我将数组放入函数中时缺少数据一样。

我正在尝试不同的方法在函数内部传递变量$mes,也许我做得不好。

你觉得我能做什么?

【问题讨论】:

  • 参见:variable scope ... mysql_ 扩展名也已失效 - 不要使用它。
  • @CD001 我现在就去读!是的,我将使用 mysqli。谢谢

标签: php mysql pdf session-variables fpdf


【解决方案1】:

确保数组在类中可用。除非真的有必要,否则不要使用全局变量。在您的类中,添加所需的属性和方法:

class Anexo03 extends FPDF {

    public $mes;

    public function setMes($mes)
    {
        $this->mes = $mes;
    }

确保数组在类中可用。

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

在 TableHeader 中,使用 $this->mes 而不是 $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);

【讨论】:

    【解决方案2】:

    在你的函数中使用 global $mes 然后回显变量

    【讨论】:

    • 我想我需要更好地了解如何在谷歌上寻求帮助。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2012-10-14
    • 2018-05-11
    • 2017-06-01
    • 2022-11-27
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    相关资源
    最近更新 更多