Welcome Guest, Not a member yet? Register   Sign In
Is this the right way of structuring MVC?
#1

[eluser]behnampmdg3[/eluser]
Hello friends;

I am new to CI and MVC. This is a very basic email validation (both format and with database) system.

I was wondering if I am doing ok or I am totally off track. Please comment or give me tips that I could improve my knowledge and turn them to skills and I become invincible. Thanks


Model
Code:
class Login_model extends CI_Model
{
  function valid_member($email)
   {
    $query = "SELECT * FROM members WHERE email='".$email."'";
    $database_results= $this->db->query($query);
    if($database_results->num_rows()==1)
     {
      foreach ($database_results->result() as $row)
       {
        $data['this_name'] = $row->name;
       }
       $this->load->vars($data);
      return true;
     }
    else
     {
      return false;
     }
   }
}

View
Code:
<div id="common_div">&lt;?php echo $validation_results; echo $this_name;?&gt;
&lt;form action ="&lt;?php echo site_url();?&gt;register" method="post"&gt;
<table>
  <tr>
   <td>Email</td>
   <td>&lt;input class = "text-box" type="text" name="email" value="&lt;?php echo $email;?&gt;" id="email" /&gt;&lt;/td>
  </tr>
  <tr>
   <td>Password</td>
   <td>&lt;input class = "text-box" type="password" name="password" /&gt;&lt;/td>
  </tr>
  <tr>
   <td colspan="2">&lt;input type="submit" value="Register" name="submit" class="input_submit"/&gt;&lt;/td>
  </tr>
</table>
&lt;/form&gt;
</div>



Controller
Code:
class Register extends CI_Controller
{

public function index()
  {
   $data['title'] = "Register Page";
   $this->load->vars($data);
   $this->form();
  }
public function form()
  {
   if(isset($_POST['submit']))
    {
     if($this->validate($this->input->post('email')))
      {
        
      }
    }
   $this->view_things();
   }
  public function validate($email=NULL)
   {
    $this->form_validation->set_rules('email', 'Email', 'required|valid_email|callback_validate_membership');
    if ($this->form_validation->run())
     {
      return true;
     }
    else
     {
      return false;
     }
   }
  
  public function validate_membership($email)
   {
    $this->load->model('login_model');
    if($this->login_model->valid_member($email))
     {
      return true;
     }
    else
     {
      $this->form_validation->set_message('validate_membership', 'This email is not registered in the database');
      return false;
     }
   }
  public function view_things()
  {
   $this->load->view('header_view');
   $this->load->view('register_view');
   $this->load->view('footer_view');
  }
}
#2

[eluser]ahmed.alsiddig[/eluser]
i think that was good.
you must practice to be a professional




Theme © iAndrew 2016 - Forum software by © MyBB