Take variable from an array in PHP with CodeIngiter

1

Hi, I have the following in CodeIgniter:

MODEL:

public function GetHoraPaciente($rut_paciente,$estado){ // muestra todoas las horas pedidas del paciente

    $this->db->where("rut_paciente",$rut_paciente);
    $this->db->where("estado",$estado);

    $this->db->select('id');
    $this->db->select('rut_medico');
    $this->db->select('fecha_programada');
    $this->db->select('hora_programada');
    $this->db->from("horas_medicas");
    $query= $this->db->get();
    return $query->result();
}

public function GetDocdate($rut_medico){ // muestra datos del doctor segun su rut;

    $this->db->where("rut",$rut_medico);
    $this->db->select('nombre');
    $this->db->select('apellido');
    $this->db->from("medico");
    $query= $this->db->get();
    return $query->row();
}

CONTROLLER:

public function HoraPaciente(){  //muestra las horas del paciente

    $rut_paciente = $this->input->post('rut_paciente');
    $estado = $this->input->post('estado');
    $this->load->model('ChincolModel');
    $data = $this->ChincolModel->GetHoraPaciente($rut_paciente,$estado);

    if (!$data == null) {

        // saco los datos la variable data
        $id = $data['id'];
        $rut_medico = $data['rut_medico'];
        $fecha_programada = $data['fecha_programada'];
        $hora_programada = $data['hora_programada'];

        //pido nombre y y apellido al module GetDocdate con el rut que saque de $data
        $this->load->model('ChincolModel');
        $medico = $this->ChincolModel->GetDocdate($rut_medico);
        // saco los datos de la variable $medico
        $nombre = $medico['nombre'];
        $apellido = $medico['apellido'];

        // los almaceno en un array  y los muestro en JSON
        $data5 = array("rut_medico" => "$rut_medico", "id" => "$id", "fecha_programada" => "$fecha_programada",
        "hora_programada" => "$hora_programada","nombre_medico" => "$nombre","apellido_medico" => "$apellido");

        header('Content-Type: application/json');
        echo json_encode($data5);

    }

}

For some reason that I do not know, I am not filling the variables that I get from $data , these are null and it makes it impossible to continue with the objective ...

What can it be?

    
asked by Hernan Humaña 02.03.2017 в 22:00
source

4 answers

2

It will probably happen if it brings you data because the function of the model seems to be correct. You should check the result of the query:

$data = $this->ChincolModel->GetHoraPaciente($rut_paciente,$estado);
echo "<pre>";
var_dump($data);die();
if (!$data == null) {

If you print something different from null it means that the query is correct. Supposedly the error is in the validation of if .

Change if (!$data == null) { for some line of these:

  • if(!is_null($data)){ }
  • if ($data != null)
  • if($data)

The way you are obtaining the data of the variable $data is incorrect since you are using it as if it were an array and from the model you get it as an object, if you want to obtain it as an array you must modify the model in the following way:

  • return $ query-> result_array ();

and in the controller, like this:

  • $ id = $ data [0] ['id'];

If it's just a record you can use the following in the model:

  • return $ query-> g_array ();

to thus have the controller as you had it:

  • $ id = $ data ["id"];
answered by 20.03.2017 / 19:51
source
1

You can try to check the result from the same query

Execute:

$this->db->last_query(); 

at the end and verify that it is well formed.

Then check if you return the value in the controller with a

var_dump($data);
    
answered by 13.07.2017 в 22:04
0

Take a var_dump for the values that the $ data array has after the query, it could be that it has data but not what you expect. Try with that and tell me.

    
answered by 03.03.2017 в 00:34
0

Access the variables as object $ data- > id, $ data- > rut_medico, etc the row function returns an object and you are accessing it as an array.

Source: row function

    
answered by 03.03.2017 в 04:09