Open Source PHP Framework (originally from EllisLab) CodeIgniter Web Framework

insert multiple rows via a php array into mysql

I'm passing a large dataset into a mysql table via php using insert commands and I'm wondering if its possible to insert approximately 1000 rows at a time via a query other than appending each value on the end of an mile long string and then executing it. I am using the codeigniter framework so its functions are also available to me.

Source: (StackOverflow)

the best way to make codeigniter website multi-language. calling from lang arrays depends on lang session?

I'm researching hours and hours, but I could not find any clear, efficient way to make it :/

I have a codeigniter base website in English and I have to add a Polish language now. What is the best way to make my site in 2 language depending visitor selection?

is there any way to create array files for each language and call them in view files depends on Session from lang selection? I don't wanna use database.

Appreciate helps! I'm running out of deadline :/ thanks!!

Source: (StackOverflow)


get_instance() in Codeigniter: Why assign it to a variable?

In Codeigniter, get_instance() is a globally available function that returns the Controller super-object which contains all the currently loaded classes (it returns the Controller class instance). I'll include the current source code:

get_instance() is defined in Codeigniter.php

// Load the base controller class
require BASEPATH.'core/Controller.php';

function &get_instance()
    return CI_Controller::get_instance();

And CI_Controller is defined in Controller.php

class CI_Controller {

    private static $instance;

     * Constructor
    public function __construct()
        self::$instance =& $this;

        // Assign all the class objects that were instantiated by the
        // bootstrap file (CodeIgniter.php) to local class variables
        // so that CI can run as one big super object.
        foreach (is_loaded() as $var => $class)
            $this->$var =& load_class($class);

        $this->load =& load_class('Loader', 'core');


        log_message('debug', "Controller Class Initialized");

    public static function &get_instance()
        return self::$instance;

Here's how it is recommended to be used in the user guide for creating libraries:

Utilizing CodeIgniter Resources within Your Library

To access CodeIgniter's native resources within your library use the get_instance() function. This function returns the CodeIgniter super object.

Normally from within your controller functions you will call any of the available CodeIgniter functions using the $this construct: $this->load->helper('url'); $this->load->library('session'); $this->config->item('base_url'); etc.

$this, however, only works directly within your controllers, your models, or your views. If you would like to use CodeIgniter's classes from within your own custom classes you can do so as follows:

First, assign the CodeIgniter object to a variable:

$CI =& get_instance();

Once you've assigned the object to a variable, you'll use that variable instead of $this: $CI =& get_instance(); $CI->load->helper('url'); $CI->load->library('session'); $CI->config->item('base_url'); etc.

Note: You'll notice that the above get_instance() function is being passed by reference:

$CI =& get_instance();

This is very important. Assigning by reference allows you to use the original CodeIgniter object rather than creating a copy of it.

Related posts: explain $CI =& get_instance(); / Codeigniter: Get Instance

So, here is my actual question:

Why does the user guide recommend assigning get_instance() to a variable? I'm fairly certain I understand the implications of not assigning by reference, but why is it recommended to assign it to a variable when get_instance()->load->model() works fine?

I see a lot of user defined or third party classes in CI that assign to a property of the object:

class MY_Class {

    private $CI;

    function __construct()
        $this->CI =& get_instance();
    function my_func()
    function my_other_func()

Poor example, but I see this frequently. Why bother with this method instead of just calling get_instance() directly? It seems like assigning the entire Controller object to a class variable wouldn't be a great idea, even if it is a reference. Maybe it doesn't matter.

I want to write a wrapper function for get_instance() so it's easier to type, and I don't have to constantly assign it to a variable.

function CI()
    return get_instance();


function CI()
    $CI =& get_instance();
    return $CI;

Then I could use CI()->class->method() from anywhere without the hassle of assigning it to a variable, it's very easy to write and understand what it does, and can result in shorter, more elegant code.

  • Is there any reason not to take this approach?
  • Is there any difference between the two CI() functions above?
  • Why is it recommended to assign get_instance() to a variable rather than calling it directly?
  • What does the & in function &get_instance(){} mean where it is defined? I know a bit about what references are for and I use them when appropriate, but I've never seen a function defined this way. If I do write a wrapper function, should I use this as well?

Please note that this is not so much a style question, but a technical one. I want to know if there are any issues, performance or otherwise, with using the method I'm suggesting.

EDIT: So far we have:

  • Method chaining is not available in php4, so assigning to a variable is a workaround (although this is fairly irrelevant as Codeigniter has dropped php4 support)
  • The minor overhead of calling a function more than once to return the object, rather than calling it once and assigning to a variable.

Anything else, or are these the only potential issues?

Source: (StackOverflow)

How to print SQL statement in codeigniter model

I have a sql statement in my model,

I then say

