Learn, Share, Build

195
September 21, 2017, at 5:51 PM

I am trying to load a different database based on the session variable in codeigniter, But i get an error An Error Was Encountered You have not selected a database type to connect to. Below i have my admin_init_elements library file

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class admin_init_elements
{
    var $CI;
    function  __construct()
    {
        $this->CI =& get_instance();
        $this->CI->load->helper('cookie'); 
        $CI =& get_instance(); 
        $DB = $CI->session->userdata('company');
        $DB2 = $CI->load->database($DB, TRUE);
    }
    function init_head($chklogin)
    {
        global $SITE,$USER;
        //populate default meta values for any controller
        $this->CI->mod_common->set_global_site_options();
        $data['admin_logged_in_status']=$this->admin_logged_in_status();
        $data['old_images']=$this->CI->mod_common->getBgImages();
        if($data['admin_logged_in_status']){
            $data['lastlogin_details'] = $this->admin_lastlogin_details(); 
            $this->set_global_user($this->CI->session->userdata('admin_user_name'),$this->CI->session->userdata('admin_userpass'));
            if($USER->id > 0)
            {
            } else 
            {
                $data['page_heading'] = $page_heading;
                echo '<p style="color:green;text-align:center;padding:20px">Access Denied. You seem to be accessing another client\'s information. </p>';
                die();
            }
            $data['notifications']  = $this->CI->mod_common->get_notifications();
            $data['topnotifications']   = $this->CI->mod_common->get_notifications_list();
            //$data['openpositions'] = $this->CI->mod_common->get_opportunity_list();
            $data['openpositions_referred'] = $this->CI->mod_common->get_opportunity_list_referred();
            $data['new_joinees']    = $this->CI->mod_common->get_newJoineesList();
            $data['new_kudos']  = $this->CI->mod_common->get_newKudos(4);

            $data['awardsList']=$this->CI->mod_common->get_awardsList(2);
            //$data['questions']=$this->CI->mod_common->get_pollingList(1);
            $data['current_user_resp']=$this->CI->mod_common->current_user_answers();
            $data['current_user_ans'] = array_keys($data['current_user_resp']);
            $data['if_manager']=$this->CI->mod_common->if_manager($USER->id);
            $data['quick_links'] = $this->CI->mod_common->get_cat_array2('pr_quicklinks','','','title');
            $comp_struct_elements=$this->CI->mod_common->get_array_frontend_raw('pr_package','*','');
            foreach($comp_struct_elements as $ce)
            {
                $data['modules'][$ce->name] = '1';
            }
            if($this->CI->mod_common->get_value_by_id('is_global','users',$this->CI->session->userdata('admin_id')==0)) {
                //Validate IP address from DB [start]
                $ip_addresses   = $this->CI->mod_common->get_array_frontend('ip_address','ip_address');
                foreach($ip_addresses as $ips){
                    $ip_add_array[] = $ips->ip_address;
                }
                if(!in_array($this->CI->input->ip_address(), $ip_add_array)){
                    exit('Invalid IP address. Entry restricted.');
                }
                //Validate IP address from DB [end]
            }
        }
        $data['cookiename'] = $this->CI->input->cookie('name', false);
        $data['cookieimage'] = $this->CI->input->cookie('image', false);
        $clients= currentuserclients();
            if(count($clients)){
                foreach($clients as $c){
                    if($c->logoimage!='' && file_exists(FCPATH.$SITE->client_logo_image_src.FOLDER_THUMB.$c->logoimage)){
                        $imgsrc = $SITE->client_logo_image_src.FOLDER_THUMB.$c->logoimage;
                    }else{
                        $imgsrc = "http://www.placehold.it/291x170/EFEFEF/AAAAAA&amp;text=no+image";
                    }
                    $data['clientsdata'][] =  "{ id:".$c->id .", name:'". $c->clientname ."', 'avatar':'".$imgsrc."','type':'client' }";
                }
            }
            $data['userids'] = $this->CI->mod_common->get_array_frontend_with_join("users_details","id, email, CONCAT(firstname,' ',lastname) as firstname","userid > 0","users");
            if(count($data['userids'])){
                foreach($data['userids'] as $u){
                    $data['useridsoptions'][$u->id] = ucfirst (strtolower($u->firstname)).' '.ucfirst (strtolower($u->lastname));
                }
            }
        //$ip_addresses = $this->CI->mod_common->get_array_frontend('ip_address','ip_address'); 
        $data['all_posted_jobs']=$this->CI->mod_common->get_posted_jobs();  
        //print_r($data['all_posted_jobs']);
        $this->CI->data['head'] = $this->CI->load->view('elements/head', $data, true);
         $this->CI->data['old_head'] = $this->CI->load->view('elements/old_head', $data, true);
        $this->CI->data['page_header'] = $this->CI->load->view('elements/page_header', $data, true);
        $this->CI->data['left_menu'] = $this->CI->load->view('elements/left_menu', $data, true);
        $this->outputData['listOfUsers'] = $this->CI->mod_common->getUsers();
        $this->CI->data['footer_scripts'] = $this->CI->load->view('elements/footer_scripts', $data, true);
        $this->CI->data['right_menu'] = $this->CI->load->view('elements/right_menu', $this->outputData, true);
        $this->CI->data['left_profile_menu'] = $this->CI->load->view('elements/left_profile_menu', $data, true);
        $this->CI->data['footer'] = $this->CI->load->view('elements/footer', $data, true);
    }
    function init_elements($chklogin='Y')
    {
        //index call to populate all the header / footer / side bar elements
        if($chklogin=='Y'){
            $this->is_admin_logged_in();
        }
        $this->init_head($chklogin);
    }
    function is_admin_logged_in()
    {
        global $USER;
        $is_admin_logged_in = $this->CI->session->userdata('is_admin_logged_in');
        $lastrefresh = $this->CI->session->userdata('lastrefresh');
        if(!isset($is_admin_logged_in) || $is_admin_logged_in != TRUE){
            $query1 = $DB2->query("Select * from pr_system_attendance where userid = '".$USER->id."' and DATE(`login_time`) = CURDATE() and login_ip = '".$_SERVER['REMOTE_ADDR']."'");
                if($query1->num_rows() > 0)
                {
                    $row = $query1->row();
                    $sql1 = "UPDATE pr_system_attendance set logout_time = '".date('Y-m-d H:i:s')."'  where id='".$row->id."'";
                    $query2 = $DB2->query($sql1);
                }
            redirect('login');
        }else{
             if(!isset($lastrefresh) || $lastrefresh < 0){
                 $query1 = $DB2->query("Select * from pr_system_attendance where userid = '".$USER->id."' and DATE(`login_time`) = CURDATE() and login_ip = '".$_SERVER['REMOTE_ADDR']."'");
                if($query1->num_rows() > 0)
                {
                    $row = $query1->row();
                    $sql1 = "UPDATE pr_system_attendance set logout_time = '".date('Y-m-d H:i:s')."'  where id='".$row->id."'";
                    $query2 = $DB2->query($sql1);
                }
                  redirect('');
             }elseif(time() > $lastrefresh + LOGIN_TIMEOUT){                 
                 $this->CI->session->set_userdata(array('session_msg' =>'Session Expired. Please Try Again', 'session_msg_class' => 'gagal' )); 
                 $query1 = $DB2->query("Select * from pr_system_attendance where userid = '".$USER->id."' and DATE(`login_time`) = CURDATE() and login_ip = '".$_SERVER['REMOTE_ADDR']."'");
                if($query1->num_rows() > 0)
                {
                    $row = $query1->row();
                    $sql1 = "UPDATE pr_system_attendance set logout_time = '".date('Y-m-d H:i:s')."'  where id='".$row->id."'";
                    $query2 = $DB2->query($sql1);
                }
                  redirect('login');
             }
        }
    }
    function admin_logged_in_status(){
        $is_admin_logged_in = $this->CI->session->userdata('is_admin_logged_in');
        $lastrefresh = $this->CI->session->userdata('lastrefresh');
        if(!isset($is_admin_logged_in) || $is_admin_logged_in != TRUE){
            $status=FALSE;
        }else{
             if(!isset($lastrefresh) || $lastrefresh < 0){
                 $status = FALSE;
             }elseif(time() > $lastrefresh + LOGIN_TIMEOUT){
                 $status = FALSE;
                 $this->CI->session->set_userdata(array('session_msg' =>'Session Expired. Please Try Again', 'session_msg_class' => 'gagal' )); 
             }else{
                 $status=TRUE;
             }
        }
        return $status;
    }
    function admin_lastlogin_details(){
        $lastlogin_det['lastlogin'] = $this->CI->session->userdata('admin_lastlogin');
        $lastlogin_det['lastloginip'] = $this->CI->session->userdata('admin_lastloginip');
        return $lastlogin_det;
    }
    function do_upload_docs($_files_tmp_name,$upload_path,$return_filename = true){
    //echo $upload_path;die;
        if($_FILES[$_files_tmp_name]['name']!=''){
            $source = './'.$upload_path.FOLDER_DOCS;
            $config['upload_path'] = $source;
            $config['allowed_types'] = '*';
            $filename = time()."_".rand();
            $config['file_name']    = $filename;    
            $this->CI->load->library('upload', $config);
            $this->CI->upload->initialize($config);
            if (! $this->CI->upload->do_upload($_files_tmp_name)){
                $error = array('err'=>'1','error' => $this->CI->upload->display_errors('<div class="gagal"> Please Try Again.', '</div>'));
                return $error;
            }else{
                $data = array('err'=>'0','upload_data' => $this->CI->upload->data());
                $source_img=$data['upload_data']['full_path'];
                if($return_filename){
                    return $data['upload_data']['file_name'];
                }
                return $data;
            }
        }
    }
    function do_upload_img($_files_tmp_name,$upload_path,$return_filename = true){
    //echo $upload_path;die;

        if($_FILES[$_files_tmp_name]['name']!=''){
            $img_config = $this->CI->config->item('upload_images');
            $source = './'.$upload_path.FOLDER_ORIGINAL;
            $config['upload_path'] = $source;
            $config['allowed_types'] = 'gif|jpg|png';
            $filename = time()."_".rand();
            $config['file_name']    = $filename;            
            $this->CI->load->library('upload', $config);
            $this->CI->upload->initialize($config); 
            if (! $this->CI->upload->do_upload($_files_tmp_name)){
                $error = array('err'=>'1','error' => $this->CI->upload->display_errors('<div class="gagal"> Please Tyr Again.', '</div>'));
                return $error;
            }else{  
                $data = array('err'=>'0','upload_data' => $this->CI->upload->data());
                $source_img=$data['upload_data']['full_path'];
                foreach($img_config as $key=>$val){
                    $config['image_library'] = 'gd2';
                    $config['source_image'] =$source_img;
                    $config['thumb_marker'] = '';
                    $config['new_image'] = str_replace(FOLDER_ORIGINAL,$key,$source_img);
                    $config['create_thumb'] = TRUE;
                    $config['maintain_ratio'] = TRUE;
                    $config['width'] = $val['width'];
                    $config['master_dim'] = 'width';
                    $config['height'] = 1;
                    $this->CI->load->library('image_lib', $config);
                    $this->CI->image_lib->initialize($config);
                    if ( ! $this->CI->image_lib->resize()){
                        $error = array('err'=>'1','error'=> $this->CI->image_lib->display_errors('<div class="gagal"> Please Tyr Again.', '</div>'));
                        return $error;          
                    }
                }
                if($return_filename){
                    return $data['upload_data']['file_name'];
                }
                return $data;
            }
        }
    }








    /*$img_config = $this->CI->config->item('upload_images');
            $source = './'.$upload_path.FOLDER_ORIGINAL;
            $config['upload_path'] = $source;
            $config['allowed_types'] = 'gif|jpg|png';
            $filename = time()."_".rand();
            $config['file_name']    = $filename;            
            $this->CI->load->library('upload', $config);
            $this->CI->upload->initialize($config); 
            if (! $this->CI->upload->do_upload($_files_tmp_name)){
                $error = array('err'=>'1','error' => $this->CI->upload->display_errors('<div class="gagal"> Please Tyr Again.', '</div>'));
                return $error;
            }else{*/




    function do_proof_upload($_files_tmp_name,$upload_path,$return_filename = true)
    {
        if($_FILES[$_files_tmp_name]['name']!=''){
            $source = './'.$upload_path.FOLDER_ORIGINAL;
            $config['upload_path'] = $source;
            $config['allowed_types'] = "pdf|doc|xml|docx|PDF|DOC|XML|DOCX|xls|xlsx|jpg|png";
            $filename = time()."_".rand();
            $config['file_name']    = $filename;
            $this->CI->load->library('upload', $config);
            $this->CI->upload->initialize($config); 
            if (! $this->CI->upload->do_upload($_files_tmp_name)){
                $error = array('err'=>'1','error' => $this->CI->upload->display_errors('<div class="gagal"> Please Tyr Again.', '</div>'));
                return $error;
            }else{
                //echo $_FILES[$_files_tmp_name]['name'];
                $expl=explode('.',$_FILES[$_files_tmp_name]['name']);
                //echo $expl[1];
                $fname= $filename.'.'.$expl[1];
                return $fname;
                //echo 'y';
                //die;
            }
        }
    }

    function do_resume_upload($_files_tmp_name,$upload_path,$return_filename = true)
    {
        if($_FILES[$_files_tmp_name]['name']!=''){
            $source = './'.$upload_path.FOLDER_ORIGINAL;
            $config['upload_path'] = $source;
            $config['allowed_types'] = "pdf|doc|xml|docx|PDF|DOC|DOCX|jpg|png|gif|jpeg";
            $filename = time()."_".rand();
            $config['file_name']    = $filename;
            $this->CI->load->library('upload', $config);
            $this->CI->upload->initialize($config); 
            if (! $this->CI->upload->do_upload($_files_tmp_name)){
                $error = array('err'=>'1','error' => $this->CI->upload->display_errors('<div class="gagal"> Please Tyr Again.', '</div>'));
                return $error;
            }else{
                //echo $_FILES[$_files_tmp_name]['name'];
                $expl=explode('.',$_FILES[$_files_tmp_name]['name']);
                //echo $expl[1];
                $fname= $filename.'.'.$expl[1];
                return $fname;
                //echo 'y';
                //die;
            }
        }
    }
    function do_resume_upload_pdf($_files_tmp_name,$upload_path,$return_filename = true)
    {
        if($_FILES[$_files_tmp_name]['name']!=''){
            $source = './'.$upload_path.FOLDER_ORIGINAL;
            $config['upload_path'] = $source;
            $config['allowed_types'] = "pdf|doc|docx|PDF|DOC|DOCX";
            $filename = time()."_".rand();
            $config['file_name']    = $filename;
            $this->CI->load->library('upload', $config);
            $this->CI->upload->initialize($config); 
            if (! $this->CI->upload->do_upload($_files_tmp_name)){
                $error = array('err'=>'1','error' => $this->CI->upload->display_errors('<div class="gagal"> Please Tyr Again.', '</div>'));
                return $error;
            }else{
                //echo $_FILES[$_files_tmp_name]['name'];
                $expl=explode('.',$_FILES[$_files_tmp_name]['name']);
                //echo $expl[1];
                $fname= $filename.'.'.$expl[1];
                return $fname;
                //echo 'y';
                //die;
            }
        }
    }
    function delete_rows($tableName=NULL)
    {
        #echo $tableName.$primary_key;
        $data['error_message']='';  
        if($this->CI->input->post('action')=='_doDelete'){  
            $data['error_message']=$this->CI->mod_common->delete($tableName,$this->CI->input->post('id'));  
        }
    }
    function delete_rows2($tableName=NULL)
    {
        #echo $tableName.$primary_key;
        $data['error_message']='';  
        if($this->CI->input->post('action')=='_doDelete'){  
            $data['error_message']=$this->CI->mod_common->delete($tableName,$this->CI->input->post('id'));  
        }
    }
    ##################################################################
    ##      COMMON PAGINATION FOR PRODUCTS & PRODUCT CATEGORY       ##
    ##################################################################
    function getPagination($uri,$segment,$viewFile,$baseURL='',$myurl='',$tableName=NULL,$columnName,$list_heading=NULL,$orderby=NULL,$status_col=NULL,$joinwith=NULL,$img_folder=NULL){
        #echo $tableName.$primary_key;
        $data['error_message']='';  
        if($this->CI->input->post('action')=='_doDelete'){              
            $data['error_message']=$this->CI->mod_common->delete($tableName,$this->CI->input->post('id'));  
        }
        if($this->CI->input->post('action')=='_doChangeStatus'){
            //print_r($_POST);exit; 
            if($tableName == 'users_details') $temptableName = 'users'; else $temptableName = $tableName;
            $data['error_message']=$this->CI->mod_common->change_status($temptableName,$this->CI->input->post('id'),$status_col,$this->CI->input->post('status'),$this->CI->input->post('statusMsg'));
        }
        if($img_folder!=NULL){
            $data['img_folder'] = $img_folder;
        }
        $data['myurl'] =$myurl;
        $config['base_url'] = base_url().$data['myurl'].$baseURL;
        $config['per_page'] =$this->CI->config->item('admin_show_items_per_page');  
        $config['total_rows']  = $this->CI->mod_common->get_rows($config['per_page'],$uri,$tableName,$columnName,$orderby,$joinwith,true);  
        $config['uri_segment'] = $segment;
        $config['num_links'] = 1;
        $config['full_tag_open'] = '<div class="pagination">';
        $config['full_tag_close'] = '</div>';
        $config['prev_link'] = '&lt;Previous ';
        $config['next_link'] = ' Next&gt;';
        $this->CI->pagination->initialize($config);
        $data['pagination_link'] = $this->CI->pagination->create_links();
        $data['serial_no']=1;
        $data['list_heading']   = $list_heading;
        if(!empty($uri)){
            $data['serial_no']=$uri+1;
        }
        $data['uri']=$uri;
        $data['rows'] = $this->CI->mod_common->get_rows($config['per_page'],$uri,$tableName,$columnName,$orderby,$joinwith);
        if(count($data['rows'])==0){
            $uri=abs($uri-$config['per_page']);
            $data['rows'] =  $this->CI->mod_common->get_rows($config['per_page'],$uri,$tableName,$columnName,$orderby,$joinwith);
        }
        $retVal = $this->CI->load->view('maincontents/'.$viewFile, $data, true);
        return $retVal;
    }
    ##################################################################
    ##      COMMON PAGINATION FOR PRODUCTS & PRODUCT CATEGORY       ##
    ##################################################################
    function set_global_user($username,$userpass){      
        $this->CI->mod_common->set_global_user($username,$userpass);
    }
    function set_post_vals($post_array){
        $retval = new stdClass();
        if(is_array($post_array) && count($post_array)){
            foreach($post_array as $key=>$val){
                $retval->$key = $val;
            }
        }
        if(!isset($retval->status)){
            $retval->status = 1;
        }
        return $retval;
    }

    /*---------------------------------------*/



}
?>

I know the error is in the constructor line $DB2 = $CI->load->database($DB, TRUE); , I have to insert , update and delete from the selected db otherwise it takes default db and inserts there.

Rent Charter Buses Company
READ ALSO
Learn, Share, Build

Learn, Share, Build

First apologies, i'm a total newby so still figuring out very basics! Basically i've got 2 recordsets (thnks to Dreamweaver), one providing me with filter buttons and one providing a full list of resultsI can't figure out how to combine these so that the results...

270
Learn, Share, Build

Learn, Share, Build

I copied an sql text file into /var/lib/mysql-files directory but the command:

225
Learn, Share, Build

Learn, Share, Build

I have several tables with columns: Value and PointsBased on the value that the user has , it needs to take the appropriate Points and calculate it in a formula before storing in to the database, in the User table

217
Learn, Share, Build

Learn, Share, Build

I have a mysql database, setup with docker

238