0

Please i have an issue with codeigniter. when i try to log here is the result

Below is the my code :

    $sub_and = '';

    if($this->location_parent == 0){
        $sub_and = "and g.idlocation_fk IN (".$locids.")";
    }else{
        $sub_and = "and g.idlocation_fk IN (".$locids.")";  
    }


    if(($this->session->userdata("user_group_fk") == 1) || ($locids == '')){
        $sub_and = '';
    }       


$sql = "select GROUP_CONCAT(DISTINCT(g.idgroup)) as group_id 
            FROM groups g
            JOIN group_users gu ON gu.idgroup_fk = g.idgroup
            WHERE gu.status = 0 and gu.iduser_fk = $iduser $sub_and";

    $results = $this->db->query($sql);
    $results = $results->row_array();
    return $results;
Pradeep
  • 9,667
  • 13
  • 27
  • 34
  • pls always response to the answers by giving some comments or ,if it helps you, by marking it as green and upvoting, it is the best way to thanks all the programmers – Pradeep Jul 28 '18 at 14:46

2 Answers2

0

you have error in concatenation. try with following.

$sql = "select GROUP_CONCAT(DISTINCT(g.idgroup)) as group_id FROM groups g JOIN group_users gu ON gu.idgroup_fk = g.idgroup WHERE gu.status = 0 and gu.iduser_fk = $iduser." ".$sub_and";
0

Hope this will help you :

Make sure $iduser is available in your method and $locids should be an array of values

$this->db->select('GROUP_CONCAT(DISTINCT(g.idgroup)) as group_id');
$this->db->from('groups g');
$this->db->join('group_users gu', 'gu.idgroup_fk = g.idgroup');

$this->db->where('gu.status', '0');
$this->db->where('gu.iduser_fk', $iduser);

/* $locids must be an array like $locids = array(1,3,4);*/
if (! empty($locids))
{
   $this->db->where_in('g.idlocation_fk', $locids);   
}

$row = $this->db->get()->row_array();
print_r($row);

For more : https://www.codeigniter.com/user_guide/database/query_builder.html

Pradeep
  • 9,667
  • 13
  • 27
  • 34