Tuesday 25 March 2014
Monday 24 March 2014
Saturday 22 March 2014
How To Create Message Box In .Net
Message Box is used to inform something to the user. Message box interrupts user and user must have to answer message box to process further. Most of time this type of message box you find in error message, warning message, etc.
In this tutorial, I am explaining about C#.Net message box.
To create message box in C#.Net, you have to include 'System.Windows.Forms' namespace.
Example:
The name of class used to create message box is 'MessageBox' and contains static method named 'Show()'.
Static method does not need to call using object, it is called directly using class name.
Message box sometimes called dialog box or popup box.
Following are the examples of message box and it is written in Form1_Load event of window form.
This message box contains just message and no title text displayed.
Simple message box looks like:
This message box contains text message and title text.
This message box looks like:
This message box contains text message, title text, Yes and No buttons.
This message box looks like:
MessageBoxButtons enumeration contains following options:
Using MessageBoxIcon enumeration, you can change icon of message box.
This message box looks like:
MessageBoxIcon enumeration contains following options:
Using MessageBoxDefaultButton enumeration, you can set default button. If you set default Button2 then button - 2 is selected.
This message box looks like:
MessageBoxDefaultButton enumeration cotains following options:
Using MessageBoxOptions enumeration, you can set following options:
In this tutorial, I am explaining about C#.Net message box.
To create message box in C#.Net, you have to include 'System.Windows.Forms' namespace.
C#.Net Message Box |
Example:
using System.Windows.Forms;
The name of class used to create message box is 'MessageBox' and contains static method named 'Show()'.
Static method does not need to call using object, it is called directly using class name.
Message box sometimes called dialog box or popup box.
Following are the examples of message box and it is written in Form1_Load event of window form.
1. Simple Message Box
MessageBox.Show("Simple Message Box");
This message box contains just message and no title text displayed.
Simple message box looks like:
2. Message Box With Message and Title Text
MessageBox.Show("All Code Tips ~ Programming Blog", "Title: All Code Tips");
This message box contains text message and title text.
This message box looks like:
3. Message Box With Different Buttons
DialogResult res = MessageBox.Show("Click on button", "All Code Tips", MessageBoxButtons.YesNo);
This message box contains text message, title text, Yes and No buttons.
This message box looks like:
MessageBoxButtons enumeration contains following options:
- MessageBoxButtons.AbortRetryIgnore
- MessageBoxButtons.OK
- MessageBoxButtons.OKCancel
- MessageBoxButtons.RetryCancel
- MessageBoxButtons.YesNo
- MessageBoxButtons.YesNoCancel
DialogResult res = MessageBox.Show("Click on button", "All Code Tips", MessageBoxButtons.YesNo);
if(res == DialogResult.Yes)
{
MessageBox.Show("You click on Yes button");
}
else
{
MessageBox.Show("You click on No button");
}
if(res == DialogResult.Yes)
{
MessageBox.Show("You click on Yes button");
}
else
{
MessageBox.Show("You click on No button");
}
4. Message Box With Icons
DialogResult res = MessageBox.Show("Click on button", "All Code Tips", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
Using MessageBoxIcon enumeration, you can change icon of message box.
This message box looks like:
MessageBoxIcon enumeration contains following options:
- MessageBoxIcon.Asterisk
- MessageBoxIcon.Error
- MessageBoxIcon.Exclamation
- MessageBoxIcon.Hand
- MessageBoxIcon.Information
- MessageBoxIcon.None
- MessageBoxIcon.Question
- MessageBoxIcon.Stop
- MessageBoxIcon.Warning
5. Default Button Settings
DialogResult res = MessageBox.Show("Click on button", "All Code Tips", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2);
Using MessageBoxDefaultButton enumeration, you can set default button. If you set default Button2 then button - 2 is selected.
This message box looks like:
MessageBoxDefaultButton enumeration cotains following options:
- MessageBoxDefaultButton.Button1
- MessageBoxDefaultButton.Button2
- MessageBoxDefaultButton.Button3
6. Setting Message Box Options
DialogResult res = MessageBox.Show("Click on button", "All Code Tips", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2, MessageBoxOptions.RightAlign);
Using MessageBoxOptions enumeration, you can set following options:
- MessageBoxOptions.DefaultDesktopOnly
- MessageBoxOptions.RightAlign
- MessageBoxOptions.RtlReading
- MessageBoxOptions.ServiceNotification
If you find any problem then comment below.
Wednesday 19 March 2014
JavaScript Dialog Boxes
Here is a tutorial post of dialog boxes / popup boxes in JavaScript. In JavaScript, there are three dialog / popup boxes. These three different types of boxes listed below:
- Alert Dialog Box
- Confirm Dialog Box
- Prompt Dialog Box
JavaScript Dialog Boxes |
Now we will see these three dialog boxes one by one:
Alert dialog box is used to give any message to the user.
Mostly alert dialog box is used in form validation error message display, for example in form, you have email id field and you have not entered email id then as an error message, you can alert user using Alert dialog box.
Example
Alert dialog box also can be written using window prefix like below:
But you can also write without window prefix.
Alert dialog box contains only one 'Ok' button. User must have to click on 'Ok' button to precess further.
The Alert dialog box looks like as following image:
Confirm dialog box is a dialog box that contains 'Ok' and 'Cancel' button.
Confirm dialog box often used for to accept something from user.
If user clicks on 'Ok' button then confirm dialog box returns true. If user clicks on 'Cancel' button then confirm dialog box returns false.
Example:
The confirm dialog box looks like as following image:
Prompt dialog box is used to get user input.
Prompt dialog box can be displayed using prompt() method of window object.
Prompt method takes two parameters: 1) Label of prompt box and 2) Default value for user input field.
Prompt dialog box contains two buttons named 'Ok' and 'Cancel' and one text input field.
If user clicks on 'Ok' button then inputed text is returned and if user clicks on 'Cancel' button then null is returned.
The prompt dialog box looks like as following image:
1. Alert Dialog Box
Alert dialog box is used to give any message to the user.
Mostly alert dialog box is used in form validation error message display, for example in form, you have email id field and you have not entered email id then as an error message, you can alert user using Alert dialog box.
Example
<head>
<script type="text/javascript">
alert('This is Alert Box');
</script>
</head>
<script type="text/javascript">
alert('This is Alert Box');
</script>
</head>
Alert dialog box also can be written using window prefix like below:
window.alert('This is Alert Box');
But you can also write without window prefix.
Alert dialog box contains only one 'Ok' button. User must have to click on 'Ok' button to precess further.
The Alert dialog box looks like as following image:
2. Confirm Dialog Box
Confirm dialog box is a dialog box that contains 'Ok' and 'Cancel' button.
Confirm dialog box often used for to accept something from user.
If user clicks on 'Ok' button then confirm dialog box returns true. If user clicks on 'Cancel' button then confirm dialog box returns false.
Example:
<head>
<script type="text/javascript">
var result = confirm('Click on button');
if(result == true)
{
alert('Ok button clicked');
}
else
{
alert('Cancel button clicked');
}
</script>
</head>
<script type="text/javascript">
var result = confirm('Click on button');
if(result == true)
{
alert('Ok button clicked');
}
else
{
alert('Cancel button clicked');
}
</script>
</head>
The confirm dialog box looks like as following image:
3. Prompt Dialog Box
Prompt dialog box is used to get user input.
Prompt dialog box can be displayed using prompt() method of window object.
Prompt method takes two parameters: 1) Label of prompt box and 2) Default value for user input field.
Prompt dialog box contains two buttons named 'Ok' and 'Cancel' and one text input field.
If user clicks on 'Ok' button then inputed text is returned and if user clicks on 'Cancel' button then null is returned.
<head>
<script type="text/javascript">
var name = prompt('Enter Name : ', 'John Smith');
alert('Inputed Value : ' + name);
</script>
</head>
<script type="text/javascript">
var name = prompt('Enter Name : ', 'John Smith');
alert('Inputed Value : ' + name);
</script>
</head>
The prompt dialog box looks like as following image:
Tuesday 18 March 2014
Types of CSS
Hello friends, here is a tutorial of types of CSS. CSS stands for Cascading Style Sheet. CSS is used in web development for to style webpage. Using CSS, you can change font-size, font-family, text color, background color, etc.
There are 3 (three) types of Cascading Style Sheet. There are few advantages and disadvantages of these different types of style sheets.
Following are the list of different types of style sheets:
Inline style sheet is embedded to individual elements of HTML.
To give inline style sheet to particular HTML element then use 'style' attribute of any HTML element.
If you give same style to another HTML element, then style code is repeated. It is better to not write too much inline style sheet.
Inline style sheet is useful when you want to override a style sheet.
Example:
OUTPUT:
Internal style sheet is also known as embedded style sheet.
This internal style sheet is written inside <head> tag of HTML.
To write internal style sheet inside <head> tag, you have to use <style> tag of HTML.
Syntax:
Scope of this style sheet is within the page. If you want to use this style sheet in another page then you have to write again this internal style sheet in that page.
Example:
OUTPUT:
This type of style sheet is written in separate '.css' file and linked to the webpage.
This type of style sheet reduces redundant code of style sheet.
Because of separate external '.css' file, you can link it into any number of pages, in short reuse the style.
I highly recommended, to use external style sheet.
There is nothing new to external style sheet creation, just create separate '.css' file like below:
Example:
mystyle.css
After creating 'mystyle.css' file then create HTML file named 'mypage.html' and embed this external style sheet file using HTML <link> tag as illustrated below:
mypage.html
OUTPUT:
NOTE: These two files 'mystyle.css' and 'mypage.html' must be in same directory/folder.
Types of Cascading Style Sheet |
There are 3 (three) types of Cascading Style Sheet. There are few advantages and disadvantages of these different types of style sheets.
Following are the list of different types of style sheets:
- Inline Style Sheet
- Internal Style Sheet
- External Style Sheet
1. Inline Style Sheet
Inline style sheet is embedded to individual elements of HTML.
To give inline style sheet to particular HTML element then use 'style' attribute of any HTML element.
If you give same style to another HTML element, then style code is repeated. It is better to not write too much inline style sheet.
Inline style sheet is useful when you want to override a style sheet.
Example:
<div style="background-color:black;color:white;">
This is div tag
</div>
This is div tag
</div>
OUTPUT:
This is div tag
2. Internal Style Sheet
Internal style sheet is also known as embedded style sheet.
This internal style sheet is written inside <head> tag of HTML.
To write internal style sheet inside <head> tag, you have to use <style> tag of HTML.
Syntax:
<style type="text/css">
...
</style>
...
</style>
Scope of this style sheet is within the page. If you want to use this style sheet in another page then you have to write again this internal style sheet in that page.
Example:
<html>
<head>
<style type="text/css">
div
{
background-color:yellow;
color:red;
}
</style>
</head>
<body>
<div>
This is div tag
</div>
</body>
</html>
<head>
<style type="text/css">
div
{
background-color:yellow;
color:red;
}
</style>
</head>
<body>
<div>
This is div tag
</div>
</body>
</html>
OUTPUT:
This is div tag
3. External Style Sheet
This type of style sheet is written in separate '.css' file and linked to the webpage.
This type of style sheet reduces redundant code of style sheet.
Because of separate external '.css' file, you can link it into any number of pages, in short reuse the style.
I highly recommended, to use external style sheet.
There is nothing new to external style sheet creation, just create separate '.css' file like below:
Example:
mystyle.css
div
{
background-color:cyan;
color:blue;
}
{
background-color:cyan;
color:blue;
}
After creating 'mystyle.css' file then create HTML file named 'mypage.html' and embed this external style sheet file using HTML <link> tag as illustrated below:
mypage.html
<html>
<head>
<link href="mystyle.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div>This is div tag</div>
</body>
</html>
<head>
<link href="mystyle.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div>This is div tag</div>
</body>
</html>
OUTPUT:
This is div tag
NOTE: These two files 'mystyle.css' and 'mypage.html' must be in same directory/folder.
Sunday 16 March 2014
HTML Image Tag Example
Hello friends, this tutorial is about HTML <img> (image) tag. To display/show image in the webpage then we have to use <img> tag of HTML. Here you can find all required attributes of <img> tag and some tips for image optimization.
Syntax:
"src" and "alt" attributes are important in image tag.
There are following important attributes list of image tag:
HTML Image Tag Example |
Syntax:
<img src="" alt="" />
"src" and "alt" attributes are important in image tag.
There are following important attributes list of image tag:
- src
- alt
- height
- width
To Read Table Tag. Click Here
src attribute:
Src stands for Source and contains value of image path.
For example, if your webpage is saved in some "xyz" directory/folder and image is also in same folder then you have to write following code to display image. Here "allcodetips.jpg" is a image file name, your image file name can be different.
Example:
<img src="allcodetips.jpg" />
If your webpage is saved in "xyz/" directory and image is stored in "xyz/img/" directory then you have to use following code:
Example:
<img src="img/allcodetips.jpg" />
alt attribute:
Alt attribute is second most useful attribute. Alt attribute contains text message and this text message is displayed when image is not display. In short when image is not possible to display at that time alt attribute's value is displayed.
Second use of alt attribute is SEO (Search Engine Optimization) because Search Engine crawler does not crawl images, so crawler crawls src and alt attributes' value.
So always use alt attribute for better Search Engine Optimization.
Example:
<img src="allcodetips.jpg" alt="All Code Tips" />
height attribute:
Height attribute specifies height of image. You can provide height value like 100, 250, 500, etc as your requirement.
Example:
<img src="allcodetips.jpg" alt="All Code Tips" height="250px" />
width attribute:
Width attribute specifies width of image. You can provide percent value to the width attribute like width="100%". You can also provide pixel value to the width attribute.
Example:
<img src="allcodetips.jpg" alt="All Code Tips" width="250px" />
Conclusion:
HTML image tag contains many other attributes, but above listed attributes are most common. You can use events in image tag using onmouseover, onclick, onmouseout, etc.
If you find any difficulty then comment below.
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.
This Table Class provides facility of creating table from arrays or database query result set.
To initialize Table Class, we have to write following statement in the controller class:
Now, we can access Table Class library object using $this->table.
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:
OUTPUT:
Above example generates table with 4 rows and 3 columns.
We can direct generate table from database query result.
Below is an example of table generation from database query result.
Above code creates table from database table 'my_table'.
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:
We can also use array like below code:
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
OUTPUT:
$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_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:
$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:
$this->table->set_empty()
Using this function, we can set default value for any table cells that are empty.
$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.
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);
$rows = array(
array('RollNo','Name','City'),
array('1','Jignesh','Rajkot'),
array('2','John','Washington'),
array('3','Mark','Canada')
);
echo $this->table->generate($rows);
OUTPUT:
RollNo | Name | City |
---|---|---|
1 | Jignesh | Rajkot |
2 | John | Washington |
3 | Mark | Canada |
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);
$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();
$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>
$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:
one | two | three |
four | five | six |
seven | eight | nine |
ten | eleven | twelve |
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->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_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(" ");
$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.
In CodeIgniter, some controller names, function names, variable names, and constant names are reserved, so developer/programmer can not use that names.
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:
Variables are names given to the values. We can change value of variables.
There are following reserved variables in CodeIgniter:
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:
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:
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.
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.
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
[array]
(
'session_id' => random hash,
'ip_address' => 'user IP address',
'user_agent' => 'user agent data',
'last_activity' => timestamp
)
(
'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
$userdata=array(
'username' => 'xyzname',
'emailid' => 'xyz@abc.com'
);
$this->session->set_userdata($userdata);
'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');
$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
)
(
[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');
$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);
$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.
Saturday 8 March 2014
Subscribe to:
Posts
(
Atom
)