Become Our Fan on Social Sites!

Facebook Twitter

Google+ RSS YouTube
Showing posts with label CodeIgniter. Show all posts
Showing posts with label CodeIgniter. Show all posts

Saturday 15 March 2014

CodeIgniter Table Class Example

We can create table using HTML table tag. But in CodeIgniter, there is a Table Class to create table. It is not mandatory to use this Table Class, we can use simple HTML table tag also.

CodeIgniter Table Class Example
CodeIgniter Table Class Example

This Table Class provides facility of creating table from arrays or database query result set.

1. How To Initialize Table Class?


To initialize Table Class, we have to write following statement in the controller class:

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

Now, we can access Table Class library object using $this->table.

2. Create Table From Array

We can create table using multi-dimensional array. First array index becomes table heading. We can also make table heading using set_heading() method.

Example:

$this->load->library('table');
$rows = array(
    array('RollNo','Name','City'),
    array('1','Jignesh','Rajkot'),
    array('2','John','Washington'),
    array('3','Mark','Canada')
);
echo $this->table->generate($rows);


OUTPUT:

RollNoNameCity
1JigneshRajkot
2JohnWashington
3MarkCanada

Above example generates table with 4 rows and 3 columns.

3. Create Table From Database Query Result

We can direct generate table from database query result.

Below is an example of table generation from database query result.

$this->load->library('table');
$query = $this->db->query('SELECT * FROM my_table');
echo $this->table->generate($query);

Above code creates table from database table 'my_table'.

4. Example of add_row() and set_heading() Functions

We can add row to the table using add_row() function and we can set heading using set_heading() function. See below code for the same:

$this->load->library('table');
$this->table->set_heading('RollNo','Name','City');

$this->table->add_row('1','Jignesh','Rajkot');
$this->table->add_row('2','John','Washington');
$this->table->add_row('3','Mark','Canada');

echo $this->table->generate();

We can also use array like below code:

$this->table->add_row(array('1','Jignesh','Rajkot'));


5. Example of make_columns() Function

This function creates multi-dimensional array with depth equal to the number of columns desired(second argument). This function takes two argument one is one-dimensional array and second is number of columns required in the table.

Example
$list = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve');
$newlist = $this->table->make_columns($list, 3);
$this->table->generate($newlist);

// Above code generates following code.

<table border="0" cellpadding="4" cellspacing="0">
<tr>
<td>one</td><td>two</td><td>three</td>
</tr>
<tr>
<td>four</td><td>five</td><td>six</td>
</tr>
<tr>
<td>seven</td><td>eight</td><td>nine</td>
</tr>
<tr>
<td>ten</td><td>eleven</td><td>twelve</td>
</tr>
</table>


OUTPUT:

onetwothree
fourfivesix
seveneightnine
teneleventwelve


6. Function Reference of Table Class

$this->table->generate()

This function is already used in above example. generate() function generates/creates table. To print, use echo statement.

$this->table->set_caption()

This function is used to set caption of the table.

$this->table->set_caption('Student Details');

$this->table->set_heading()

This function is also already used in above example. This function sets heading, like <th> tag in HTML.

$this->table->add_row()

This function creates row in the table. See above example, we already used add_row() function.

If we want to set attribute to particular cell, then we have to use associative array like below:

$cell = array('data'=>'Red','class'=>'mystyle','colspan'=>2);
$this->table->add_row($cell,'Green','Blue');

// Above code generates following HTML code

<td class="mystyle" colspan="2">Red</td>
<td> Green </td>
<td> Blue </td>

$this->table->make_columns()

Example of this function we already discussed. This function creates multi-dimensional array with desired number of columns.

$this->table->set_template()

We can create own template. See below example:

$tmpl = array('table_open'=>'<table border="1" cellpadding="2" cellspacing="1" class="mytable">');
$this->table->set_template($tmpl);

$this->table->set_empty()

Using this function, we can set default value for any table cells that are empty.

$this->table->set_empty("&nbsp;");

$this->table->clear()

This function clears table heading and row data.

If you want to create table using HTML <table> tag then click on following link:

Read How To Create Table In HTML

Conclusion:

CodeIgniter Table creation is very simple. But if you are not comfortable with this CodeIgniter Table Class then use simple table tag of HTML.

Thursday 13 March 2014

CodeIgniter Reserved Names

This post is about Reserved Names in CodeIgniter. Reserved names are names that are used in internal operations and developer can not use that names.

Reserved Words In CodeIgniter
Reserved Names CodeIgniter

In CodeIgniter, some controller names, function names, variable names, and constant names are reserved, so developer/programmer can not use that names.

1. Reserved Constants In CI


Constants are variable names with fixed/constant value. It should be in capital letter. Constants are fixed, we can not change value of constants.


Following are list of reserved constants in CodeIgniter:
  • ENVIRONMENT
  • EXT
  • FCPATH
  • SELF
  • BASEPATH
  • APPPATH
  • CI_VERSION
  • FILE_READ_MODE
  • FILE_WRITE_MODE
  • DIR_READ_MODE
  • DIR_WRITE_MODE
  • FOPEN_READ
  • FOPEN_READ_WRITE
  • FOPEN_WRITE_CREATE_DESTRUCTIVE
  • FOPEN_READ_WRITE_CREATE_DESTRUCTIVE
  • FOPEN_WRITE_CREATE
  • FOPEN_READ_WRITE_CREATE
  • FOPEN_WRITE_CREATE_STRICT
  • FOPEN_READ_WRITE_CREATE_STRICT

