codeigniter "where" clause doesn't work

codeigniter "where" clause doesn't work

rampagewinnerrampagewinner Posts: 1Questions: 0Answers: 0

hi, i'm working with datatables in codeigniter, i have this code:
dashboard_model.php

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->where('user.user_id', $this->session->userdata('user_id'));
        $this->db->from($this->table);
        $this->db->join('user', $this->table.'.user_id = user.user_id');


        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();

    }



    public function insert_todo($data){

        $this->db->insert('todo', $data);

    }




}

dashboard_controller.php

public function listar_usuario(){

    $this->load->model('dashboard_model');
    $fetch_data = $this->dashboard_model->make_datatables();
    $data = array();

    foreach ($fetch_data as $row){


        $sub_array = array();
        $sub_array[] = $row->user_id;
        $sub_array[] = $row->login;
        $sub_array[] = $row->content;
        $sub_array[] = '<button type="button" name="update" id="' .$row->todo_id. '" class="btn btn-warning btn-xs">Actualizar</button>';
        $sub_array[] = '<button type="button" name="delete" id="' .$row->todo_id. '" class="btn btn-danger btn-xs">Borrar</button>';

        $data[] = $sub_array;


    }

    $output = array(
        "draw" => intval($_POST["draw"]),
        "recordsTotal" => $this->dashboard_model->get_all_data(),
        "recordsFiltered" => $this->dashboard_model->get_filtered_data(),
        "data" => $data
    );

    echo json_encode($output);


}

my ajax:

$(document).ready(function () {
       var dataTable = $('#user_data').DataTable({
           "processing": true,
           "serverSide": true,
           "order": [],
           "ajax": {
               url: "<?php echo base_url().'dashboard/listar_usuario'; ?>",
               type: "POST"
           },
           "columnDefs":[
               {
                   "target": [0],
                   "orderable": false
               }
           ]

       });

i just wanted to fetch the data of the user who is logged, the user actually logged is "userexample" with id of 16


it still show me the id 15 "another user", the "where" condition i put in the model doesn't work, even if i put $this->db->where('user.user_id', 16) it still fetching me all the data.

can someone help me with this??

Replies

  • allanallan Posts: 63,464Questions: 1Answers: 10,466 Site admin

    You'd really need to ask in a CI forum or StackOverflow. I'm afraid I've not actually used CI before and so can't really help in that regard.

    Allan

This discussion has been closed.