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 databasepassword
is the password for that userdatabase
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.