Book Image

CodeIgniter 1.7

Book Image

CodeIgniter 1.7

Overview of this book

CodeIgniter (CI) is a powerful open-source PHP framework with a very small footprint, built for PHP coders who need a simple and elegant toolkit to create full-featured web applications. CodeIgniter is an MVC framework, similar in some ways to the Rails framework for Ruby, and is designed to enable, not overwhelm. This book explains how to work with CodeIgniter in a clear logical way. It is not a detailed guide to the syntax of CodeIgniter, but makes an ideal complement to the existing online CodeIgniter user guide, helping you grasp the bigger picture and bringing together many ideas to get your application development started as smoothly as possible. This book will start you from the basics, installing CodeIgniter, understanding its structure and the MVC pattern. You will also learn how to use some of the most important CodeIgniter libraries and helpers, upload it to a shared server, and take care of the most common problems. If you are new to CodeIgniter, this book will guide you from bottom to top. If you are an experienced developer or already know about CodeIgniter, here you will find ideas and code examples to compare to your own.
Table of Contents (21 chapters)
CodeIgniter 1.7
Credits
About the Authors
About the Reviewer
Preface

What we need to use the library


In order to use this library, we will need CI version 1.7.2, so if you have been following this book with a previous version, download version 1.7.2 and unzip it. We have unzipped it in a folder called codeigniterc. If you do the same, you will be able to check your CI installation in http://localhost/codeigniterc/. Once this is done, we will create a new database; call it cart or whatever you want. For our example we will call it cart.

Note

Don't forget to change the base_url value in your application/config/config.php. For example:

$config['base_url'] = "http://localhost/codeigniterc/";

We need to configure our CI installation to be able to use this database. Remember that the database configuration file was placed in application/config/database.php:

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "root";
$db['default']['password'] = "root";
$db['default']['database'] = "cart";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";

Again, the most important values are:

  • hostname is the database server where our database is located, in this example, localhost

  • username is that of a user who has permissions to use the database

  • password is the password for that user

  • database is the database we will use

The easiest way to see if this configuration is working, is to autoload the database library. Remember this was done in the application/config/autoload.php file; we can see the libraries, just add database in the array name as shown:

$autoload['libraries'] = array('database');

After this is done, when we refresh http://localhost/codeigniterc/, we can check if the configuration is working. If something goes wrong we will see a message like:

A Database Error Occurred

We are almost done with the preparations. Now we need to create a new table in our database using the following code:

CREATE TABLE IF NOT EXISTS `ci_sessions` (
session_id varchar(40) DEFAULT '0' NOT NULL,
ip_address varchar(16) DEFAULT '0' NOT NULL,
user_agent varchar(50) NOT NULL,
last_activity int(10) unsigned DEFAULT 0 NOT NULL,
user_data text NOT NULL,
PRIMARY KEY (session_id)
);

The cart library also uses the Session class. To save cart data into the database, we need to create the mentioned table. Once this is done, we can start using the cart library.

Note

For using the database in the Session class, we need to change another parameter in application/config/config.php file. This time it will be sess_use_database; we need to set it to TRUE:

$config['sess_use_database'] = TRUE;

Note: This class seems to work without the database, but as the user guide tells us to do so, we are keeping to that in this example.