2. Reserved Variables In CI

Variables are names given to the values. We can change value of variables.

There are following reserved variables in CodeIgniter:
  • $config
  • $mimes
  • $lang

3. Reserved Functions In CI

Function is a few line of codes and reduces redundancy in application.

In CodeIgniter there are following reserved functions that developer / programmer can't create such functions:
  • is_really_writable()
  • load_class()
  • get_config()
  • config_item()
  • show_error()
  • show_404()
  • log_message()
  • _exception_handler()
  • get_instance()

4. Reserved Controller In CI

In CodeIgniter, controller is a special class and works as intermediary between views and model.


Following are the reserved controller names in CodeIgniter, programmer / developer can't use it:
  • Controller
  • CI_Base
  • _ci_initialize
  • Default
  • index

Conclusion:

Above listed names are reserved names in CodeIgniter, so developer / programmer should have to take care about naming controller name, function name, variable name, or constant name.

Tuesday 11 March 2014

How To Create Session In CodeIgniter

1. What is Session?

Session maintains a user's state and tracks user's activity while user browse the website. We have to maintain session manually because HTTP is a stateless protocol.

How To Create Session
Session Creation In CodeIgniter


2. Session Initialization

To avail session in all pages of website then first we have to initialize Session class.


We can initialize Session class as below:

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

After above statement, now we can access session library using '$this->session'.

Created session saved in the cookie.

3. What Session Data Contain?

In CodeIgniter, session is just an array and contains following information:
  • User's unique session id (Hashed with MD5)
  • User's IP address
  • User's User Agent data
  • 'last activity' time stamp
For example
[array]
(
    'session_id' => random hash,
    'ip_address' => 'user IP address',
    'user_agent' => 'user agent data',
    'last_activity' => timestamp
)

Above data is available by default.

4. How To Add Custom Data To Session?

We can store our own data in the user's cookie. There are two ways to add data to the session, listed below:
  • Using array
  • One value at a time
Let's see one example of using array:

$userdata=array(
    'username' => 'xyzname',
    'emailid' => 'xyz@abc.com'
);
$this->session->set_userdata($userdata);

Let's see one example of one value at a time:

$this->session->set_userdata('username','xyzname');
$this->session->set_userdata('emailid','xyz@abc.com');

Cookie can only hold 4KB of data, so careful about data size.

5. How To Retrieve/Get Session Data?

Session data can be retrieved using all_userdata() or userdata() functions.

$this->session->all_userdata();

$this->session->userdata('session-key-name');

all_userdata() function returns an associative array as follows:

Array
(
   [session_id] => d1f7ddfa2c71b9aba777763c74047c3c
   [ip_address] => '127.0.0.1'
   [user_agent] => Mozilla/5.0 (Windows NT 6.1; rv:27.0) Gecko/20100101 Firefox/27.0
   [last_activity] => 1394467693
)

To extract individual session then use userdata() function as described below:

$this->session->userdata('user_name')

In above example, session key is 'user_name'.

6. How To Remove/Unset Session Data?

To remove/unset session data unset_userdata() function is used. Provide session key to unset_userdata() function.

If we want to remove/unset particular session then provide that session key.

For example:
$this->session->unset_userdata('username');
$this->session->unset_userdata('emailid');

If we want to remove/unset session using associative array then consider following example:

$array_data = array('username'=>'','emailid'=>'');
$this->session->unset_userdata($array_data);

7. How To Destroy Session?

If we want to destroy current session then use following function:

$this->session->sess_destroy();

This function will destroy current session.

8. About Flashdata

Flashdata is useful when we want to display informational or status messages like 'your data is saved', 'data is retrieving', '5 record deleted', etc.

Flashdata is only available to the next server request then after automatically cleared.

How To Add Flash Data

$this->session->set_flashdata('item','value');

We can also pass an array to the function same as set_userdata().

Retrieve/Read Flash Data

To retrieve flashdata we need flashdata() function as described below:

$this->session->flashdata('item');

We can preserve flashdata through an additional request using keep_flashdata() function.

$this->session->keep_flashdata('item');

In CodeIgniter, session management is very easy. If you find any difficulty then comment below.

Thursday 20 February 2014

How To Configure Database In CodeIgniter

Hello friends, I am telling you about how to configure/set database in CodeIgniter. CodeIgniter framework contains many configuration files.

HOW TO SET DATABASE IN CODEIGNITER
Database Configuration in CodeIgniter

Sunday 2 February 2014

Hello World Program in CodeIgniter

Hello friends, here is another post about CodeIgniter first program. Before we learn how to create Hello World program in CodeIgniter, first we have to understand what is Controller?

1. What is Controller?

Controller is a class that works as intermediary between Model and View. In CodeIgniter, Controller runs first then Controller calls/loads Models or Views as required.
CodeIgniter Hello World Program - Basic Controller Program
Hello World Program In CodeIgniter
 

Sunday 19 January 2014

CodeIgniter Basic Tutorial

1. What is CodeIgniter?

CodeIgniter is a powerful, proven, agile & open web application framework for to build dynamic websites using PHP. CodeIgniter is developed by EllisLab, Inc. And its initial release is February 28, 2006. We can find everything about CodeIgniter from official website of CodeIgniter: http://www.ellislab.com/codeigniter.
CodeIgniter Basic Tutorial
CodeIgniter Basic Tutorial