简体   繁体   English

在codeigniter中生成row()的查询结果:

[英]generating querying result for row() in codeigniter:

I am newbie in codeigniter. 我是Codeigniter的新手。 If I have a model like this : 如果我有这样的模型:

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();
}

And I try to accessed it from may controller : 我尝试从may控制器访问它:

public function reminderIT() {
    $id = $this->input->post('id');
    $data = $this->model_request->get_data_for_reminder($id); 

How to Generating Query Results, thanks for the help. 如何生成查询结果,谢谢您的帮助。

EDIT I am new in CI, my question is : let's say I want to get the 'nama_user' into an a variable like this : 编辑我是CI的新手,我的问题是:假设我想将'nama_user'放入这样的变量中:

foreach ($data as $d) {
        $name = $d['nama_user'];

    }

    echo json_encode($name);

I use firebug, it gives me null. 我用萤火虫,它给了我空。 I think my foreach is in a problem 我认为我的上课有问题

In order to return an array from your model call you can use result_array() as 为了从模型调用中返回一个数组,可以使用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
}

Controller File with your query code 带有查询代码的控制器文件

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 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();
}

//Generating Query Results if use $query->row_array(); //如果使用$ query-> row_array();则生成查询结果 in model file 在模型文件中

Controller.php 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);

You fetched data for selected id it means you get a single row, if you want to get "nama_user" then in your controller: 您获取了选定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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM