Useful functions for CodeIgniter

Useful functions for CodeIgniter

CodeIgniter is comparatively small footprint and very good framework. Too many things are inbuilt in the CodeIgniter framework but sometime we need to wrap them again for more code re-usability. I am giving some example here which are really helpful in real-time CodeIgniter applications.

Helper function to detect browser agent:

/**
 * This method used to get current browser agent
 */
if(!function_exists('getBrowserAgent'))
{
    function getBrowserAgent()
    {
        $CI = get_instance();
        $CI->load->library('user_agent');
        $agent = '';
        if ($CI->agent->is_browser())
        {
            $agent = $CI->agent->browser().' '.$CI->agent->version();
        }
        else if ($CI->agent->is_robot())
        {
            $agent = $CI->agent->robot();
        }
        else if ($CI->agent->is_mobile())
        {
            $agent = $CI->agent->mobile();
        }
        else
        {
            $agent = 'Unidentified User Agent';
        }
        return $agent;
    }
}

Create password hash:

/**
 * This function used to generate the hashed password
 * @param {string} $plainPassword : This is plain text password
 */
if(!function_exists('getHashedPassword'))
{
    function getHashedPassword($plainPassword)
    {
        return password_hash($plainPassword, PASSWORD_DEFAULT);
    }
}

Verify the hashed password with your original password:

/**
 * This function used to generate the hashed password
 * @param {string} $plainPassword : This is plain text password
 * @param {string} $hashedPassword : This is hashed password
 */
if(!function_exists('verifyHashedPassword'))
{
    function verifyHashedPassword($plainPassword, $hashedPassword)
    {
        return password_verify($plainPassword, $hashedPassword) ? true : false;
    }
}

Loading header, footer and main views in CodeIgniter:

/**
 * This function used to load views
 * @param {string} $viewName : This is view name
 * @param {mixed} $headerInfo : This is array of header information
 * @param {mixed} $pageInfo : This is array of page information
 * @param {mixed} $footerInfo : This is array of footer information
 * @return {null} $result : null
 */
function loadViews($viewName = "", $headerInfo = NULL, $pageInfo = NULL, $footerInfo = NULL){
  $this->load->view('includes/header', $headerInfo);
  $this->load->view($viewName, $pageInfo);
  $this->load->view('includes/footer', $footerInfo);
}

Pagination function in CodeIgniter – to reduce the code:

/**
 * This function used provide the pagination resources
 * @param {string} $link : This is page link
 * @param {number} $count : This is page count
 * @param {number} $perPage : This is records per page limit
 * @return {mixed} $result : This is array of records and pagination data
 */
function paginationCompress($link, $count, $perPage = 10, $segment = 2) {
  $this->load->library ( 'pagination' );
  $config ['base_url'] = base_url () . $link;
  $config ['total_rows'] = $count;
  $config ['uri_segment'] = $segment;
  $config ['per_page'] = $perPage;
  $config ['num_links'] = 5;
  $config ['full_tag_open'] = '<nav><ul class="pagination">';
  $config ['full_tag_close'] = '</ul></nav>';
  $config ['first_tag_open'] = '<li class="arrow">';
  $config ['first_link'] = 'First';
  $config ['first_tag_close'] = '</li>';
  $config ['prev_link'] = 'Previous';
  $config ['prev_tag_open'] = '<li class="arrow">';
  $config ['prev_tag_close'] = '</li>';
  $config ['next_link'] = 'Next';
  $config ['next_tag_open'] = '<li class="arrow">';
  $config ['next_tag_close'] = '</li>';
  $config ['cur_tag_open'] = '<li class="active"><a href="#">';
  $config ['cur_tag_close'] = '</a></li>';
  $config ['num_tag_open'] = '<li>';
  $config ['num_tag_close'] = '</li>';
  $config ['last_tag_open'] = '<li class="arrow">';
  $config ['last_link'] = 'Last';
  $config ['last_tag_close'] = '</li>';

  $this->pagination->initialize ( $config );
  $page = $config ['per_page'];
  $segment = $this->uri->segment ( $segment );

  return array (
      "page" => $page,
      "segment" => $segment
  );
}

I am using above function effectively in CodeIgniter Admin Panel – CIAS. These functions are useful in any CodeIgniter project you have. Please take a look and enjoy.