Professional CodeIgniter

(singke) #1

Chapter 8: Last-Minute Upgrades


251


Here are the pieces you ’ ll need to make this work:

Some kind of simple form that site visitors can fill out

A controller function that accepts the form entry

A database table in which to store the information from the form

A model that facilitates the use of the form

An administrative set of screens that let you manage subscribers and e - mails

As usual, you ’ re going to work from the back forward; that is, you ’ re going to establish the database
table and the model first and then work out the controller functions and, eventually, the look and feel of
the form itself and where to place it.

Creating the Database Table and Model


The database table for this part of the project should be kept to a minimal number of fields to keep the
task of updating it as simple as possible. Although some might argue that you need many different kinds
of fields to track subscribers, all you really need is name and email. Your database table will then have
three fields: a primary field ( id ), a name field, and an e - mail field. Nothing more is required to get started.

CREATE TABLE ‘subscribers’ (
‘id’ int(11) NOT NULL auto_increment,
‘name’ varchar(255) NOT NULL,
‘email’ varchar(255) NOT NULL,
PRIMARY KEY (‘id’)
) ENGINE=MyISAM;

Creating a model based on this simple table should also be old hat to you by now. For right now, you just
need three functions in your model: to subscribe, unsubscribe, and show all subscribers. You ’ re going to
call this model MSubscribers.

< ?php

class MSubscribers extends Model{

function MSubscribers(){
parent::Model();
}

function createSubscriber(){
$data = array(
‘name’ = > $_POST[‘name’],
‘email’ = > $_POST[‘email’]
);

$this- > db- > insert(‘subscribers’, $data);
}





Free download pdf