where clause for datatable in codeigniter not working

I'm trying to make simple datatable, It's completely okay with out the where clues but I need the where clue to work "$this->db->where('user.user_id', $this->session->userdata('user_id'));". Please help me to fine out whats going wrong here.

class Dashboard_model extends CI_Model{
    var $table = "todo";
    var $select_column = array("todo_id", "todo.user_id", "login", "content");
    var $order_column = array(null, "login", "content");
    public function make_query(){
        $this->db->select($this->select_column);
        $this->db->join('user', $this->table.'.user_id = user.user_id');
        $this->db->where('user.user_id', $this->session->userdata('user_id'));
        $this->db->from($this->table);
        if (isset($_POST["search"]["value"])){
            $this->db->like("login", $_POST["search"]["value"]);
            $this->db->or_like("content", $_POST["search"]["value"]);
        }
        if (isset($_POST["order"])){
            $this->db->order_by($this->order_column[ $_POST['order']['0']['column'] ], $_POST['order']['0']['dir']);
        }else{
            $this->db->order_by("todo_id", "DESC");
        }
    }
    public  function make_datatables(){
        $this->make_query();
        if ($_POST["length"] != -1){
            $this->db->limit($_POST["length"], $_POST["start"]);
        }
        $query = $this->db->get();
        return $query->result();
    }
    public function get_filtered_data(){
        $this->make_query();
        $query = $this->db->get();
        return $query->num_rows();
    }
    public function get_all_data(){
        $this->db->select("*");
        $this->db->from($this->table);
        return $this->db->count_all_results();
    }
}

Upvotes: 1

Views: 1388

Answers (1)

Vitthal
Vitthal

Reputation: 127

Your where clause is not working because you have used OR like

$this->db->or_like(`"content", $_POST["search"]["value"]`);

This will make OR condition inside query.

Use group start and group end before and after like query.

example

        if (isset($_POST["search"]["value"])){
            $this->db->group_start();
            $this->db->like("login", $_POST["search"]["value"]);
            $this->db->or_like("content", $_POST["search"]["value"]);
            $this->db->group_end();
        }

EDIT

Also check raw query by

$this->db->last_query();

Upvotes: 1

Related Questions