ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Live Tutorial Starting in 20 Minutes

September 11, 2007 1:46pm

Subscribe [1]
  • #1 / Sep 11, 2007 1:46pm

    Michael Wales

    2070 posts

    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 / Sep 11, 2007 2:02pm

    tucspl

    15 posts

    awesome

  • #3 / Sep 11, 2007 2:12pm

    tucspl

    15 posts

    ok, so are you working on it right now?

  • #4 / Sep 11, 2007 3:23pm

    Michael Wales

    2070 posts

    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 / Sep 11, 2007 3:29pm

    tucspl

    15 posts

    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 / Sep 11, 2007 3:31pm

    Michael Wales

    2070 posts

    Are you copy-pasting the code or retyping. I have no errors… I’ll repaste here, just to make sure yours is correct:

    // 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 / Sep 11, 2007 3:53pm

    tucspl

    15 posts

    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 / Sep 11, 2007 3:56pm

    tucspl

    15 posts

    This is my blog.php:

    <?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:

    <?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 / Sep 11, 2007 4:26pm

    Michael Wales

    2070 posts

    Are you on PHP4 or PHP5?

  • #10 / Sep 11, 2007 4:33pm

    tucspl

    15 posts

    php 5

  • #11 / Sep 11, 2007 5:09pm

    Michael Wales

    2070 posts

    wierd… for that tut specifically I was PHP5 on xampplite.

    If I get a chance I’ll try it at home tonight as well, but I am pretty sure everything is ok.

    Are you sure some quotes didn’t get turned into retarded characters after the copy-paste (and not literal quotes)?

  • #12 / Sep 11, 2007 5:11pm

    tucspl

    15 posts

    no retards i found.  Isn’t this weird. Maybe… I set a user for the database.  Would that be the problem?

  • #13 / Sep 11, 2007 5:26pm

    ELRafael

    274 posts

    try close the class

    class Blog extends Controller
    {
      ...
    } //See the close tag?
  • #14 / Sep 11, 2007 6:02pm

    tucspl

    15 posts

    try close the class

    class Blog extends Controller
    {
      ...
    } //See the close tag?

    closed all, still problem.  :blank:

  • #15 / Sep 12, 2007 4:52am

    bijon

    46 posts

    Hi,
    Thanks for new way write tutorial. I run your code and it works fine. Do u finish the comments parts ?  Can you please explain in the code :
                      $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’))
              );

    What will be do the dohash () function .

    Thanks

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases