Category Archives: Codeigniter

Delete Directory in Codeigniter


Delete Directory in Codeigniter You can use file helper in codeigniter to delete files. Using this helper you can delete files in folder.


Delete Directory in Codeigniter Example

Here in an example of deleting all files in folder-

Delete Directory in Codeigniter Example:

$path = $this->config->base_url().'images';
$this->load->helper("file"); // load the helper
delete_files($path, true); // delete all files/folders inside images folder

It will delete all folders/files inside the images folder.

Codeigniter check HTTP method


Codeigniter check HTTP method– It is very simple we can detect the HTTP request method whether it is POST Or GET. You can use input library to check the HTTP method.


Codeigniter check HTTP method Example

Using input library you can check the get or post method simply as below-

| Example:

 if($this->input->post()){
  // do something
 }
 if($this->input->get()){
  // do something
 }

Thus you can detect the post method or get method HTTP requst.

Codeigniter Create Directory


Codeigniter Create Directory– You can create directory in codeigniter same as core PHP. Here in this tutorial, we are going to explain how to create directory in codeigniter in standard way.


Codeigniter Create Directory | Folder Example

You can create directory in codeigniter simply as below –

Codeigniter Create Directory Example:

 $path = "images/profile";

    if(!is_dir($path)) //create the folder if it's not exists
    {
      mkdir($path,0755,TRUE);
    } 

The above example will create folder profile in images folder if it does not exists. It will assign the folder permission 0755 so you can add permission as per your need.

Codeigniter Load Multiple Models


Codeigniter Load Multiple Models– There is no limitation to load models in controller function, you can load multiple Models in same function. Here in this tutorial we are going to explain how you can load multiple models in a controller function.


Codeigniter Load Multiple Models In Controller Function Example

Multiple models can be loaded simply as below-

Codeigniter Load Multiple Models Example:

load->model('users');
        $this->load->model('profile');
        $this->load->model('logs');
    }

    function seeAllUserData()
    {
        $data['users'] = $this->users->getAllUsers();     
        $data['profiles'] = $this->profile->getAllProfiles();    
        $data['logs'] = $this->profile->getAllLogs();  
        $this->load->view('my_view', $data);
    }
}

Codeigniter Add Js Css File


Codeigniter Add Js Css File There are many ways to add the js and css files in Codeigniter but the simple way is add js css path in header using base url.


Codeigniter Add Js Css File Example

The simplest and minimal way to include js and css is using base url. In the below example we have created folder named as assets and two folders inside it – js and css which contains the js and css files.

Codeigniter Add Js Css File Example:



On the same way you can include other files in codeigniter.

Codeigniter get project base path


Codeigniter get project base path– You can use FCPATH or BASEPATH to get the project base path.


Codeigniter get project base path | Base Directory | Document Root Example

The constants FCPATH or BASEPATH will give you the directory base path where your project is setup.

Codeigniter get project base path Example:

$projectPath = FCPATH;

Codeigniter multiple where Condition


Codeigniter multiple where Condition – It is very simple to add multiple where condition filter in Codeigniter. Codeigniter where condition accepts the conditions as an associative array so can pass multiple conditions easily. Here in this tutorial, we are going to explain how to add multiple where conditions in where clause.


Codeigniter multiple where Condition Example

You can add multiple where condition simply as below-

Codeigniter multiple where Condition Example:

$where_array = array(
               'email'=>'test@gmail.com',
               'status'=>'1'
              );
$table_name = "users";
$limit = 10;
$offset = 0;
$query = $this->db->get_where($table_name,$where_array, $limit, $offset);

If you run the above example it will produce the output something like this –

SELECT * from users where email='test@gmail.com' AND status ='1' limit 0, 10;

On the same way you can add multiple conditions.

Codeigniter Order By Query


Codeigniter Order By Query – We often need to add the order by clause on queries. It is very simple to use order by clause on CodeIgniter query. Here in this article we are going to explain how to add order by on Active records.


Codeigniter Order By Query ASC | DESC Example

You can add order by filter simly as below-

Codeigniter Order By Query Example:

$this->db->from('my_table');
$this->db->order_by("column1 asc,column2 desc");
$query = $this->db->get(); 
return $query->result();

You can add multiple filers separated by comma(,).

Tags

#orderBy Query

Codeigniter Order By Query not Working

Codeigniter Order By Clause

Codeigniter File Upload


Codeigniter File Upload: Codeigniter provides uploading classes to upload the files. It provides several options to set restrictions(such as file size, file type) and preferences of the file. Here in this tutorial we are going to explain how you can upload files/Images in Codeigniter.


Codeigniter File Upload | Image Upload Library Example

Let us go step by step to understand how to upload files/images in Codeigniter.

Steps to Upload Files

Following steps are main steps to upload files in codeigniter-

  • Upload Form(Html View)
  • Controller
  • Success Page(Html View)

Upload Form(Html View)

Create a view file as upload_form_view.php in application/views folder. This Html Form contains the form input to select and upload the file –

Codeigniter File Upload | Html Part Example:

















Controller

Create a controller file as upload.php in application/controllers folder. This will contain the code to upload file on server as below-

Codeigniter File Upload Controller Example:

load->helper(array('form', 'url'));
        }

        public function index()
        {
                $this->load->view('upload_form_view', array('error' => ' ' ));
        }

        public function process_upload()
        {
                $config['upload_path']          = './uploads/';
                $config['allowed_types']        = 'gif|jpg|png';
                $config['max_size']             = 2048;
                $config['max_width']            = 1000;
                $config['max_height']           = 800;
                $this->load->library('upload', $config);

                if ( ! $this->upload->process_upload('myfile'))
                {
                        $error = array('error' => $this->upload->display_errors());

                        $this->load->view('upload_form_view', $error);
                }
                else
                {
                        $data = array('upload_data' => $this->upload->data());

                        $this->load->view('success_view', $data);
                }
        }

}
?>

In controller method process_upload $config array is used to set the preferences and restrictions. Let us have look on the config –

  • $config[‘upload_path’] : This is used to set the directory path where images should be uploaded.
  • $config[‘max_size’] : This is used to set the maximum file size allowed to be uploaded.
  • $config[‘allowed_types’] : This is used to set the file types that will be allowed for upload for example jpg, png etc.
  • $config[‘max_width’] : This will set the max with of file allowed.
  • $config[‘max_height’] : This will set the max height of file allowed.

Success Page(Html View)

Create a view file as success_view.php in application/views folder. This view will be loaded when the file is uploaded successfully.

Success Page View:





The file was successfully uploaded.

    $value):?>
  • :