Welcome Guest, Not a member yet? Register   Sign In
Is it good approach to CI ?
#1

[eluser]Unknown[/eluser]
Hello Im Maciek and I come from Poland.
Im new in Code Igniter community, and I started codding in Code Igniter Framework yesterday.

I wrote registring module, and I want some experts advices.

Is it good approach to CI ?

Controller

Code:
<?php

class Register extends CI_Controller {

    public function index()
    {
       $data['title'] = 'Register';
       $data['header'] = 'Come on and register !';
      
         $this->form_validation->set_rules('username', 'Username', 'required|alpha|min_length[3]|xss_clean');
         $this->form_validation->set_rules('password', 'Password', 'required|min_length[6]|xss_clean');
         $this->form_validation->set_rules('email', 'Email', 'required|valid_email');

            if ($this->form_validation->run() == false) {

                $this->load->view('view_register', $data);
                
            } else {
                
                $this->load->model('Register_model');
                $count = $this->Register_model->checkUser($this->input->post('username'));
                
                    if ($count > 0) {
                        
                        $data['error'] = '<p>There is the same user in database</p>';
                        $this->load->view('view_register', $data);
                        
                    } else {
                        
                        $count = $this->Register_model->checkMail($this->input->post('email'));
                        
                        if ($count > 0) {
                            
                            $data['error'] = '<p>There is the same e-mail in database</p>';
                            $this->load->view('view_register', $data);
                            
                        } else {
                            
                            $this->Register_model->addUser();
                            
                            $data['title'] = 'Congratulation';
                            $data['header'] = 'You already registered account';
                            $data['user'] = $this->input->post('username');
                            
                            $this->load->view('view_result', $data);
                            
                        }
                        
                    }
                
            }
    }
    
    
}

?&gt;

Model

Code:
&lt;?php

class Register_model extends CI_Model {
    
    
    public function checkUser($username)
    {
        
        $this->db->select('username');
        $this->db->where('username =', $username);
        $query = $this->db->get('users');
        
        return $query->num_rows();
        
    }
    
    public function checkMail($mail)
    {
        
        $this->db->select('email');
        $this->db->where('email =', $mail);
        $query = $this->db->get('users');
        
        return $query->num_rows();
        
    }
    
    public function addUser()
    {
        $this->username = $this->input->post('username');
        $this->password = md5($this->input->post('password'));
        $this->email = $this->input->post('email');
        
        $this->db->insert('users', $this);
        
    }
    

    
}

?&gt;

Views
view_register.php

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;

&lt;head&gt;
    &lt;meta http-equiv="content-type" content="text/html; charset=utf-8" /&gt;

    &lt;title&gt;&lt;?=$title;?&gt;&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;

<h1>&lt;?=$header;?&gt;</h1>

    <p>To jest Rejestracja</p>

    &lt;?=form_open('register');?&gt;
    
    Login:<br />
    &lt;?=form_input(array('name' => 'username'));?&gt;<br /><br />
    
    Password:<br />
    &lt;?=form_password(array('name' => 'password'));?&gt;<br /><br />
    
    E-mail:<br />
    &lt;?=form_input(array('name' => 'email'));?&gt;<br /><br />
    
    &lt;?=form_submit('register', 'Rgister');?&gt;
    &lt;?=form_close();?&gt;
    
    &lt;?php echo validation_errors(); ?&gt;
    
    &lt;?if (isset($error)) {echo($error);}?&gt;
    

&lt;/body&gt;
&lt;/html&gt;

view_result.php

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;

&lt;head&gt;
    &lt;meta http-equiv="content-type" content="text/html; charset=utf-8" /&gt;

    &lt;title&gt;&lt;?=$title;?&gt;&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;

<h1>&lt;?=$header;?&gt;</h1>

Thank you &lt;?=$user?&gt;<br />
You can &lt;?=anchor('login', 'login')?&gt; now


&lt;/body&gt;
&lt;/html&gt;

Thank a lot for all advices.
#2

[eluser]Eric Barnes[/eluser]
Personally I would move the "checkUser" and "checkMail" to validation callbacks. That way you can wrap the error message inside it.
#3

[eluser]toopay[/eluser]
[quote author="ReptileReX" date="1308848623"]I wrote registring module[/quote]

Look at community contribution on Wiki, theres some HMVC solution for you, to create "module". When you say module, its tends to separated mini application, rather than a Controller. So you can manage it more flexible.




Theme © iAndrew 2016 - Forum software by © MyBB