我收到错误在我的控制器的codeigniter中调用非对象上的成员函数row()
public function edit_survey_pro($id)
{
$id = intval($id);
$survey = $this->model->get("surveys",array("ID" => $id),100000);
if (sizeof($survey) == 0) $this->template->error(lang("error_32"));
$this->template->loadContent("user/edit_survey_pro", array(
"survey" => $survey->row()
)
);
}
我的模特是
function get($table,$where='',$perpage=0,$start=0,$order_by='',$arr='')
{
$this->db->from($table);
if($perpage != 0 && $perpage != NULL)
$this->db->limit($perpage,$start);
if($where){
$this->db->where($where);
}
if($order_by){
$this->db->order_by($order_by);
}
if($arr=='')
$query = $this->db->get()->result();
else
$query = $this->db->get()->result('array');
if(!empty($query))
if($perpage != 0 && $perpage != NULL)
$result = $query;
else
$result = $query[0];
else
$result = array();
return $result;
}
这里loadContent()只是加载带有视图路径的内容
public function loadContent($view,$data=array(),$die=0){
//something to load the content
}
答案 0 :(得分:1)
在我的模型中我在$query
中将结果作为对象数组获取,然后将其作为$result
返回,如下所示 -
$query = $this->db->get()->result();
但在控制器 $survey
存储对象数组,我想显示该对象数组的内容,之前我使用
$this->template->loadContent("user/edit_survey_pro", array(
"survey" => $survey->row()
)
);
获取该数据,但问题是$survey->row()
无法返回该数据bcoz它不是一个对象,它是对象数组,因此无法通过row()
方法返回
所以不是这样,我只是调用那个数据的第一个元素 -
$this->template->loadContent("user/edit_survey_pro", array(
"survey" => $survey[0]
)
);
不知怎的,它为我工作bcoz我想显示数据的第一行
如果sembody想显示所有数据,那么我认为他shuld尝试逻辑来为我增加该对象数组的键值$survey[]
你可以使用foreach
循环来增加键的值元件
答案 1 :(得分:0)
我看到的问题是您的模型,我会剖析它并在原始代码中添加注释以指出问题:
function get($table,$where='',$perpage=0,$start=0,$order_by='',$arr='')
//above there are problems, you are setting some of your parameters to
//equal blank, but below, in your conditionals, you are checking if they
// exist. They will always exist if they are set to blank. Fix them by
// setting them = NULL like this:
// get($table,$where=null,$perpage=0,$start=0,$order_by=null,$arr=null)
{
$this->db->select();// <-- you forgot this
$this->db->from($table);
if($perpage != 0 && $perpage != NULL)
//when will $perpage = null? , if never, then you dont need it.
$this->db->limit($perpage,$start);
if($where){
//change this to if(isset($where)). Also why do you use
//curly braces here, but not in the above if statement if only
//one line is affected in your if. I would remove the
//curly braces here.
$this->db->where($where);
}
if($order_by){
// change this to if(isset($order_by)). Same thing as
//above about the curly braces here
$this->db->order_by($order_by);
}
if($arr=='')
// change this to if(isset($arr)).
$query = $this->db->get()->result();
else
$query = $this->db->get()->result('array');
//change this to: $query = $this->db->get()->result_array();
if(!empty($query))
//change the above to if($query->num_rows > 0). Also, here since
//your code body is longer then one line, you will need curly braces
//around your if statement
if($perpage != 0 && $perpage != NULL)
//again, will $perpage ever be NULL? However, why do you need
//this conditional at all, if the limit above is already
//doing this job?
$result = $query;
else
$result = $query[0];
else
$result = array();
return $result;
}
应用所有更改后:
<强> MODEL:强>
function get($table, $where=null, $perpage=0, $start=0, $order_by=null, $arr=null)
{
$this->db->select();
$this->db->from($table);
if($perpage != 0)
$this->db->limit($perpage, $start);
if(isset($where))
$this->db->where($where);
if(isset($order_by))
$this->db->order_by($order_by);
if(isset($arr)) {
$result = $this->db->get()->result_array();
} else {
$result = $this->db->get()->result();
}
return $result;
}
<强> CONTROLLER 强>
public function edit_survey_pro($id) {
$id = intval($id);
$survey = $this->model->get("surveys",array("ID" => $id),100000);
if (!$survey) {
$this->template->error(lang("error_32"));
} else {
$data["survey"] = $survey;
$this->template->loadContent("user/edit_survey_pro", $data);
}
}
答案 2 :(得分:-1)
我认为当你使用series
时,需要将表名作为param传递:
$this->db->get()