Unable to fetach all data in codeigniter

codeigniter get single row
codeigniter query
codeigniter get row by id
codeigniter count rows in controller
codeigniter query->result associative array
foreach in codeigniter
select query in codeigniter controller
select query with where condition in codeigniter

I have the following controller where I get the records based on user login. its work successfully but I want to make an admin who fetch all the user data. my mind is not getting any idea how to do that. pls, help me. my method for fetch user data is following.

public function get_posts($slug = FALSE, $limit = FALSE, $offset = FALSE){
    if($limit){
        $this->db->limit($limit, $offset);
    }
    $user_id = $this->session->userdata['user_id'];
    if($slug === FALSE){
        $this->db->select('*');
        $this->db->from('posts');

        $this->db->order_by('posts.id', 'DESC');
        $this->db->join('users', 'users.id= posts.user_id');
        $this->db->where('users.id',$user_id);
        $query = $this->db->get();
        return $query->result_array();
    }

    $query = $this->db->get_where('posts', array('slug' => $slug));
    return $query->row_array();
}

Do the following changes: (Read the comments carefully.)

public function get_posts($slug = FALSE, $limit = FALSE, $offset = FALSE){
    if($limit){
        $this->db->limit($limit, $offset);
    }
    $user_id = $this->session->userdata['user_id'];
    $user_type = $this->session->userdata['userType'];  // Assuming that you would be storing user role in session with 'userType' key
    if($slug === FALSE){
        $this->db->select('*');
        $this->db->from('posts');

        $this->db->order_by('posts.id', 'DESC');
        $this->db->join('users', 'users.id= posts.user_id');
        if($user_type != 'admin'){  // Then fetch all post of logged in user. Otherwise all post of all the users will be fetched.
            $this->db->where('users.id',$user_id);
        }
        $query = $this->db->get();
        return $query->result_array();
    }

        $query = $this->db->get_where('posts', array('slug' => $slug));
        return $query->row_array();
}

[SOLVED] Codeigniter wont display two million records [Is this a , To be exact 2 million and 500 thousands rows. And when i attempt browser it says: Unable to select the specified database: emlmproj Filename: 2430 posts. You're retrieving all records when defining $config['total_rows']. In this CodeIgniter tutorial, I will show you how to fetch data from database using model, view, controller approach. As usual, we need need to setup database connection in order to perform actions like select, insert, delete, update..etc in the database. open your database.php file and set up the database configuration values, after setting up …


If you don't have the user role stored in the session you should fetch the current user data again:

public function get_posts($slug = FALSE, $limit = FALSE, $offset = FALSE){
    if($limit){
        $this->db->limit($limit, $offset);
    }

    $user_id = $this->session->userdata['user_id'];

    // Fetch current user from db
    $this->db->select('*');
    $this->db->from('users');
    $this->db->where('users.id',$user_id);
    $query = $this->db->get();
    $user = $query->row_array();

    if($slug === FALSE){
        $this->db->select('*');
        $this->db->from('posts');

        $this->db->order_by('posts.id', 'DESC');
        $this->db->join('users', 'users.id = posts.user_id');

        if(!$user['is_admin']){  // Assuming that you would be storing if user is admin in database in a 'is_admin' field
            $this->db->where('users.id',$user_id);
        }

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

    $query = $this->db->get_where('posts', array('slug' => $slug));
    return $query->row_array();
}

Queries, When retrieving data you will typically assign the query to your own variable, That typically is TRUE/FALSE on success or failure for write type queries such as​  Next thing is to create a function called getAllRecords which fetch all records from test table. To access database codeigniter has built in library database which we have to load to access its functionality. $this->load->library(“database”) will load database library in $this->db object =.


try this,

public function get_posts($slug = FALSE, $limit = FALSE, $offset = FALSE, $is_admin = FALSE){
    $this->db->select('*');
    $this->db->from('posts');        
    if(!$is_admin){
        $this->db->join('users', 'users.id = posts.user_id');
        $this->db->where('users.id', $this->session->userdata('user_id'));
    }
    if($slug){ $this->db->where('posts.slug', $slug); }        
    if($limit){ $this->db->limit($limit, $offset); }
    $this->db->order_by('posts.id', 'DESC');
    $query = $this->db->get();        
    return ($query->num_rows() > 1) ? $query->result_array() : $query->row_array();
}

USAGE

// In USER (same like you are calling now)
$posts = get_posts();

// In ADMIN
$posts = get_posts(false, false, false, true);

Generating Query Results, Result Arrays; Result Rows; Custom Result Objects; Result Helper Methods; Class returns the query result as an array of objects, or an empty array on failure. First, pass the id from you controller to your model. Just call the function from your model into your controller as so Controller: [code]$data['info'] = $this->My


Generating Query Results : CodeIgniter User Guide, This function returns the query result as an array of objects, or an empty array on failure. Typically The number of rows returned by the query. Note: In this  It will show you how a basic CodeIgniter application is constructed and for starters, I will assume that you already have installed CodeIgniter and you have a MySQL database installed on your webserver. In this article, I will show you how to fetch data from s database and show it in a view in a tabular format in codeigniter.


Query Builder Class, $this->db->get(). Runs the selection query and returns the result. Can be used by itself to retrieve all records from a table: $query = $this->db->get('mytable');  Query Builder Class¶ CodeIgniter gives you access to a Query Builder class. This pattern allows information to be retrieved, inserted, and updated in your database with minimal scripting. In some cases only one or two lines of code are necessary to perform a database action. CodeIgniter does not require that each database table be its own class file.


Queries, When “write” type queries are run it simply returns TRUE or FALSE depending on success or failure. When retrieving data you will typically assign the query to  In CodeIgniter, get() method is used to run select statement and return data from table. It can be used stand alone to retrieve all records from a table. It can be used stand alone to retrieve all records from a table.