Category Archives: Codeigniter

Codeigniter Calendar Library


Codeigniter Calendar Library: Codeigniter provides Calendar library which enables us to create calendar dynamically. It provides you flexibility to control the design of the calendar. You can also customize the calendar such as controlling look & feel and passing data into cells etc. Here in this tutorial we are going to explain how you can use this library to create calendars dynamically.


Codeigniter Calender Library

Let us go step by step to understand how codeigniter library can be used –

Load Calendar Library

Before using the calendar library you must load the calendar library. You can load calendar library simply as below –

Codeigniter Load Calendar Library Syntax:

$this->load->library('calendar');

Now after loading this library you can use it.

Displaying Calendar

Once the library is loaded you can display the calendar simply as below –

Display | Show Calendar Example:

$this->load->library('calendar');
echo $this->calendar->generate();

The above syntax will generate the calendar in the codeigniter. You can also pass data to your calendar cells.

Codeigniter Email Library


Codeigniter Email Library: Codeigniter provides email library to send emails. It is very easy to use this library and send emails. You can configure it on the fly. Here in this tutorial we are going to demonstrate how to use email library in codeigniter and send mails.


Codeigniter Email Library | Send Mail Example

First of all we need to load the mail library before using it. We can load library simply as below-

Syntax

Codeigniter Load Email Library Syntax

$this->load->library('email');

Now you can use it to send mails.

Example

Let us create a simple example to send email-

Codeigniter Send Email Example

$this->load->library('email');
$this->email->from('sender@example.com', 'Your Name');
$this->email->to('receiver@example.com');
$this->email->cc('ccmail@example.com');
$this->email->bcc('bcc@example.com');
$this->email->subject('Codeigniter Email Test');
$this->email->message('This is test mail.');
$this->email->send();

Learn More

Let us have look over more examples.


Send Attachments

You can also attach some file in email using the following method –

Codeigniter Send Email Example

$this->load->library('email');
$this->email->from('sender@example.com', 'Your Name');
$this->email->to('receiver@example.com');
$this->email->cc('ccmail@example.com');
$this->email->bcc('bcc@example.com');
$this->email->subject('Codeigniter Email Test');
$this->email->message('This is test mail.');
$this->email->attach('http://example.com/myfile.pdf');
// or just give file path $this->email->attach('path/myfile.pdf');
$this->email->send();

Send Inline Attachments

If you want to send inline attachments you can use $this->email->attachment_cid() to send inline attachments-

Codeigniter Send Inline(image) Attachments

$this->load->library('email');
$this->email->from('sender@example.com', 'Your Name');
$this->email->to('receiver@example.com');
$this->email->cc('ccmail@example.com');
$this->email->bcc('bcc@example.com');
$this->email->subject('Codeigniter Email Test');
$this->email->message('This is test mail.');
$filename = "images/profile.png";
$cid = $this->email->attachment_cid($filename);
$this->email->message('Image Attachments');
$this->email->send();

Codeigniter Count Rows Of Table


Codeigniter Count Rows Of Table : There are several ways developers use to get total number of rows in table. You can use $this->db->count_all(‘table_name’) to get the total rows of a table, this is . Here in this tutorial we are going to explain how you can use database class to get the total row count of a table.


Codeigniter Count Rows Of Table | Query Example

Codeigniter Count Rows Of Table

The syntax and example to count the rows is as –

Syntax

Codeigniter Count Rows Of Table : Query Syntax

$this->db->count_all('table_name');

Where table_name is name of the table.

After running the above query it will give the total count of rows in specified table.

Example

Now suppose we have table “users” and we want to count the number of rows in this table. The example to count rows in “users” table is as –

Codeigniter Count Rows Of Table : Query Example

$totalRows = $this->db->count_all('users');
echo $totalRows;

The above example will give you total number of rows in users table. Output will be an integer like : 90.

In case of joins use $query->num_rows() to count the results.

Codeigniter – no input file specified


Codeigniter – no input file specified : Sometimes we get error “no input file specified” in codeigniter. This problem comes due to htaccess error. You can easily fix this error by just adding the following change in .htaccess.


Codeigniter – no input file specified

Make sure “?” is added after index.php as below. After adding this it will solve your problem.

Codeigniter – no input file specified:

RewriteRule ^(.*)$ index.php?/$1 [L]

This will fix your problem.

Codeigniter Load View in Helper


Codeigniter Load View in Helper – It is very simple to load views in helper you can use ci instance to load the view file in helper function. Here in this tutorial we are going to explain how you can load the view file in helper function.


Codeigniter Load View in Helper

We have crated a custom function in helper file and we want to load views using the codeigniter ci instance.

Codeigniter Load View in Helper Example:

function renderView($viewName)
{
		$CI = &get_instance();
		$CI->load->view($viewName);		
	
}

The above function first gets the instance in $CI variable and then it uses the load view method to load the view file. The above example will work same as the loading views in codeigniter controller function.

Subquery In Codeigniter Active Records


Subquery In Codeigniter Active Records : We sometimes need to write subquery in codeigniter. There are many to write subqueries in codeigniter. The most common question we have here is how to write subqueries in Codeigniter Active Records not the core Queries. Here in this example we are going to explain how you can write subqueries in codeigniter active Recoreds.


Subquery In Codeigniter Active Records

->where() Accepts strings and passes it to the query directly. So we are going to pass subquery in this as below –

Subquery In Codeigniter Active Records

Subquery In Codeigniter Active Records Example:

$this->db->select('*')->from('Users');
$this->db->where('id NOT IN (SELECT id FROM profiles where status = "1")', NULL, FALSE);

The above example will produce the subquery and result as per subquery written.

Codeigniter download file example


Codeigniter download file example : To download any file in codiegniter we need download helper so first of all load download helper then use it’s download method. Here in this tutorial, we are going to explain how you can download file in CI using download helper with simple example.


Syntax : Codeigniter download file example

Codeigniter download file example

Load Helper

First of all you need to load helper simply as below-

$this->load->helper('download');

After loading helper you can access it’s function to download file.


force_download('filename', 'data');

filename : The name of the downloadble file.

data : Content of the file.

Example:

Here is an example to download file from the server.

$data = file_get_contents("/path/my-image.jpg");
force_download('my-today-day.jpg', 'data');

File will be downloaded in with name my-today-day.jpg.

Codeigniter get post data


Codeigniter get post data : $this->input->post(‘field_name’); is used for getting data from post method.


Syntax : Codeigniter get post data


$this->input->post('field_name');

Suppose we have a simple form :

'post')); ?>







Suppose our controller is :

input->post('name');

          
       }
}

which will print the name posted from the form using post method

Codeigniter print session data variable array


Codeigniter print session data variable array : Codeigniter Session class is used to deal with session. We can use $this->session->userdata to get all session data in codeigniter. Here in this tutorial, we are going to explain how you can get session data in codiegniter and print the array.


Example : Codeigniter print session data variable array

There are following ways to access the array data in codeigniter.

print_r($this->session->userdata);

Or

$sessionData = $this->session->all_userdata();
print_r($sessionData);

Both will return associative array of the session.