0% found this document useful (0 votes)
68 views

Module 6 - Session and Pagination Library

This document discusses session management and pagination in CodeIgniter. It describes how to initialize and use the session class to store and retrieve session data, including setting, getting, and removing session variables. It also covers flashdata and tempdata. For pagination, it explains how to generate pagination links based on the total number of rows and items per page, and customize the pagination display using configuration preferences.

Uploaded by

Emmie Gonzales
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

Module 6 - Session and Pagination Library

This document discusses session management and pagination in CodeIgniter. It describes how to initialize and use the session class to store and retrieve session data, including setting, getting, and removing session variables. It also covers flashdata and tempdata. For pagination, it explains how to generate pagination links based on the total number of rows and items per page, and customize the pagination display using configuration preferences.

Uploaded by

Emmie Gonzales
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Web System Technologies

IT0049
Module 6
Session and Pagination
Library
• To be able to manage session for storing small data.
• To use session in forms and as data references.
Session class should either be initialized in your controller constructors, or it
can be auto-loaded by the system.

To initialize the Session class manually in your controller constructor, use


the $this->load->library() method:

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

Once loaded, the Sessions library object will be available using:


$this->session
When a page is loaded, the session class will check to see if
valid session cookie is sent by the user’s browser.

If a sessions cookie does not a new session will be created


and saved.

If a valid session does exist, its information will be updated.


