Book Image

CodeIgniter Web Application Blueprints

Book Image

CodeIgniter Web Application Blueprints

Overview of this book

Table of Contents (16 chapters)
CodeIgniter Web Application Blueprints
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating the model


The Urls_model contains three functions; obviously it contains our __construct() function but we're not focusing on that at the moment as it's not doing anything except referencing its parent.

Instead, let's look at the two functions save_url() and fetch_url(). As their names suggest, one saves information to the database and the other fetches information from it. For now, let's go and create the code and we'll discuss in detail what each function does later:
Create the urls_model.php model file and add the following code to it:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Urls_model extends CI_Model {
  function __construct() {
    parent::__construct();
  }

  function save_url($data) {
    /*
    Let's see if the unique code already exists in 
    the database.  If it does exist then make a new 
    one and we'll check if that exists too.  
    Keep making new ones until it's unique.  
    When we make one that's unique, use it...