gpt4 book ai didi

php - 导出到 Excel 时 undefined variable

转载 作者:行者123 更新时间:2023-11-29 23:46:57 26 4
gpt4 key购买 nike

我正在使用 php/mysql 站点的导出到 Excel 功能。当我在文件中设置查询时,它工作得很好,xls 文档会立即下载。

我无法让它与自定义 php 变量一起使用,一旦我将自定义变量放在 php 文件顶部以便用户可以修改查询,它就会变得困惑并显示 undefined variable 。

我正在使用 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);

?>

最佳答案

这里存在范围问题。

你可以做的就是将你的 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

关于php - 导出到 Excel 时 undefined variable ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25863213/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com