Session data is simply an array associated with a particular
session ID (cookie).
If you’ve used sessions in PHP before, you should be familiar
with PHP’s $_SESSION.
Any piece of information from the session array is available through
the $_SESSION superglobal:
$_SESSION['item’]

Or through the magic getter:


$this->session->item

And for backwards compatibility, through the userdata() method:


$this->session->userdata('item’);
If you want to retrieve all of the existing userdata, you can
simply omit the item key (magic getter only works for
properties):

$_SESSION

// or:

$this->session->userdata();
You can simply assign data to the $_SESSION array, as with any other
variable. Or as a property of $this->session.

For example, assigning a session data with the name “sessdata” should look
something like:

$this->session->sessdata = “value”;
Where $array is an associative array containing your new data.

Here’s an example:
$newdata = array(
'username' => 'johndoe’,
'email' => '[email protected]’,
'logged_in' => TRUE );

$this->session->set_userdata($newdata);
If you want to verify that a session value exists, simply check with isset():

isset($_SESSION['some_name’]);

Or you can call has_userdata():

$this->session->has_userdata('some_name');
Unsetting a value in $_SESSION can be done through unset():

unset($_SESSION['some_name']);

or multiple values:

unset( $_SESSION['some_name'], $_SESSION['another_name'] );

as set_userdata() can be used to add information to a


session, unset_userdata() can be used to remove it:

$this->session->unset_userdata('some_name’); Also accepts an array of


item keys to unset:
CodeIgniter supports “flashdata”, or session data that will only be available
for the next request and is then automatically cleared. Useful for one-time
informational, error or status messages.

To mark an existing item as “flashdata”:

$this->session->mark_as_flash('item’);

If you want to mark multiple items as flashdata, simply pass the keys as an
array:
$this->session->mark_as_flash(array('item', 'item2'));
To add flashdata:
$_SESSION['item'] = 'value’;
$this->session->mark_as_flash('item’);

Or using the set_flashdata() method:


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

You can also pass an array to set_flashdata(), in the same manner


as set_userdata().
Reading flashdata variables is the same as reading regular session data
through $_SESSION:
$_SESSION['item’]

However, if you want to be sure that you’re reading “flashdata”, you can also
use the flashdata() method:

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

Or to get an array with all flashdata, simply omit the key parameter:
$this->session->flashdata();
If you find that you need to preserve a flashdata variable through an additional
request, you can do so using the keep_flashdata() method.

$this->session->keep_flashdata('item’); // Single Item


or
$this->session->keep_flashdata(array('item1', 'item2', 'item3’));//Multiple Items
CodeIgniter also supports “tempdata”, or session data with a specific
expiration time. After the value expires, or the session expires or is deleted,
the value is automatically removed.

To mark an existing item as “tempdata”, simply pass its key and expiry time
(in seconds!) to the mark_as_temp() method:

// 'item' will be erased after 300 seconds


$this->session->mark_as_temp('item', 300);
You can mark multiple items as tempdata in two ways:

// Both 'item' and 'item2' will expire after 300 seconds


$this->session->mark_as_temp(array('item', 'item2'), 300);

// 'item' will be erased after 300 seconds, while 'item2’


// will do so after only 240 seconds
$this->session->mark_as_temp(array(
'item' => 300,
'item2' => 240
));
To add tempdata:
$_SESSION['item'] = 'value’;
$this->session->mark_as_temp('item', 300); // Expire in 5 minutes

Or alternatively, using the set_tempdata() method:


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

Passing an array to set_tempdata():


$tempdata = array('newuser' => TRUE, 'message' => 'Thanks for
joining!’);
$this->session->set_tempdata($tempdata, NULL, $expire);
To read tempdata, you can just access it through
the $_SESSION superglobal array:
$_SESSION['item’]

Or you can also use the tempdata() method:


$this->session->tempdata('item’);

if you want to retrieve all existing tempdata:


$this->session->tempdata();
If you need to remove a tempdata you can directly unset it from
the $_SESSION array:

unset($_SESSION['item']);

Or

$this->session->unset_tempdata('item');
To clear the current session ,you may simply use either
PHP’s session_destroy() function, or the sess_destroy() method.

session_destroy();
// or
$this->session->sess_destroy();
Pagination
Pagination refers to links that allows you to navigate from page to page, like
this:

Pagination provides users with additional navigation for browsing through


parts of the webpage. Parts of the pagination are usually numbers, hints,
arrows as well as “previous” and “next” buttons.
base_url the full URL to the controller class/function containing your
pagination.
total_rows This number represents the total rows in the result set you are
creating pagination for.
per_page The number of items you intend to show per page.
The create_links() method returns an empty string when there is no
pagination to show.
The following is a list of all the preferences you can pass to the initialization
function to tailor the display.

$config[‘uri_segment’] = 3;
Pagination function automatically determines which segment of your URI contains the page
number.
$config[‘num_links’] = 2;
The number of “digit” links you would like before and after the selected page number.
$config[‘use_page_numbers’] = TRUE;
By default, the URI segment will use the starting index for the items you are paginating.
$config[‘page_query_string’] = TRUE;

By default, the pagination library assume you construct your links something like:
http://example.com/index.php/test/page/20

If you have $config['enable_query_strings'] set to TRUE your links will automatically be


re-written using Query Strings :

http://example.com/index.php?c=test&m=page&per_page=20
Adding Enclosing Markup

$config[‘full_tag_open’] = ‘<p>’;
The opening tag placed on the left side of the entire result.

$config[‘full_tag_close’] = ‘</p>’;
The closing tag placed on the right side of the entire result.
Customizing the First Link

$config[‘first_link’] = ‘First’;
The text shown in the “first” link. Setting the value to FALSE will hide this
link.

$config[‘first_tag_open’] = ‘<div>’;
Opening tag for the “first” link.

$config[‘first_tag_close’] = ‘</div>’;
Closing tag for the “first” link.
Customizing the Last Link
$config[‘last_link’] = ‘Last’;
The text shown in the “last” link. Setting the value to FALSE will hide this
link.

$config[‘last_tag_open’] = ‘<div>’;
Opening tag for the “last” link.

$config[‘last_tag_close’] = ‘</div>’;
Closing tag for the “last” link.
Customizing the “Next” Link
$config[‘next_link’] = ‘&gt;’;
The text shown in the “next” page link. Setting the value to FALSE will hide
this link.

$config[‘next_tag_open’] = ‘<div>’;
Opening tag for the “next” link.

$config[‘next_tag_close’] = ‘</div>’;
Closing tag for the “next” link.
Customizing the “Previous” Link

$config[‘prev_link’] = ‘&lt;’;
The text shown in the “previous” page link. Setting the value to FALSE will
hide this link.

$config[‘prev_tag_open’] = ‘<div>’;
Opening tag for the “previous” link.

$config[‘prev_tag_close’] = ‘</div>’;
Closing tag for the “previous” link.
Customizing the “Current Page” Link

$config[‘cur_tag_open’] = ‘<b>’;
Opening tag for the “current” link.
$config[‘cur_tag_close’] = ‘</b>’;
Closing tag for the “current” link.
Customizing the “Digit" Link

$config[‘num_tag_open’] = ‘<div>’;
Opening tag for the “digit” link.
$config[‘num_tag_close’] = ‘</div>’;
Closing tag for the “digit” link.
Hiding the Pages

$config['display_pages'] = FALSE;
If you only want “next” and “previous” links displayed.

You might also like