CodeIgniter Forums

Full Version: How to get user ID in libraries CodeIgniter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How to get user ID in libraries,

User Table
Code:
| ID | username |
|---------------|
| 1  | Alex     |
| 2  | Johey    |
| 3  | Leos     |
| 4  | Pacat    |

Store Table
Code:
| ID | userID | store_picture |
|-----------------------------|
| 1  | Alex   | img1.jpg      |
| 2  | Johey  | img3.jpg      |
| 3  | Leos   | img2.jpg      |
| 4  | Pacat  | img5.jpg      |

libraries/Store.php
Code:
<?php
    if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
    
    class Store 
    {
    
        var $info=array();
        
        public function __construct() 
        {
            $CI =& get_instance();
            $site = $CI->db->select("userID,store_background")
            ->where("userID", [HERE USER ID])
            ->get("store_info");
            
            if($site->num_rows() == 0) {
                $CI->template->error(
                    "You are missing the site settings database row."
                );
            } else {
                $this->info = $site->row();
            }
        }
    
    }
    
    ?>



Thanks
1. If u have session, get it from session $CI->session->userdata('userId')
2. u can also path it as a parameter for library
$params = array('type' => 'large', 'color' => 'red');
$this->load->library('Someclass', $params);
To work and use CodeIgniter inside a library you need to get the CI Super Object Instance.

PHP Code:
class Your_Name {

    
/**
     * @var
     * Holds the CI Super Object.
     */
    
private $CI;

    
/**
     * __construct ()
     * --------------------------------------------------------------------
     *
     * Constructor    PHP 5+
     *
     * NOTE: Not needed if not setting values or extending a Class.
     *
     */
    
public function __construct()
    {
        
$this->CI =& get_instance();
    }



Then when you need to access you use this.

PHP Code:
$someThing $this->CI->yourThing(); 

But for user id's, information etc; Use the session.