开发者

Trying to get property of non-object - CodeIgniter

开发者 https://www.devze.com 2023-02-14 06:43 出处:网络
I\'m trying to make update form, that is going to retrieve the data for the specific ID selected, and fill in the form, so its going to be available for updating.

I'm trying to make update form, that is going to retrieve the data for the specific ID selected, and fill in the form, so its going to be available for updating.

When I click edit on the specific entry (Product in my case), it takes me to the edit_product_view, but states the error "Trying to get property of non-object" for every variable that I use in set_values of the form elements.

Using print_r, I get the correct associative array, so it's passed correctly.

This is excerpt of my edit_product_view.

<h2><?php echo $heading; ?></h2>
<hr>
<table id="newproduct">
<?php echo form_open('products/edit/'.$product->id); ?>
<tr>
    <td class="label"><?php echo form_label('Name:');?></td>
    <td><?php echo form_input('prodname', set_value('prodname', $product->prodname));?></td>
</tr>
<tr>
    <td class="label"><?php echo form_label('Product Type:');?></td>
    <td><?php echo form_dropdown('ptname_fk', $product_types, set_value('ptname_fk', $product->ptname_fk));?></td>
</tr>

$product is the array holding all the key-value pairs, but I cannot fill the form for some reason.

开发者_JAVA百科

Thank you in advance!


To access the elements in the array, use array notation: $product['prodname']

$product->prodname is object notation, which can only be used to access object attributes and methods.


To get the value:

$query = $this->db->query("YOUR QUERY");

Then, for single row from(in controller):

$query1 = $query->row();
$data['product'] = $query1;

In view, you can use your own code (above code)


In my case, I was looping through a series of objects from an XML file, but some of the instances apparently were not objects which was causing the error. Checking if the object was empty before processing it fixed the problem.

In other words, without checking if the object was empty, the script would error out on any empty object with the error as given below.

Trying to get property of non-object

For Example:

if (!empty($this->xml_data->thing1->thing2))
{
   foreach ($this->xml_data->thing1->thing2 as $thing)
   {

   }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号