sumanth reddy sumanth reddy - 18 days ago 5
PHP Question

Managing views in CodeIgniter

I want to create different views for admin and user. I have created a page called members which will be displayed after user logs in successfully. But how to create another view for admin.

This is my controller:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Project extends CI_Controller {


public function index()
{
$this->login();
}

public function members()
{


$this->load->view('members');

}
public function singup()
{
$this->load->view('singup');
}
public function login()
{
$this->load->view('login');
}
public function login_validation()
{
$this->load->library('form_validation');
$this->form_validation- >set_rules('email','Email','required|trim|callback_validate_credentials');
$this->form_validation->set_rules('password','Password','required|md5');
if($this->form_validation->run())
{
$this->load->view('members');

}else{
$this->load->view('login');
}
}

function validate_credentials()
{
$this->load->model('Project_users');
if($this->Project_users->can_login_in())
{
return true;
}
else
{
$this->form_validation->set_message('validate_credentials','incorrect username/password');
return false;
}
}
public function singup_validation()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('email','Email','required|trim|valid_email|is_unique[users.email]');
$this->form_validation->set_rules('password','Password','required');
$this->form_validation->set_rules('cpassword','Confrim Password','required|matches[password]');
$this->form_validation->set_message('is_unique',"this email is already registed");
if($this->form_validation->run())
{
$this->load->model('project_users');
$this->project_users->add_temp_user();
}else{
echo "please singup";
$this->load->view('singup');
}
}
}


after login_validation is successful the user will be moved to member page.in same same way if admin logs in he should be moved to admin page.

this is my model.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Project_users extends CI_Model {
function can_login_in()
{
$this->db->where('email',$this->input->post('email'));
$this->db->where('password',md5($this->input->post('password')));

$query = $this->db->get('users');
if($query->num_rows() == 1){

return true;
}else{
return false;
}
}


public function add_temp_user()
{
$data=array('email'=>$this->input- >post('email'),'password'=>md5($this->input->post('password')));
$query=$this->db->insert('users',$data);
if($query){
echo "inserted";
}
else{
echo "colud not add to db";
}
}

}


i am very new to mvc and codeigniter

Answer

I don't know if this is what you want, but just for create another view, simply create other method for login and then load the new view.

public function adminlogin()
{
    $this->load->view('adminlogin');
}

public function admingloginvalidation()
{

    $this->load->library('form_validation');
    $this->form_validation->set_rules('email','Email','required|trim|callback_validate_credentials');
    $this->form_validation->set_rules('password','Password','required|md5');
    if ($this->form_validation->run()) {

        $this->load->view('adminmembers');

    } else {

        $this->load->view('adminlogin'); 

    }
}

then just point your browser to project/adminlogin, or you can create a new controller/view for the admin.