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.
我正在尝试制作更新表单,即检索所选特定ID的数据,并填写表单,以便可以进行更新。
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.
当我单击特定条目(我的情况下是产品)上的编辑时,它会转到edit_product_view,但是对于我在表单元素的set_values中使用的每个变量,都会声明错误“试图获取非对象的属性”。
Using print_r
, I get the correct associative array, so it's passed correctly.
使用print_r,我得到正确的关联数组,所以它被正确传递。
This is excerpt of my edit_product_view
.
这是我的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.
$ product是包含所有键值对的数组,但由于某种原因我无法填写表单。
Thank you in advance!
先谢谢你!
3 个解决方案
#1
47
To access the elements in the array, use array notation: $product['prodname']
要访问数组中的元素,请使用数组表示法:$ product ['prodname']
$product->prodname
is object notation, which can only be used to access object attributes and methods.
$ product-> prodname是对象表示法,只能用于访问对象属性和方法。
#2
6
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)
在视图中,您可以使用自己的代码(上面的代码)
#3
3
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.
在我的例子中,我正在循环遍历XML文件中的一系列对象,但是有些实例显然不是导致错误的对象。在处理之前检查对象是否为空它修复了问题。
In other words, without checking if the object was empty, the script would error out on any empty object with the error "Trying to get property of non-object"
换句话说,在不检查对象是否为空的情况下,脚本会在任何空对象上出错,并显示错误“试图获取非对象的属性”
e.g.
例如
if (!empty($this->xml_data->thing1->thing2))
{
foreach ($this->xml_data->thing1->thing2 as $thing)
{
}
}
#1
47
To access the elements in the array, use array notation: $product['prodname']
要访问数组中的元素,请使用数组表示法:$ product ['prodname']
$product->prodname
is object notation, which can only be used to access object attributes and methods.
$ product-> prodname是对象表示法,只能用于访问对象属性和方法。
#2
6
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)
在视图中,您可以使用自己的代码(上面的代码)
#3
3
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.
在我的例子中,我正在循环遍历XML文件中的一系列对象,但是有些实例显然不是导致错误的对象。在处理之前检查对象是否为空它修复了问题。
In other words, without checking if the object was empty, the script would error out on any empty object with the error "Trying to get property of non-object"
换句话说,在不检查对象是否为空的情况下,脚本会在任何空对象上出错,并显示错误“试图获取非对象的属性”
e.g.
例如
if (!empty($this->xml_data->thing1->thing2))
{
foreach ($this->xml_data->thing1->thing2 as $thing)
{
}
}