1

I am newbie in 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 :

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

How can I generate the query results correctly?

EDIT Let's say I want to get the 'nama_user' into an a variable like this:

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

I use firebug, it gives me null. I think my foreach is part of the problem.

0

3 Answers 3

1

In order to return an array from your model call you can use result_array() as

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
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

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(); in model file

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

Comments

0

You fetched data for selected id it means you get a single row, if you want to get "nama_user" then in your controller:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.