0

I am trying to implement cart functionality in codeigniter. In my controller I have a public function add and in my model a public function called get to fetch data from database according to the product selected.

here is my Controller

public function add() {
    $id = $this->input->post('id');
    $product = $this->products_model->get($id);

    echo "<pre>";
    print_r($product);  die();

    $data = array(
      'id' => $id,
      'name' => $product->pro_name,
      'qty' => 1,
      'price' => $product->pro_price
    );
    $this->cart->insert($data);
  }

and here is my Model

public function get($id) {
  $results = $this->db->get_where('products', array('pro_id' => $id));
  return $results->result_array();
}

When I print_r($product) I get an array like this.

Array
(
    [0] => Array
        (
            [pro_id] => 1
            [pro_name] => Beef Carrot & Pea Food
            [pro_price] => 150.00
            [pro_image] => 1.png
        )

)

But when I try to insert in the data array I get this error.

A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: controllers/cart.php

Line Number: 11

Backtrace:

File: E:\xampp\htdocs\ci\dogschew\application\controllers\cart.php
Line: 11
Function: _error_handler

File: E:\xampp\htdocs\ci\dogschew\index.php
Line: 315
Function: require_once
Cœur
  • 37,241
  • 25
  • 195
  • 267
Aurazo Script
  • 115
  • 1
  • 1
  • 9

3 Answers3

0

Hope this will help you :

Since u r using single item with object in your controller,So you should use row() instead of result_array();

Your model should be like this :

public function get($id) 
{
  $results = $this->db->get_where('products', array('pro_id' => $id));
  return $results->row();
}

Your controller should be like this :

Print $data in your controller to check it has data;

public function add() 
{
     $id = $this->input->post('id');
     $product = $this->products_model->get($id);

     $data = array(
       'id' => $id,
       'name' => $product->pro_name,
       'qty' => 1,
       'price' => $product->pro_price
     );
     print_r($data); die();
     $this->cart->insert($data);
}

For more : https://www.codeigniter.com/user_guide/database/results.html

Pradeep
  • 9,667
  • 13
  • 27
  • 34
  • Partially solved. Now whenever I insert only data with even number ID values are getting inserted in the cart and not the odd number id values. Means values with 2,4,6,8 are getting inserted and 1,3,5,7 are not getting inserted. I have reached the question limit so can't raise a new one. If you can help by updating the answer it will be a great help. – Aurazo Script Jul 03 '18 at 07:59
  • 1
    without seeing your code unable to help you from this side may be some check are there – Pradeep Jul 03 '18 at 08:15
  • did it solve your current problem? is my answer helpful for you or not? – Pradeep Jul 03 '18 at 08:36
  • Yes it solved. And sorry the second problem was misintepreted. The problem is here. I asked a new question with another account plz check. https://stackoverflow.com/questions/51150107/codeigniter-add-to-cart-does-not-insert-data-in-cart-array-where-name-has-a-spec?noredirect=1#comment89283847_51150107 – Aurazo Script Jul 03 '18 at 08:51
0

You need to access values using array syntax not object syntax

$product->pro_name instead of this use $product['pro_name']

Javed Sayyed
  • 149
  • 11
  • Partially solved. Now whenever I insert only data with even number ID values are getting inserted in the cart and not the odd number id values. Means values with 2,4,6,8 are getting inserted and 1,3,5,7 are not getting inserted. I have reached the question limit so can't raise a new one. If you can help by updating the answer it will be a great help. – Aurazo Script Jul 03 '18 at 08:10
  • Copying and pasting this for every answer isn't going to help. Update your question with the relevant code. Bare in mind (for the future) it is considered bad form to change the question after the fact ;) – Alex Jul 03 '18 at 08:28
0

You are returning an array and not an object. Hence, $product will contain an array...

As the error says:

Trying to get property of non-object

Try this:

$data = [
    'id' => $id, 
    'name' => $product[0]['pro_name'], 
    'qty' => 1, 
    'price' => $product[0]['pro_price']
];

...or better yet, use row() method on your model's get() method, like so:

public function get($id)
{
    $results = $this->db->get_where('products', [
        'pro_id' => $id
    ]); 

    return $results->row();
}

Using that, you can now have:

$data = [
    'id' => $id, 
    'name' => $product->pro_name, 
    'qty' => 1, 
    'price' => $product->ipro_price
];

Source: https://www.codeigniter.com/user_guide/database/results.html#result-rows

ubuntux
  • 394
  • 3
  • 12
  • Partially solved. Now whenever I insert only data with even number ID values are getting inserted in the cart and not the odd number id values. Means values with 2,4,6,8 are getting inserted and 1,3,5,7 are not getting inserted. I have reached the question limit so can't raise a new one. If you can help by updating the answer it will be a great help. – Aurazo Script Jul 03 '18 at 08:10
  • try to check your error logs to find some clues. i am suspecting duplicate record insertion error via `id` column if that's your primary key on `cart` table. can you show us your `cart` table structure? – ubuntux Jul 03 '18 at 08:27