我是codeigniter的新手。如果我有这样的模型:
public function get_data_for_reminder($id) {
$this->db->select('nama_user, keluhan, email, addresed_to');
$query = $this->db->get_where('tbl_requestfix', array('id_request' => $id));
return $query->row();
}
我尝试从may控制器访问它:
public function reminderIT() {
$id = $this->input->post('id');
$data = $this->model_request->get_data_for_reminder($id);
如何生成查询结果,感谢您的帮助。
修改 我是CI的新手,我的问题是:让我们说我想得到' nama_user'变成这样的变量:
foreach ($data as $d) {
$name = $d['nama_user'];
}
echo json_encode($name);
我使用firebug,它给了我null。我认为我的foreach存在问题
答案 0 :(得分:1)
要从模型调用中返回数组,您可以使用result_array()
作为
public function get_data_for_reminder($id) {
$this->db->select('nama_user, keluhan, email, addresed_to');
$query = $this->db->get_where('tbl_requestfix', array('id_request' => $id));
return $query->result_array();//<---- This'll always return you set of an array
}
答案 1 :(得分:0)
包含查询代码的控制器文件
public function reminderIT() {
$id = $this->input->post('id');
$data = $this->model_request->get_data_for_reminder($id);
//Generating Query Results like this because you use $query->row();
$data->nama_user;
$data->keluhan;
$data->email;
$data->addresed_to;
$info_json = array("nama_user" => $data->nama_user, "keluhan" => $data->keluhan, "email" => $data->email, "addresed_to" => $data->addresed_to);
echo json_encode($info_json);
}
<强> MODEL.PHP 强>
public function get_data_for_reminder($id) {
$this->db->select('nama_user, keluhan, email, addresed_to');
$query = $this->db->get_where('tbl_requestfix', array('id_request' => $id));
return $query->row_array();
}
//如果使用$ query-&gt; row_array()生成查询结果;在模型文件中
<强> Controller.php这样强>
function reminderIT() {
$id = $this->input->post('id');
$data = $this->model_request->get_data_for_reminder($id);
foreach($data as $row)
{
$myArray[] = $row;
}
echo json_encode($myArray);
答案 2 :(得分:0)
您为选定的ID获取了数据,这意味着您获得了一行,如果您想在控制器中获得“nama_user”:
public function reminderIT() {
$id = $this->input->post('id');
$data = $this->model_request->get_data_for_reminder($id);
$name = $data->nama_user;
echo json_encode($name);
}