Check if it is empty or not in codeigniter

I am working at codeigniter. I have created one function in the model. The function is as follows:

function pr($id)
    {
        //$query5 = $this->db->query("select max(to_date) as last_date from agent_print_details where agent_id = ".$id);
        //$result5 = $query5->result();

        $this->db->select('max(to_date) as last_date');
        $this->db->from('agent_print_details');
        $this->db->where('agent_id',$id);

        $result = $this->db->get();

        if($result->num_rows()>0)

        return $result->result();

        else

        return "empty";

    }

      

my controller:

$pr_detail = $this->cashier1_model->pr($role['id']);

if($pr_detail != 0)
{
    echo "nisarg";
}
else
{
    echo "123";
}

      

when i print pr_detail then it will print output like this:

Array ( [0] => stdClass Object ( [last_date] => ) ) 

      

it will give blank data, so it should print 123 , but it displays nisarg .

So what do I need to do to print 123 ?

+3


source to share


3 answers


If no result is found, return FALSE

to the model file. Also use CI select_max

to get maximum value

MODEL

$this->db->select_max('to_date','last_date');
$this->db->from('agent_print_details');
$this->db->where('agent_id', $id);
$result = $this->db->get();
if ($result->num_rows() > 0) {
    return $result->result();
} else {
    return FALSE;
}

      



CONTROLLER

$pr_detail = $this->cashier1_model->pr($role['id']);

if($pr_detail)
{
    echo "nisarg";
}
else
{
    echo "123";
}

      

+5


source


you can use count function, also, have you defined table name in get function?



...
$result = $this->db->get('agent_print_details');
//if($result->num_rows() > 0)
if(count($result) > 0)
...

      

+1


source


When you use $ result-> num_rows ()> 0, you CANT use $ result-> result (). If you want to return these results, you need to first check to see if it is empty and then query again to get the results.

$this->db->select_max('to_date','last_date');
$this->db->from('agent_print_details');
$this->db->where('agent_id', $id);
$result = $this->db->get();
if ($result->num_rows() > 0) {

   $this->db->select_max('to_date','last_date');
   $this->db->from('agent_print_details');
   $this->db->where('agent_id', $id);
   $result = $this->db->get();

   return $result->result();

} else {

    return FALSE;

}

      

+1


source







All Articles