我在PHP中编写了一个帮助器类。它有一个构造函数,用于初始化将在方法中填充的数组。有两种方法,一种是填充数组,另一种是返回数组。问题是当我在另一个网页中创建对象并调用方法时,get方法返回错误。而且我得到的错误。请帮助
注意:未定义变量:“C /....."
中的startlatitude<?php
session_start();
class TrafficData
{
function __construct()
{
$startlatitude = array();
$startlongitude = array();
$endlatitude = array();
$endlongitude = array();
$delay = array();
$accidenttype = array();
}
function getdatabyseverity($dayofweek, $zipcode)
{
$host="localhost"; // Host name
$username="kush"; // Mysql username
$password="password"; // Mysql password
$db_name="trafficprediction"; // Database name
$tbl_name="livecongestion"; // Table name
// Create connection
$conn = new mysqli($host, $username, $password, $db_name);
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT `Street`, `CongestionType` , `StartLatitude`, `StartLongitude`, `EndLatitude`, `EndLongitude`, `Delay` FROM `" . $tbl_name . "` WHERE `ZipCode`=\"" .$zipcode . "\" and `Day`=\"" .$dayofweek."\"";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc())
{
$startlatitude[] = $row["StartLatitude"];
$startlongitude[] = $row["StartLongitude"];
$endlatitude[] = $row["EndLatitude"];
$endlongitude[] = $row["EndLongitude"];
$delay[] = $row["Delay"];
$accidenttype[] = $row["CongestionType"];
}
} else
{
echo "0 results";
}
}`
function getarray($text)
{
if($text == "START_LAT")
{
return $startlatitude;
}
}
现在,下面的代码就是我用来创建对象的代码。
<?php
include('DataClass.php');
function getdata()
{
$zipcode=$_POST['zipcode'];
$dayofweek=$_POST['day'];
$severity=$_POST['severity'];
$dataobject = new TrafficData();
$dataobject->getdatabyseverity($dayofweek, $zipcode);
$startlatitude = array();
$startlatitude = $dataobject->getarray("START_LAT");
}
答案 0 :(得分:0)
您创建object
的代码很好。但是你没有返回类变量,而是函数getarray()
尝试返回在函数范围内不可用的局部变量$startlatitude
。
我猜你忘了$this
来访问类变量了。 OOPS
概念!
<?php
session_start();
class TrafficData
{
public var $startlatitude;
function __construct()
{
$this->startlatitude = array();
$startlongitude = array();
$endlatitude = array();
$endlongitude = array();
$delay = array();
$accidenttype = array();
}
function getdatabyseverity($dayofweek, $zipcode)
{
$host="localhost"; // Host name
$username="kush"; // Mysql username
$password="password"; // Mysql password
$db_name="trafficprediction"; // Database name
$tbl_name="livecongestion"; // Table name
// Create connection
$conn = new mysqli($host, $username, $password, $db_name);
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT `Street`, `CongestionType` , `StartLatitude`, `StartLongitude`, `EndLatitude`, `EndLongitude`, `Delay` FROM `" . $tbl_name . "` WHERE `ZipCode`=\"" .$zipcode . "\" and `Day`=\"" .$dayofweek."\"";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc())
{
$this->startlatitude[] = $row["StartLatitude"];
$startlongitude[] = $row["StartLongitude"];
$endlatitude[] = $row["EndLatitude"];
$endlongitude[] = $row["EndLongitude"];
$delay[] = $row["Delay"];
$accidenttype[] = $row["CongestionType"];
}
} else
{
echo "0 results";
}
}`
function getarray($text)
{
if($text == "START_LAT")
{
return $this->startlatitude;
}
}