1

Actually after fetching the data from the Database, i want to create a new Object and insert this object to the array but when i check the array it shows the NULL value here is my code:

<?php

$query = "sql query";
$filter_Result = mysqli_query($con, $query);
$newOrders = Array();

while ($row = mysqli_fetch_array($filter_Result)) {
    $order;
    $orderId = $row['order_id']; //fetch row id
    $temp = check_id($newOrders, $orderId);

    if ($temp != null) {
        $order = $temp;
    } else {
        echo " <br>";
        $order = new Order($row['order_id'], $row['status'], $row['created_Date']);
        $newOrders[] = $order;

    }
    $item = new Item($row['status'], $row['quantity']);
    $order->AddItem($item, null);

}
function check_id($newOrders, $orderId) {
    $length = count($newOrders);
    for ($i = 0; $i < $length; $i++) {

        if ($newOrders[$i]->$orderId == $orderId)
            return $newOrders[$i];
    }
    return null;
}

foreach ($newOrders as $order) {
}

?>
Dhruv Tyagi
  • 814
  • 1
  • 9
  • 28
  • $order = new Order($row['order_id'], $row['status'], $row['created_Date']); – Dhruv Tyagi Jun 02 '16 at 08:10
  • Sidenote, your class variables shouldn't be defined with 'var' in PHP5, they should be defined using either 'public', 'private' or 'protected'. More info here: http://stackoverflow.com/questions/1206105/what-does-php-keyword-var-do – H2ONOCK Jun 02 '16 at 08:32

4 Answers4

1

You have a variable in your Order class

var $order_Id;

But then you try to assign value to $orderId which does not exist

$this->orderId = $orderId;

I would suggest turning all PHP errors on while developing. You can include this in your php code to see if you get any errors. It is very hard to see all the small errors with naked eye :) Let PHP do it for you.

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

More about error reporting here.

K.I
  • 568
  • 1
  • 6
  • 19
  • After editing: $this->$order_Id = $order_Id; Still problem is same – Dhruv Tyagi Jun 02 '16 at 08:25
  • 1
    because you are using $this incorrectly. $this->$var means "access the field of $this with the name held in variable $var", which is empty. Correct is $this->var – Adder Jun 02 '16 at 08:30
  • Yes Adder is right. Replace it with $this->order_Id = $order_Id; – K.I Jun 02 '16 at 08:32
  • @Adder can u explain me, In the main code i got some Notices during output, notice are **Notice: Undefined property: Order::$53 in C:\xampp\htdocs\testing\new_latest.php on line 46** and the line is _if ($newOrders[$i]->$orderId == $orderId)_ – Dhruv Tyagi Jun 02 '16 at 11:29
  • you got an extra $ in the variable name, it should read '$newOrders[$i]->orderId'. If you leave the $ there it means it tries to read the element which has an id equal to the value of $orderId, which happens to be 53 – Adder Jun 02 '16 at 12:24
0

You have several small mistakes, e.g. using "this" incorrectly, using different name for "orderId", plus a wrong name for the constructors. The constructor name should be "Order" or "__construct", same for "Item" constructor.

   class Order {
  /* Member variables */
  var $orderId;
  var $status;
  var $createdDate;
  var $items = array();   

  function Order($orderId, $status, $createdDate)
  {
      $this->orderId = $orderId;
      $this->status = $status;
      $this->createdDate = $createdDate;
  }   
  function AddItem($itemId,$quantity)     
  {
       $item = new Item($itemId,$quantity);
      $items[] = $item;
  }

}

$c = new Order(1, 'OK', 'today');
print_r($c);
Adder
  • 5,708
  • 1
  • 28
  • 56
0

Now i found the Solution in the PHP we have to use __construct() for the creating a Constructor.... So use it __construct instead of class name for more info visit: __construct() vs SameAsClassName() for constructor in PHP

Community
  • 1
  • 1
Dhruv Tyagi
  • 814
  • 1
  • 9
  • 28
-2

new_order.php

 <?php
       class Order {
      /* Member variables */
      var $order_Id;
      var $status;
      var $createdDate;
      var $items = array();   

      function __Order($order_Id, $status, $createdDate)
      {
          $this->order_Id = $order_Id;
          $this->status = $status;
          $this->createdDate = $createdDate;
      }   
      function AddItem($itemId,$quantity)     
      {
           $item = new Item($itemId,$quantity);
          $items[] = $item;
      }

    }

    class Item {
        var $productId;
        var $productName;
        var $quantity;
        var $personalization;

        function __Item($productId, $quantity)
        {
            $this->productId = $productId;
            $this->productName = $productName;
            $this->quantity = $quantity;
            $this->personalization = $personalization;
        }
    }
    ?>
dedy
  • 36
  • 5