 $query = $this->db->query($sql, array(fields, fields1);

if($query){return true:}
else{echo "failed"; return false;}

My query always fails, how do I get php to print the exact sql statement being sent to my database? And display that on my php view, page

Source: (StackOverflow)

explain $CI =& get_instance();

looking through code igniters source code,

in its helper functions I keep seeing $CI =& get_instance(); can anyone please explain to me how this works?

I get that it is returning a reference to the $CI super object, but where does get_instance() come from?

Source: (StackOverflow)

How should I choose an authentication library for CodeIgniter? [closed]

I see there are a few. Which ones are maintained and easy to use? What are their pros and cons?

Source: (StackOverflow)

CodeIgniter: Create new helper?

I need to loop lot of arrays in different ways and display it in a page. The arrays are generated by a module class. I know that its better not to include functions on 'views' and I want to know where to insert the functions file.

I know I can 'extend' the helpers, but I don't want to extend a helper. I want to kind of create a helper with my loop functions.. Lets call it loops_helper.php

Source: (StackOverflow)

Fatal Error: Allowed Memory Size of 134217728 Bytes Exhausted (CodeIgniter + XML-RPC)

I have a bunch of client point of sale (POS) systems that periodically send new sales data to one centralized database, which stores the data into one big database for report generation.

The client POS is based on PHPPOS, and I have implemented a module that uses the standard XML-RPC library to send sales data to the service. The server system is built on CodeIgniter, and uses the XML-RPC and XML-RPCS libraries for the webservice component. Whenever I send a lot of sales data (as little as 50 rows from the sales table, and individual rows from sales_items pertaining to each item within the sale) I get the following error:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 54 bytes)

128M is the default value in php.ini, but I assume that is a huge number to break. In fact, I have even tried setting this value to 1024M, and all it does is take a longer time to error out.

As for steps I've taken, I've tried disabling all processing on the server-side, and have rigged it to return a canned response regardless of the input. However, I believe the problem lies in the actual sending of the data. I've even tried disabling the maximum script execution time for PHP, and it still errors out. Thank you so much for your help!

Source: (StackOverflow)

Codeigniter - no input file specified

Hi I am a beginner in Codeigniter and I saw a CI tutorial and was just trying to do a simple thing. I downloaded the CI and added this file to controller directory, but it won't work.


class site extends CI_Controller

        public function index()
                echo "Hello World";

        function dosomething()
                echo "Do Something";



When I try to access it using http://..../index.php/site I get the output ... "no input file specified" .... by the way, I named the file site.php

Source: (StackOverflow)

MongoDB and CodeIgniter [closed]

Can anyone assist in pointing me to a tutorial, library, etc. that will allow me to work with MongoDB from CodeIgniter?

Any help is really appreciated.

Source: (StackOverflow)

Where do I put image files, css, js, etc. in Codeigniter?

Where is it acceptable to put css folders and image file folders? I was thinking inside the view folder? However the controller always reroutes the path to the base url so I have to specify the path in the .html file to where it sits, which is redundant.

Source: (StackOverflow)

CodeIgniter activerecord, retrieve last insert id?

Are there any options to get the last insert id of a new record in CodeIgniter?

$last_id = $this->db->insert('tablename',
    array('firstcolumn' => 'value',
    'secondcolumn' => 'value')

Considering the table consits of fields id (autoincrement) firstcolumn and secondcolumn.

This way you can use the insert id in the following code.

Source: (StackOverflow)

Codeigniter - handling errors when using active record

I am putting together a few models for my codeigniter site and can't seem to find any word in the documentation of how to handle errors that could occur when using the Active Record system.

The documentation demonstrates how to perform CRUD along with some relatively involved queries but no where along the line is error handling discussed. I have done a quick google search and it would appear that the Active Record classes do not throw exceptions. Is this the case? No try catch then...

So, how do you code to handle database errors in codeigniter? (failed connection, duplicate key, broken referential integrity, truncation, bad data types etc etc)

Source: (StackOverflow)

Sending email with gmail smtp with codeigniter email library

class Email extends Controller {

    function Email()

    function index()
        $config['protocol']    = 'smtp';
        $config['smtp_host']    = 'ssl://smtp.gmail.com';
        $config['smtp_port']    = '465';
        $config['smtp_timeout'] = '7';
        $config['smtp_user']    = 'mygmail@gmail.com';
        $config['smtp_pass']    = '*******';
        $config['charset']    = 'utf-8';
        $config['newline']    = "\r\n";
        $config['mailtype'] = 'text'; // or html
        $config['validation'] = TRUE; // bool whether to validate email or not      


        $this->email->from('mygmail@gmail.com', 'myname');

        $this->email->subject('Email Test');
        $this->email->message('Testing the email class.');  


        echo $this->email->print_debugger();


I am getting this error:

A PHP Error was encountered
Severity: Warning
Message: fsockopen() [function.fsockopen]: unable to connect to ssl://smtp.gmail.com:465 (Connection timed out)
Filename: libraries/Email.php
Line Number: 1641

Using PORT 25/587

I got this error:

A PHP Error was encountered
Severity: Warning
Message: fsockopen() [function.fsockopen]: SSL operation failed with code 1. OpenSSL Error messages: error:140770FC:SSL routines:func(119):reason(252)
Filename: libraries/Email.php
Line Number: 1641

I don't want to use phpmailer now. (Actually I have tried to use phpmailer, but I failed).

How do I solve this problem guys?

Source: (StackOverflow)

how to get last insert id after insert query in codeigniter active record

I have an insert query for used to insert the form fileds into a mysql table. I want to get the last incremented id for the insert operation as the return of my query but I have some problems with it! bellow is what i have done!

Inside the controller:

function add_post(){
    $post_data = array(
        'id'            => '',
        'user_id'   =>  '11330',
        'content'   =>  $this->input->post('poster_textarea'),
        'date_time' => date("Y-m-d H:i:s"),
        'status'        =>  '1'
    return $this->blog_model->add_post($post_data);

And inside model:

function add_post($post_data){
    return $this->db->insert_id();

I get nothing as the return of the add_post in model!

Source: (StackOverflow)