Code Ignitor - Trying to get property of a non object

Multi tool use
Multi tool use


Code Ignitor - Trying to get property of a non object



I am trying to implement cart functionality in codeignitor. 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.


add


get



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.


print_r($product)


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:xampphtdocscidogschewapplicationcontrollerscart.php
Line: 11
Function: _error_handler

File: E:xampphtdocscidogschewindex.php
Line: 315
Function: require_once



Please help.




3 Answers
3



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();


row()


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;


$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





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 3 at 7:59





without seeing your code unable to help you from this side may be some check are there
– pradeep
Jul 3 at 8:15





did it solve your current problem? is my answer helpful for you or not?
– pradeep
Jul 3 at 8: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. stackoverflow.com/questions/51150107/…
– Aurazo Script
Jul 3 at 8:51



You need to access values using array syntax not object syntax



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


$product->pro_name


$product['pro_name']





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 3 at 8: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 3 at 8:28



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


$product



As the error says:



Trying to get property of non-object


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:


row()


get()


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





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 3 at 8: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 3 at 8:27


id


cart


cart






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

ie3NF6UOi30vP fJsGPUC d,AzMuRQ,UzWjimxgQia41sVOK7p1bQu,jcatBBL gZI3Yu8i6Mi Xwrb4eF0 6p8vURJ1BNfw slOum
n0LTkpIFS2Q h 7Hx

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications