导出到excel时未定义变量

时间:2014-09-16 07:42:08

标签: php mysql excel

我正在使用php / mysql站点导出excel功能。当我在文件中有一个set查询时,它可以正常工作,xls文档会立即下载。

我在使用自定义php变量时遇到问题,只要我在php文件的顶部放置一个自定义变量,以便用户可以修改查询,它就会混乱并且说未定义变量

我是,使用post方法通过html表单传递变量,所以我看不出我做错了什么,有没有人指出我正确的方向?

第一个文件是exportoexcel.php,它是查询选择器,然后由下一个文件调用。

<?php

$VARIABLE="IDNX";

class ExportToExcels
{
    private $host;
    private $dbUser;
    private $dbPassword;
    private $database;
    private $dbTableName;
    private $data;
    private $mysqli;
    private $displayColumns;
    public $excelFileName;

    public function __construct() {
        $this->host = "****";
        $this->dbUser = "****";
        $this->dbPassword="****";
        $this->database="****";
        $this->excelFileName="Report1";
    }

    public function setServerSetting($host,$dbUser,$dbPassword,$database,$dbTableName)
    {
        $this->host = $host;
        $this->dbUserName =$dbUser;
        $this->dbPassword=$dbPassword;
        $this->database=$database;
        $this->tableName=$dbTableName;
    }

    public function establishConnection()
    {
        $mysqli = new mysqli($this->host, $this->dbUserName, $this->dbPassword, $this->database);
        if ($mysqli->connect_errno) {
            return false;
            exit;
        }
        else
        {
            return $mysqli;
        }
    }

    public function getAllData($mysqli)
    {
        $result = $mysqli->query("SELECT * FROM MTYDE WHERE VARIABLE LIKE '$VARIABLE'");
        if($result->num_rows > 0){
            while ($row = $result->fetch_assoc()){
                $data[] = $row;
            }
        }
        $result->close();
        return $data;
    }

    public function closeConnection($mysqli)
    {
        $mysqli->close();
    }

    public function setExcelfileName($excelFileName)
    {
        $this->excelFileName=$excelFileName;
    }

    public function setContentTypeForExcel()
    {
        header ("Expires: 0");
        header ("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT");
        header ("Cache-Control: no-cache, must-revalidate");
        header ("Pragma: no-cache");
        header ("Content-type: application/vnd.ms-excel");
        header ("Content-Disposition: attachment; filename=".$this->excelFileName.".xls");
        header ("Content-Description: Generated Report" );
    }

    public function getColumnName($mysqli)
    {
        $result = $mysqli->query("SHOW COLUMNS FROM $this->tableName");
        if($result->num_rows > 0){
            while ($row = $result->fetch_object()){
                $displayColumns[$row->Field] = ucwords(str_replace('_', ' ', $row->Field));
            }
        }
        $result->close();
        return $displayColumns;
    }

    public function generateExcelFile($mysqli , $options=null)
    {
        $this->displayColumns=$this->getColumnName($mysqli);
        $this->data=$this->getAllData($mysqli , $options);
        $this->setContentTypeForExcel();

        echo 'Report' ."\n";
        echo 'Date:'."\t" . date("F j, Y, g:i a") ."\n";
        echo "\n";

        $totalRecord=count($this->displayColumns);
        $i=1;
        foreach($this->displayColumns as $val)
        {
            echo ($i == $totalRecord) ? $val . "\n" : $val . " \t" ;
            $i++;
        }

        $totalRow= count($this->data);
        for($i=0; $i < $totalRow; $i++ ) { $y=1; foreach($this->displayColumns as $key=>$val)
            {
              echo ($y == $totalRecord) ? $this->data[$i][$key] . " \n" : $this->data[$i][$key] . " \t" ;
              $y++;
          }
      }
  }

}

?>

报告文件,下载excel

 <?php

$VARIABLE="IDNX";

    include "/exportoexcel.php";
    //Lets create object of class
    $exportToExcelObj = new ExportToExcels();
    //Server Setting
    $exportToExcelObj->setServerSetting("localhost","***","***","***","***");
    //Establish a connection
    $mySQLiConn=$exportToExcelObj->establishConnection();
    //Provide your Report Name
    $exportToExcelObj->setExcelfileName('MEASDATA');
    // Generating report
    $exportToExcelObj->generateExcelFile($mySQLiConn);
    //Close connection
    $exportToExcelObj->closeConnection($mySQLiConn);

    ?>

1 个答案:

答案 0 :(得分:0)

这里有一个范围问题。

你可以做的是将var发送给构造函数:

$exportToExcelObj = new ExportToExcels($VARIABLE);

然后在你的构造函数中:

public function __construct($var) {
  $this->host = "****";
  $this->dbUser = "****";
  $this->dbPassword="****";
  $this->database="****";
  $this->excelFileName="Report1";
  $this->VARIABLE = $var
}

稍后在需要时使用$this->VARIABLE

一些解释:

$var = 'something';

class MyClass {

  private $classVar = 'something else';

  function showVar() {
    echo $var;
  }

  function showClassVar() {
    echo $this->classVar;
  }

}

$class = new Class;
$class->showVar(); // Won't work, $var is not defined in the scope of your class (and of your function)
$class->showClassVar(); // Works, $classVar is a class property