i.one i.one - 1 month ago 13
PHP Question

How to call function other model in one model?

I have two function in my model, I want to call other model in my model, but actually error array to string conversion.
This is my code:

public function get_detail_activity()
{
$status=$activity;//call function model activity
$query=$this->db->query("SELECT
t_trx_activity.activity_id FROM t_trx_activity
JOIN t_trx_activity_detail ON
t_trx_activity_detail.activity_id =t_trx_activity.activity_id
WHERE t_trx_activity.activity_id = '".$status."' //function put here");

if($query->num_rows()>0)
{
return $query->result();
}
else
{
return array();
}

public function activity()
{
$query=$this->db->query("SELECT t_trx_activity.activity_id
FROM t_trx_activity
ORDER BY
t_trx_activity.activity_id DESC LIMIT 1");
$nik=$this->session->userdata('nik');

if($query->num_rows()>0)
{
return $query->result();
}
else
{
return array();
}
}


How to call function other model in one model?

Answer

you are not calling the function properly, try the following,

public function get_detail_activity()
{
    $status= $this->activity();//call function model activity
    $query=$this->db->query("SELECT 
          t_trx_activity.activity_id FROM t_trx_activity
          JOIN t_trx_activity_detail ON
          t_trx_activity_detail.activity_id =t_trx_activity.activity_id
          WHERE t_trx_activity.activity_id = '".$status."'");
    if($query->num_rows()>0)
    {
        return $query->result_array();
    }
    else
    {
        return array();
    }
}


 public function activity()
 {
     $query=$this->db->query("SELECT t_trx_activity.activity_id
         FROM t_trx_activity
         ORDER BY
         t_trx_activity.activity_id DESC LIMIT 1");
         $nik=$this->session->userdata('nik');

     if($query->num_rows()>0)
     {
         return $query->row()->activity_id;
     }
     else
     {
         return 0;
     }
 }
Comments