Welcome Guest, Not a member yet? Register   Sign In
Live Tutorial Starting in 20 Minutes
#1

[eluser]Michael Wales[/eluser]
Live Tutorial: CodeIgniter Blog

At 1000 PST I will start "live-blogging" the development of a blog in CodeIgniter. Right now I am just aiming for basic functionality - user login, posting, and commenting. We may add in Categories if things go well, I don't know yet.

So, check out that post - and start refreshing around 1005 / 1010 when I get the first update out there.
#2

[eluser]tucspl[/eluser]
awesome
#3

[eluser]tucspl[/eluser]
ok, so are you working on it right now?
#4

[eluser]Michael Wales[/eluser]
I ended the live tutorial a bit earlier than I had planned, but it's been a fast-paced hour and I think there is tons of information there for the beginner to soak up:

- MVC Concepts (minus the M)
- Basic User Validation
- Form Validation
- Active Record (get, getwhere, and insert)
- Passing Data from Controller to View
- Using Sessions

My Challenge to You:
Finish up the blog controller by writing the article() method, which should display a blog post in it's entirety.

Add the ability for non-logged in users to post comments. The basic concepts are already there with the posting ability, just modify it for commenting (don't forget the post_id field).

Closing Thoughts
I'm not sure if blogging is the right format for a Live Tutorial. It was an interesting test, but in the end I was focused on going as quickly as possible to keep new content hitting the page often.

I would definitely be willing to perform some live video tutorials via Kyte.tv, Skype, etc - is anyone interested in this?
#5

[eluser]tucspl[/eluser]
quick question,
Parse error: parse error, unexpected ';', expecting T_FUNCTION in /home/chainrea/public_html/dev/infrastructure/application/controllers/user.php on line 91

I am not 100% positive what is causing this.
#6

[eluser]Michael Wales[/eluser]
Are you copy-pasting the code or retyping. I have no errors... I'll repaste here, just to make sure yours is correct:

Code:
// Check username/email for uniqueness
    function _check_username($username) {
        $email = $this->validation->email;
        $query = $this->db->getwhere('users', array('username'=>$username), 1, 0);
        if ($query->num_rows() != 0) {
            $this->validation->set_message('_check_username', 'This username has already been registered');
            return FALSE;
        } else {
            $query = $this->db->getwhere('users', array('email'=>$email), 1, 0);
            if ($query->num_rows() != 0) {
                $this->validation->set_message('_check_username', 'This email address has already been registered');
                return FALSE;
            }
        }
        return TRUE;
    }
#7

[eluser]tucspl[/eluser]
this is so wierd, i copied and pasted all of your code. I have another error:


Parse error: parse error, unexpected ';', expecting T_FUNCTION in /home/chainrea/public_html/dev/infrastructure/application/controllers/blog.php on line 54
#8

[eluser]tucspl[/eluser]
This is my blog.php:
Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Blog extends Controller {

    function Blog() {
        parent::Controller();
    }

    // Our front page
    function index() {
        if ($this->session->userdata('logged_in')) {
            $data['user']['logged_in'] = TRUE;
        } else {
            $data['user']['logged_in'] = FALSE;
        }
        $query = $this->db->get('posts');
        foreach ($query->result() as $row) {
            $data['posts'][$row->id]['title'] = $row->title;
        }
        $this->load->view('blog/front', $data);
    }

    // We're viewing a single blog post
    function article() {
        $this->load->view('blog/article');
    }

    // We're writing a post
    function write() {
        if ($this->session->userdata('logged_in') === FALSE) {
            redirect('blog');
        }
        $this->load->library('validation');
        $rules['title'] = 'trim|required|min_length[3]|max_length[255]';
        $rules['body'] = 'trim|required';
        $this->validation->set_rules($rules);
        $fields['title'] = 'title';
        $fields['body'] = 'body';
        $this->validation->set_fields($fields);
        if ($this->validation->run()) {
            $this->load->helper('date');
            $insert = array(
                'title' => $this->input->post('title'),
                'slug' => url_title($this->input->post('title')),
                'author_id' => $this->session->userdata('logged_in'),
                'body' => $this->input->post('body'),
                'created_on' => now()
            );
            $this->db->insert('posts', $insert);
            redirect('blog');
        } else {
            $this->load->view('blog/write');
        }
    }

?>

This is my user.php:
Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class User extends Controller {

    function Blog() {
        parent::Controller();
    }

    // If logged in, send to front page
    // If not logged in, send to login form
    function index() {
    }

    // User Registration
    function register() {
        $this->load->library('validation');
        $rules['username'] = 'trim|required|min_length[3]|max_length[20]|callback__check_username';
        $rules['email'] = 'trim|required|valid_email|max_length[100]';
        $rules['display_name'] = 'trim|required|min_length[3]|max_length[40]';
        $rules['password'] = 'trim|required|min_length[3]|max_length[20]';
        $rules['password_conf'] = 'trim|matches[password]';
        $this->validation->set_rules($rules);
        $fields['username'] = 'username';
        $fields['email'] = 'email address';
        $fields['display_name'] = 'display name';
        $fields['password'] = 'password';
        $fields['password_conf'] = 'password confirmation';
        $this->validation->set_fields($fields);
        if ($this->validation->run()) {
            $this->load->helper('security');
            $insert = array(
                'username' => $this->input->post('username'),
                'email' => $this->input->post('email'),
                'display_name' => $this->input->post('display_name'),
                'password' => dohash($this->input->post('password'))
                );
            $this->db->insert('users', $insert);
            redirect('blog');
        } else {
            $this->load->view('user/register');
        }
    

    // User Login
    function login() {
        $this->load->library('validation');
        $rules['username'] = 'trim|required|callback__check_login';
        $rules['password'] = 'trim|required';
        $this->validation->set_rules($rules);
        $fields['username'] = 'username';
        $fields['password'] = 'password';
        $this->validation->set_fields($fields);
        if ($this->validation->run()) {
            $query = $this->db->getwhere('users', array('username' => $this->input->post('username')));
            $row = $query->row();
            $this->session->set_userdata('logged_in', $row->id);
            redirect('blog');
        } else {
            $this->load->view('user/login');
        }
    }


    // Check username/email for uniqueness
    function _check_username($username) {
        $email = $this->validation->email;
        $query = $this->db->getwhere('users', array('username'=>$username), 1, 0);
        if ($query->num_rows() != 0) {
            $this->validation->set_message('_check_username', 'This username has already been registered');
            return FALSE;
        } else {
            $query = $this->db->getwhere('users', array('email'=>$email), 1, 0);
            if ($query->num_rows() != 0) {
                $this->validation->set_message('_check_username', 'This email address has already been registered');
                return FALSE;
            }
        }
        return TRUE;
    }
    
    // Check the login information
    function _check_login($username) {
        $this->load->helper('security');
        $password = dohash($this->validation->password);
        $query = $this->db->getwhere('users', array('username'=>$username, 'password'=>$password), 1, 0);
        if ($query->num_rows() == 0) {
            $this->validation->set_message('_check_login', 'Invalid login');
            return FALSE;
        }
        return TRUE;
    }
}

?>

both errors they are going to consist of a "}".
#9

[eluser]Michael Wales[/eluser]
Are you on PHP4 or PHP5?
#10

[eluser]tucspl[/eluser]
php 5




Theme © iAndrew 2016 - Forum software by © MyBB