我真的被这个问题吓坏了,它开始推迟我项目的其余部分,这真的让我很沮丧。
我正在尝试使用从数据库表中获取的值来填充下拉列表,这样如果将来用户想要向下拉列表中添加更多选项,他们可以将它们添加到数据库中的表中。
我正在使用采用 MVC 设计模式的 Codeigniter 平台 (PHP)。
这是我收到的错误消息:
A PHP Error was encountered Severity: Notice Message: Trying to get property of non-object Filename: views/submit.php Line Number: 139
我的模型函数是这个,它从名为“Staff”的表中检索行。 这很好用!
function retrieve_values()
{
$query = $this->db->get('staff');
if ($query->num_rows() > 0)
{
//true if there are rows in the table
return $query->result_array(); //returns an object of data
}
return false;
}
这是接收参数并将其传递到我的 View 的 Controller 函数。 这很好用!
public function displayform()
{
//Checks if a user is logged in, if they are not they get redirected -
if ( $this->session->userdata('name') == FALSE || $this->session->userdata('access_level') == FALSE)
{
redirect ('site/index');// to home page
}
//Stores the returned array in instance called "formdata" which will be passed to the view to be used in pulldown menu
$page['formdata']=$this->submit_model->retrieve_values();
//This loads the form
//Instance of "page" in array "page" specifies the file name of the page to load
$page['page'] = 'submit';
$this->load->view('template', $page );
return;
}
这是导致问题的 View 部分:我正在使用 foreach,然后将数组的实例回显到选项中。
<select>
<?php foreach ($formdata as $row) { ?>
<option value="<?php echo $row->staff_id; ?>"><?php echo $row->name; ?></option>
<?php } ?>
</select>
变量 $formdata 的 printr() 显示它包含这些值:
Array (
[0] => Array (
[staff_id] => 1
[name] => Cardiology Nurse
)
[1] => Array (
[staff_id] => 2
[name] => Radiology Nurse
)
[2] => Array (
[staff_id] => 3
[name] => Scrub Nurse
)
[3] => Array (
[staff_id] => 4
[name] => Circulating Nurse
)
[4] => Array (
[staff_id] => 5
[name] => Nurse
)
[5] => Array (
[staff_id] => 6
[name] => Training Nurse
)
[6] => Array (
[staff_id] => 7
[name] => Physiologist
)
[7] => Array (
[staff_id] => 8
[name] => Radiographer
)
[8] => Array (
[staff_id] => 9
[name] => Consultant
)
[9] => Array (
[staff_id] => 10
[name] => Radiologist
)
[10] => Array (
[staff_id] => 11
[name] => Cardiologist
)
[11] => Array (
[staff_id] => 12
[name] => Anaethestist
)
[12] => Array (
[staff_id] => 13
[name] => Non-medical Staff
)
)
最佳答案
formdata 是数组的数组,而不是对象,因此只需更改您的 View 即可:
<option value="<?php echo $row->staff_id; ?>"><?php echo $row->name; ?></option>
// to
<option value="<?php echo $row['staff_id']; ?>"><?php echo $row['name']; ?></option>
关于php - 试图获得非对象的属性? PHP/代码点火器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6847287/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
在rails源中:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/lazy_load_hooks.rb可以看到以下内容@load_hooks=Hash.new{|h,k|h[k]=[]}在IRB中,它只是初始化一个空哈希。和做有什么区别@load_hooks=Hash.new 最佳答案 查看rubydocumentationforHashnew→new_hashclicktotogglesourcenew(obj)→new_has
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信
我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象