0
votes

How to fix this error:

Warning: Illegal offset type in isset or empty in F:\xampp\htdocs\digikalamvc\core\model.php on line 140

Fatal error: Uncaught Error: Unsupported operand types in F:\xampp\htdocs\digikalamvc\models\model_showcart4.php:90 Stack trace: #0 F:\xampp\htdocs\digikalamvc\controllers\showcart4.php(31): model_showcart4->saveOrder(Array) #1 F:\xampp\htdocs\digikalamvc\core\app.php(34): Showcart4->saveorder() #2 F:\xampp\htdocs\digikalamvc\index.php(7): App->__construct() #3 {main} thrown in F:\xampp\htdocs\digikalamvc\models\model_showcart4.php on line 90

Code in model.php on line 140: ( if (isset($_SESSION[$name])) { )

public static function sessionGet($name) {
    if (isset($_SESSION[$name])) {
        return $_SESSION[$name];
    } else {
        return false;
    }
}

Code in model_showcart4.php on line 90:

$priceTotal =$basketPrice-$basketDiscount+$postprice;

Code model_showcart4:

$basket = $basketInfo[0];
    $basket = serialize($basket);
    $basketPrice = $basketInfo[1];
    $basketDiscount = $basketInfo[2];

    $postprice = $this->calculatePostPrice($addressInfo['city']);
    $postType = self::sessionGet(['postType']);
    if ($postType == 1) {
        $postprice = $postprice['pishtaz'];
    } elseif ($postType == 2) {
        $postprice = $postprice['sefareshi'];
    }

    $priceTotal =$basketPrice-$basketDiscount+$postprice;

Code showcart4.php on line 31:

function saveorder() {
    $this->model->saveOrder($_POST);
}
2
you are passing array as a key to array in isset. $postType = self::sessionGet(['postType']); argument is array. but array can not be a key in other arrayViktar Pryshchepa

2 Answers

1
votes

$postType = self::sessionGet(['postType']); as you can see, argument is an array. so here isset($_SESSION[$name]) code gets to be invalid, because array key should be integer or string, not an array.

$postType = self::sessionGet('postType'); - this should work, I guess

0
votes

Your warning indicates that the key given to your function doesn't exist in the $_SESSION array. In model_showcart4.php on line 83 you passed an array to your function, which is probably the cause of this error.

I would recommend to add an extra check in your if statement. Use array_key_exists function to check if the key actually exists in the given array, so if for some reason a key is given which simply doesn't exists, your code wouldn't break.

F:\xampp\htdocs\digikalamvc\core\model.php

public static function sessionGet($name) {
    if (array_key_exists($name, $_SESSION) && isset($_SESSION[$name])) {
        return $_SESSION[$name];
    } else {
        return false;
    }
}