php 试图获取非对象的属性 - CodeIgniter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5175161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trying to get property of non-object - CodeIgniter
提问by Marko Aleksi?
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!
先感谢您!
回答by Orbit
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
是对象表示法,只能用于访问对象的属性和方法。
回答by shike
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)
查看,可以使用自己的代码(上面的代码)
回答by Ben Wilson
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 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)
{
}
}