Welcome Guest, Not a member yet? Register   Sign In
How to get user ID in libraries CodeIgniter
#1

(This post was last modified: 05-29-2017, 09:04 PM by googlemy.)

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
Reply
#2

(This post was last modified: 05-29-2017, 11:06 PM by neuron.)

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);
Reply
#3

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB