Professional CodeIgniter

(singke) #1

Chapter 8: Last-Minute Upgrades


252


function removeSubcriber($id){
$this- > db- > where(‘id’, $id);
$this- > db- > delete(‘subscribers’);
}

function getAllSubscribers(){
$data = array();
$Q = $this- > db- > get(‘subscribers’);
if ($Q- > num_rows() > 0){
foreach ($Q- > result_array() as $row){
$data[] = $row;
}
}
$Q- > free_result();
return $data;
}

}//end class
? >

Don ’ t forget to autoload it:

$autoload[‘model’] = array(‘MCats’, ‘MProducts’, ‘MOrders’,’MAdmins’,’MSizes’,
‘MColors’, ‘MPages’, ‘MSubscribers’);

Adding a subscribe() Function


As you might imagine, you ’ re going to create a form that will post data to welcome/subscribe. Before
creating that form, it may be helpful to build out that controller function first.

Basically, the idea behind this function is to take the incoming POST data and post that data to the
Subscribers table. First you want to make sure that the e - mail in question is valid (with a little help from
the e - mail helper provided by CodeIgniter). If the e - mail is invalid or if the form doesn ’ t have a name
filled in, then redirect the user to the home page with an error message. Otherwise, run
createSubscriber() , and display a confirmation message.

Here is the subscribe() function in its entirety:

function subscribe(){
if ($this- > input- > post(‘email’)){
$this- > load- > helper(‘email’);
if (!valid_email($this- > input- > post(‘email’))){
$this- > session- > set_flashdata(‘subscribe_msg’, ‘Invalid email. Please try
again!’);
redirect(‘welcome/index’,’refresh’);
}
Free download pdf