Home > Back-end >  No data available in Table PHP
No data available in Table PHP

Time:01-05

Why does my datatable is not displaying my data whenever i switch account? but if i'm on the admin account i can see the data of the table what is happening? i don't understand can someone please help me?

here is my index.php

<script type="text/javascript">
var manageTable;
var base_url = "<?php echo base_url(); ?>";

$(document).ready(function() {

  // initialize the datatable 
  manageTable = $('#manageTable').DataTable({
    'ajax': base_url   'maincat/fetchMainCatData',
    'order': [],
  });

});
</script>

and here is my controller

    public function fetchMainCatData()
    {
        if(!in_array('viewMaincat', $this->permission)) {
            redirect('dashboard', 'refresh');
        }
        
        $result = array('data' => array());

        $data = $this->model_maincat->getMainCatData();

        foreach ($data as $key => $value) {
           
            

            // button
            $buttons = '';
            if(in_array('updateMaincat', $this->permission)) {
                $buttons .= '<a href="'.base_url('maincat/update/'.$value['id']).'" ><i ></i></a>';
            }

            if(in_array('deleteMaincat', $this->permission)) { 
                $buttons .= ($value['button_status'] == 1) ? ' <button style="display:none;" type="button"  onclick="removeFunc('.$value['id'].')" data-toggle="modal" data-target="#removeModal"><i ></i></button>' : '<button style="margin-left: 5px;" type="button"  onclick="removeFunc('.$value['id'].')" data-toggle="modal" data-target="#removeModal"><i ></i></button>';
            }
            


            $availability = ($value['active'] == 1) ? '<span >Active</span>' : '<span >Inactive</span>';

            $result['data'][$key] = array(
                $value['name'],

                $availability,
                $buttons
            );
        } // /foreach

        echo json_encode($result);
    }
}

here is the model:

public function getMainCatData($id = null)
    {
        if($id) {
            $sql = "SELECT * FROM tbl_main_category where id = ?";
            $query = $this->db->query($sql, array($id));
            return $query->row_array();
        }   

        $user_id = $this->session->userdata('id');
        if($user_id == 1) {
            $sql = "SELECT * FROM tbl_main_category ORDER BY id DESC";
            $query = $this->db->query($sql);
            return $query->result_array(); 
        }
        else {
            
            $user_data = $this->model_users->getUserData($user_id);
            $sql = "SELECT * FROM tbl_main_category ORDER BY id DESC";
            $query = $this->db->query($sql);

            $data = array();

            return $data;       
        }
    }

it was working fine in admin account but whenver i switch account it display no data

enter image description here

but if the account is admin here is the data

enter image description here

what is my problem here i don't quite understand why it's giving me an empty data whenever i switch account? same model and controller using

CodePudding user response:

Assuming the id of your admin account is 1, when you are using a non admin account, you are going through the else portion of your if, in the getMainCatData function.

This function is fetching the data from the database, however, it is always returning an empty array:

        }
        else {
            
            /* ... */

            $data = array();

            return $data;       
        }

You might want to return the value of the query instead:

        }
        else {
            
            /* ... */

            $query = $this->db->query($sql);
            
            return $query->result_array();     
        }

Note. Both your if and else block seams to be doing the same thing, I'm not sure that if is necessary.

  • Related