-1

I have written a helper class in PHP. It has one constructor which initializes the arrays that will be populated in the methods. There are 2 methods, one is to populate the array and the other is to return the array. The problem is when I create the object in another webpage and call the methods, the get method returns me an error. And the error that I am getting this. PLEASE HELP

Notice: Undefined variable: startlatitude in "C/....."

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

Now the below code is what I am using to create the object.

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

1 Answers1

0

Your code to create object is fine. But you are not returning class variable, instead function getarray() try to return local variable $startlatitude which is not available within function scope.

I guess, you forgot $this to access class variables. OOPS concepts!

<?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;
         }
     }
Parixit
  • 3,829
  • 3
  • 37
  • 61