Welcome Guest, Not a member yet? Register   Sign In
New to CI
#4

[eluser]drewbee[/eluser]
I'm mad now. Why did no one tell me about CI Before? Sad lol.

controller:
Code:
<?php

class Registration extends Controller
{
    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
        $this->load->helper(array('form', 'url'));
        
        $this->load->library(array('template','validation'));
        
        $this->template->title = 'Seller Registration';
        $this->template->meta_robots = 'noindex,nofollow';
        $this->template->addCrumb('/registration/index.html', 'Registration');
        $this->template->addTemplate('registration');
        
        $rules['email'] = 'trim|required|max_length[30]|valid_email|callback_emailUnique';
        $rules['first_name'] = 'trim|required|min_length[1]|max_length[50]';
        $rules['middle_initial'] = 'trim|exact_length[1]|alpha';
        $rules['last_name'] = 'trim|required|min_length[1]|max_length[50]';
        $rules['username'] = 'trim|required|min_length[5]|max_length[30]|callback_usernameUnique';
        $rules['password_1'] = 'required|matches[password_2]|min_length[5]|max_length[30]';
        $rules['password_2'] = '';

        $this->validation->set_rules($rules);
        
        $fields['email'] = '';
        $fields['first_name'] = '';
        $fields['middle_initial'] = '';
        $fields['last_name'] = '';
        $fields['username'] = '';
        $fields['password_1'] = '';
        $fields['password_2'] = '';
        
        $this->validation->set_fields($fields);
        
        if ($this->validation->run() == FALSE)
        {
            $this->template->draw();
        }
        else
        {
            $links = array(anchor('validate_email', 'Validate Email'), anchor('home', 'Home'));
            $this->template->draw('You have succesfully registered an account', $links);
        }
    }
    
    function usernameUnique($username)
    {
        if ($this->validation->if_exists('accounts', 'username', $username))
        {
            $this->validation->set_message('usernameUnique', 'Username is taken. Please try something new.');
            return false;
        }
        else
        {
            return true;
        }
    }
    
    function emailUnique($email)
    {
        if ($this->validation->if_exists('accounts', 'email', $email))
        {
            $this->validation->set_message('emailUnique', 'Email Address Already Exists.');
            return false;
        }
        else
        {
            return true;
        }    
    }
}
?>

Validation:
Code:
<?php
class CIEXT_Validation extends CI_Validation
{

    function __construct()
    {
        parent::__construct();
        $this->set_error_delimiters('<span class="error">', '</span>');
    }
    
    function if_exists($table, $column, $value)
    {
        $query = $this->CI->db->query("SELECT        1
                                       FROM          $table
                                       WHERE         $column = ".$this->CI->db->escape($value)."");
                                  
        if ($query->num_rows() > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
        
    }    
}
?&gt;

Already had to design myself a nice little template library as well. To cool. Big Grin Big Grin Big Grin


Messages In This Thread
New to CI - by El Forum - 07-28-2008, 11:07 AM
New to CI - by El Forum - 07-28-2008, 12:51 PM
New to CI - by El Forum - 07-28-2008, 02:42 PM
New to CI - by El Forum - 07-29-2008, 01:10 PM



Theme © iAndrew 2016 - Forum software by © MyBB