I am attempting to develop a registration system with CodeIgniter. I've got a controller known as Register using the following code:

class Register extends CI_Controller {
    public function index()
    {
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<span class="error">', '</span>');
        $this->form_validation->set_rules('username', 'username', 'required|min_length[3]|max_length[12]|trim');
        $this->form_validation->set_rules('password', 'password', 'required|min_length[2]|md5');
        $this->form_validation->set_rules('email', 'email', 'required|valid_email|trim');
        $this->form_validation->set_rules('artist', 'artist', 'max_length[32]|trim');
        $this->form_validation->set_rules('captcha', 'CAPTCHA', 'required|trim');
        $this->load->view('header');
        if(!$this->form_validation->run())
        {
            $this->load->view('register_form');
        }
        else
        {
            $this->load->view('register_done');
        }
        $this->load->view('footer');
    }
}

To date so great. Basically visit the register page I recieve the registration form displayed. Basically send the shape also it passes the shape validation inspections, I recieve the success page, when the form has errors, I recieve the shape back with a few error messages.

Ok now what I wish to do may be the database stuff. I've an idea of methods I'm able to obtain the Publish values in the registration form into my database, but not a clue the way i can see if a username or email already is available, and when so, display that error around the registration form. Here's my registration form view:

<?php $this->load->helper('form'); ?>
<?php echo form_open('register'); ?>
    <ul id="register">
        <ul>
            <h3>Account information</h3>
            <li>
                <label for="username">Choose a username</label>
                <input type="text" name="username" value="<?php echo set_value('username'); ?>" />
                <span class="desc">The name you'd like to be known by</span>
                <?php echo form_error('username'); ?>
            </li>
            <li>
                <label for="password">Pick a password</label>
                <input type="password" name="password" />
                <span class="desc">The best passwords are random and more than 6 characters long</span>
                <?php echo form_error('password'); ?>
            </li>
            <li>
                <label for="email">Enter your valid email address</label>
                <input type="text" name="email" value="<?php echo set_value('email'); ?>" />
                <span class="desc">We'll send you an activation email</span>
                <?php echo form_error('email'); ?>
            </li>
        </ul>
        <ul>
            <h3>About you</h3>
            <li>
                <label for="band">Who's your favorite artist?</label>
                <input type="text" name="artist" value="<?php echo set_value('artist'); ?>" />
                <span class="desc">Don't put Lady GaGa.</span>
                <?php echo form_error('artist'); ?>
            </li>
        </ul>
        <ul>
            <h3>Security question</h3>
            <li>
                <label for="captcha">Enter the letters you see in the image</label>
                <?php $this->load->helper('captcha');
                $cap = create_captcha(array('img_path' => './captcha/', 'img_url' => 'http://localhost/captcha/', 'img_width' => 200, 'img_height' => 30));
                $data = array('captcha_time' => $cap['time'], 'ip_address' => $this->input->ip_address(), 'word' => $cap['word']);
                $query = $this->db->insert_string('captcha', $data);
                $this->db->query($query);
                echo $cap['image']; ?>
                <input type="text" name="captcha" />
                <?php echo form_error('captcha'); ?>
            </li>
        </ul>
        <ul>
            <h3 class="submit">
                <input type="submit" value="Register" />
            </h3>
        </ul>
    </ul>
<?php echo form_close(); ?>

As you can tell, I am benefiting from the form_error() purpose of CI to show form errors right underneath the area, and I'd like the "username already is available" error also to be shown underneath the username area.

Can anybody provide outside assistance? A nudge within the right direction?

Thanks!