Last active
August 29, 2015 14:03
-
-
Save Johndllee/0834987d9caf1090dcb0 to your computer and use it in GitHub Desktop.
integrate the simple CRUD and login system
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Home extends CI_Controller { | |
public function index(){ | |
if($this->session->userdata('logged_in')){ | |
//Get the logged in users id | |
$user_id = $this->session->userdata('user_id'); | |
//Get all lists from the model | |
// $this->load->library('/controllers/person'); to load the library | |
$data['person'] = $this->Person_model->list_all(); | |
//$data['lists'] = $this->List_model->get_all_lists($user_id); | |
// $data['tasks'] = $this->Task_model->get_users_tasks($user_id); | |
} | |
$data['main_content'] = 'home'; | |
$this->load->view('layouts/main',$data); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Person extends CI_Controller { | |
// num of records per page | |
private $limit = 10; | |
function __construct() | |
{ | |
parent::__construct(); | |
// load library | |
$this->load->library(array('table','form_validation')); | |
// load helper | |
$this->load->helper('url'); | |
// load model | |
$this->load->model('Person_model','',TRUE); | |
} | |
function index($offset = 0) | |
{ | |
// offset | |
$uri_segment = 3; | |
$offset = $this->uri->segment($uri_segment); | |
// load data | |
$persons = $this->Person_model->get_paged_list($this->limit, $offset)->result(); | |
// generate pagination | |
$this->load->library('pagination'); | |
$config['base_url'] = site_url('person/index/'); | |
$config['total_rows'] = $this->Person_model->count_all(); | |
$config['per_page'] = $this->limit; | |
$config['uri_segment'] = $uri_segment; | |
$this->pagination->initialize($config); | |
$data['pagination'] = $this->pagination->create_links(); | |
// generate table data | |
$this->load->library('table'); | |
$this->table->set_empty(" "); | |
$this->table->set_heading('No', 'Name', 'Gender', 'Date of Birth (dd-mm-yyyy)', 'Actions'); | |
$i = 0 + $offset; | |
foreach ($persons as $person) | |
{ | |
$this->table->add_row(++$i, $person->name, strtoupper($person->gender)=='M'? 'Male':'Female', date('d-m-Y',strtotime($person->dob)), | |
anchor('person/view/'.$person->id,'view',array('class'=>'view')).' '. | |
anchor('person/update/'.$person->id,'update',array('class'=>'update')).' '. | |
anchor('person/delete/'.$person->id,'delete',array('class'=>'delete','onclick'=>"return confirm('Are you sure want to delete this person?')")) | |
); | |
} | |
$data['table'] = $this->table->generate(); | |
// load view | |
$this->load->view('personList', $data); | |
} | |
function add() | |
{ | |
// set empty default form field values | |
$this->_set_fields(); | |
// set validation properties | |
$this->_set_rules(); | |
// set common properties | |
$data['title'] = 'Add new person'; | |
$data['message'] = ''; | |
$data['action'] = site_url('person/addPerson'); | |
$data['link_back'] = anchor('person/index/','Back to list of persons',array('class'=>'back')); | |
// load view | |
$this->load->view('personEdit', $data); | |
} | |
function addPerson() | |
{ | |
// set common properties | |
$data['title'] = 'Add new person'; | |
$data['action'] = site_url('person/addPerson'); | |
$data['link_back'] = anchor('person/index/','Back to list of persons',array('class'=>'back')); | |
// set empty default form field values | |
$this->_set_fields(); | |
// set validation properties | |
$this->_set_rules(); | |
// run validation | |
if ($this->form_validation->run() == FALSE) | |
{ | |
$data['message'] = ''; | |
} | |
else | |
{ | |
// save data | |
$person = array('name' => $this->input->post('name'), | |
'gender' => $this->input->post('gender'), | |
'dob' => date('Y-m-d', strtotime($this->input->post('dob')))); | |
$id = $this->Person_model->save($person); | |
// set user message | |
$data['message'] = '<div class="success">add new person success</div>'; | |
} | |
// load view | |
$this->load->view('personEdit', $data); | |
} | |
function view($id) | |
{ | |
// set common properties | |
$data['title'] = 'Person Details'; | |
$data['link_back'] = anchor('person/index/','Back to list of persons',array('class'=>'back')); | |
// get person details | |
$data['person'] = $this->Person_model->get_by_id($id)->row(); | |
// load view | |
$this->load->view('personView', $data); | |
} | |
function update($id) | |
{ | |
// set validation properties | |
$this->_set_rules(); | |
// prefill form values | |
$person = $this->Person_model->get_by_id($id)->row(); | |
$this->form_data->id = $id; | |
$this->form_data->name = $person->name; | |
$this->form_data->gender = strtoupper($person->gender); | |
$this->form_data->dob = date('d-m-Y',strtotime($person->dob)); | |
// set common properties | |
$data['title'] = 'Update person'; | |
$data['message'] = ''; | |
$data['action'] = site_url('person/updatePerson'); | |
$data['link_back'] = anchor('person/index/','Back to list of persons',array('class'=>'back')); | |
// load view | |
$this->load->view('personEdit', $data); | |
} | |
function updatePerson() | |
{ | |
// set common properties | |
$data['title'] = 'Update person'; | |
$data['action'] = site_url('person/updatePerson'); | |
$data['link_back'] = anchor('person/index/','Back to list of persons',array('class'=>'back')); | |
// set empty default form field values | |
$this->_set_fields(); | |
// set validation properties | |
$this->_set_rules(); | |
// run validation | |
if ($this->form_validation->run() == FALSE) | |
{ | |
$data['message'] = ''; | |
} | |
else | |
{ | |
// save data | |
$id = $this->input->post('id'); | |
$person = array('name' => $this->input->post('name'), | |
'gender' => $this->input->post('gender'), | |
'dob' => date('Y-m-d', strtotime($this->input->post('dob')))); | |
$this->Person_model->update($id,$person); | |
// set user message | |
$data['message'] = '<div class="success">update person success</div>'; | |
} | |
// load view | |
$this->load->view('personEdit', $data); | |
} | |
function delete($id) | |
{ | |
// delete person | |
$this->Person_model->delete($id); | |
// redirect to person list page | |
redirect('person/index/','refresh'); | |
} | |
// set empty default form field values | |
function _set_fields() | |
{ | |
$this->form_data->id = ''; | |
$this->form_data->name = ''; | |
$this->form_data->gender = ''; | |
$this->form_data->dob = ''; | |
} | |
// validation rules | |
function _set_rules() | |
{ | |
$this->form_validation->set_rules('name', 'Name', 'trim|required'); | |
$this->form_validation->set_rules('gender', 'Gender', 'trim|required'); | |
$this->form_validation->set_rules('dob', 'DoB', 'trim|required|callback_valid_date'); | |
$this->form_validation->set_message('required', '* required'); | |
$this->form_validation->set_message('isset', '* required'); | |
$this->form_validation->set_message('valid_date', 'date format is not valid. dd-mm-yyyy'); | |
$this->form_validation->set_error_delimiters('<p class="error">', '</p>'); | |
} | |
// date_validation callback | |
function valid_date($str) | |
{ | |
//match the format of the date | |
if (preg_match ("/^([0-9]{2})-([0-9]{2})-([0-9]{4})$/", $str, $parts)) | |
{ | |
//check weather the date is valid of not | |
if(checkdate($parts[2],$parts[1],$parts[3])) | |
return true; | |
else | |
return false; | |
} | |
else | |
return false; | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Users extends CI_Controller{ | |
public function register(){ | |
if($this->session->userdata('logged_in')){ | |
redirect('home/index'); | |
} | |
$this->form_validation->set_rules('first_name','First Name','trim|required|xss_clean'); | |
$this->form_validation->set_rules('last_name','Last Name','trim|required|xss_clean'); | |
$this->form_validation->set_rules('email','Email','trim|required|valid_email|xss_clean'); | |
$this->form_validation->set_rules('username','Username','trim|required|min_length[4]|xss_clean'); | |
$this->form_validation->set_rules('password','Password','trim|required|min_length[4]|max_length[50]|xss_clean'); | |
$this->form_validation->set_rules('password2','Confirm Password','trim|required|matches[password]|xss_clean'); | |
if($this->form_validation->run() == FALSE){ | |
//Load view and layout | |
$data['main_content'] = 'users/register'; | |
$this->load->view('layouts/main',$data); | |
//Validation has ran and passed | |
} else { | |
if($this->User_model->create_member()){ | |
$this->session->set_flashdata('registered', 'You are now registered, please log in'); | |
//Redirect to index page with error above | |
redirect('home/index'); | |
} | |
} | |
} | |
public function login(){ | |
$this->form_validation->set_rules('username','Username','trim|required|min_length[4]|xss_clean'); | |
$this->form_validation->set_rules('password','Password','trim|required|min_length[4]|max_length[50]|xss_clean'); | |
if($this->form_validation->run() == FALSE){ | |
//Set error | |
$this->session->set_flashdata('login_failed', 'Sorry, the login info that you entered is invalid'); | |
redirect('home/index'); | |
} else { | |
//Get from post | |
$username = $this->input->post('username'); | |
$password = $this->input->post('password'); | |
//Get user id from model | |
$user_id = $this->User_model->login_user($username,$password); | |
//Validate user | |
if($user_id){ | |
//Create array of user data | |
$user_data = array( | |
'user_id' => $user_id, | |
'username' => $username, | |
'logged_in' => true | |
); | |
//Set session userdata | |
$this->session->set_userdata($user_data); | |
redirect('home/index'); | |
} else { | |
//Set error | |
$this->session->set_flashdata('login_failed', 'Sorry, the login info that you entered is invalid'); | |
redirect('home/index'); | |
} | |
} | |
} | |
public function logout(){ | |
//Unset user data | |
$this->session->unset_userdata('logged_in'); | |
$this->session->unset_userdata('user_id'); | |
$this->session->unset_userdata('username'); | |
$this->session->sess_destroy(); | |
//Set message | |
$this->session->set_flashdata('logged_out', 'You have been logged out'); | |
redirect('home/index'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Person_model extends CI_Model { | |
//private $tbl_person= 'tbl_person'; | |
function __construct(){ | |
parent::__construct(); | |
} | |
function list_all(){ | |
$this->db->order_by('id','asc'); | |
return $this->db->get('tbl_person'); | |
} | |
function count_all(){ | |
return $this->db->count_all($this->tbl_person); | |
} | |
function get_paged_list($limit = 10, $offset = 0){ | |
$this->db->order_by('id','asc'); | |
return $this->db->get($this->tbl_person, $limit, $offset); | |
} | |
function get_by_id($id){ | |
$this->db->where('id', $id); | |
return $this->db->get($this->tbl_person); | |
} | |
function save($person){ | |
$this->db->insert($this->tbl_person, $person); | |
return $this->db->insert_id(); | |
} | |
function update($id, $person){ | |
$this->db->where('id', $id); | |
$this->db->update($this->tbl_person, $person); | |
} | |
function delete($id){ | |
$this->db->where('id', $id); | |
$this->db->delete($this->tbl_person); | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class User_model extends CI_Model{ | |
public function create_member(){ | |
$new_member_insert = array( | |
'first_name' => $this->input->post('first_name'), | |
'last_name' => $this->input->post('last_name'), | |
'email' => $this->input->post('email'), | |
'username' => $this->input->post('username'), | |
'password' => md5($this->input->post('password')) | |
); | |
$insert = $this->db->insert('users', $new_member_insert); | |
return $insert; | |
} | |
public function login_user($username,$passowrd){ | |
//Secure password | |
$enc_password = md5($passowrd); | |
//Validate | |
$this->db->where('username',$username); | |
$this->db->where('password',$enc_password); | |
$result = $this->db->get('users'); | |
if($result->num_rows() == 1){ | |
return $result->row(0)->id; | |
} else { | |
return false; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--Display Messages--> | |
<?php if($this->session->flashdata('registered')) : ?> | |
<?php echo '<p class="text-success">' .$this->session->flashdata('registered') . '</p>'; ?> | |
<?php endif; ?> | |
<?php if($this->session->flashdata('logged_out')) : ?> | |
<?php echo '<p class="text-success">' .$this->session->flashdata('logged_out') . '</p>'; ?> | |
<?php endif; ?> | |
<?php if($this->session->flashdata('need_login')) : ?> | |
<?php echo '<p class="text-error">' .$this->session->flashdata('need_login') . '</p>'; ?> | |
<?php endif; ?> | |
<h1>Welcome to CI Test!</h1> | |
<p>CI Test </p> | |
<?php if($this->session->userdata('logged_in')) : ?> | |
<br /> | |
<!--Display Errors--> | |
//<?php $this->load->view('person'); ?> //line 17 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<a href="index.php"><title>login CRUD</title></a> | |
<link href="<?php echo base_url(); ?>assets/css/bootstrap.css" rel="stylesheet"> | |
<link href="<?php echo base_url(); ?>assets/css/custom.css" rel="stylesheet"> | |
<link href="<?php echo base_url(); ?>res/css/style.css" rel="stylesheet" type="text/css" /> | |
<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script> | |
</head> | |
<body> | |
<div class="container"> | |
<div class="container navbar navbar-fixed-top span11"> | |
<div class="navbar-inner navbar-ct-blue"> | |
<div class="container-fluid"> | |
<a class="brand" href="#"></a> | |
<div class="nav-collapse collapse"> | |
<p class="navbar-text pull-right"> | |
<!--RIGHT TOP CONTENT--> | |
<?php if($this->session->userdata('logged_in')) : ?> | |
Welcome,  <font style="text-transform: uppercase;" color="white"><strong><?php echo $this->session->userdata('username'); ?></strong></font> | |
<?php else : ?> | |
<a href="<?php echo base_url(); ?>users/register">Register</a> | |
<?php endif; ?> | |
</p> | |
<ul class="nav nav-pills"> | |
<li class="active"><a href="<?php echo base_url(); ?>"><i class="icon-home icon-white"></i>Home</a></li> | |
<?php if($this->session->userdata('logged_in')) : ?> | |
<li><a href="<?php echo base_url(); ?>lists">My List</a></li> | |
<?php endif; ?> | |
</ul> | |
</div><!--/.nav-collapse --> | |
</div> | |
</div> | |
</div> | |
<div class="container-fluid span11"> | |
<div class="row-fluid"> | |
<div class="span3"> | |
<div class="well sidebar-nav"> | |
<div style="margin:0 0 10px 10px;"> | |
<!--SIDEBAR CONTENT--> | |
<?php $this->load->view('users/login'); ?> | |
</div> | |
</div><!--/.well --> | |
</div><!--/span--> | |
<div class="span9"> | |
<!--MAIN CONTENT--> | |
<?php $this->load->view($main_content); ?> | |
</div><!--/span--> | |
</div><!--/row--> | |
<hr> | |
<footer> | |
<p>By Psionics</p> | |
</footer> | |
</div><!--/.fluid-container--> | |
</div><!--/.container--> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="content"> | |
<h1>Simple CRUD Application</h1> | |
<div class="paging"><?php echo $pagination; ?></div> | |
<div class="data"><?php echo $table; ?></div> | |
<br /> | |
<?php echo anchor('person/add/','add new data',array('class'=>'add')); ?> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if($this->session->userdata('logged_in')) : ?> | |
<p>You are logged in as <?php echo $this->session->userdata('username'); ?></p> | |
<!--Start Form--> | |
<?php $attributes = array('id' => 'logout_form', | |
'class' => 'form-horizontal'); ?> | |
<?php echo form_open('users/logout',$attributes); ?> | |
<!--Submit Buttons--> | |
<?php $data = array("value" => "Logout", | |
"name" => "submit", | |
"class" => "btn btn-primary"); ?> | |
<?php echo form_submit($data); ?> | |
<?php echo form_close(); ?> | |
<?php else : ?> | |
<h3>Login Form</h3> | |
<?php if($this->session->flashdata('login_failed')) : ?> | |
<?php echo '<p class="text-error">' .$this->session->flashdata('login_failed') . '</p>'; ?> | |
<?php endif; ?> | |
<!--Start Form--> | |
<?php $attributes = array('id' => 'login_form', | |
'class' => 'form-horizontal'); ?> | |
<?php echo form_open('users/login',$attributes); ?> | |
<!--Field: Username--> | |
<p> | |
<?php echo form_label('Username:'); ?> | |
<?php | |
$data = array( | |
'name' => 'username', | |
'placeholder' => 'Enter Username', | |
'style' => 'width:90%', | |
'value' => set_value('username') | |
); | |
?> | |
<?php echo form_input($data); ?> | |
</p> | |
<!--Field: Password--> | |
<p> | |
<?php echo form_label('Password:'); ?> | |
<?php | |
$data = array( | |
'name' => 'password', | |
'placeholder' => 'Enter Password', | |
'style' => 'width:70%', | |
'value' => set_value('password') | |
); | |
?> | |
<?php echo form_password($data); ?> | |
</p> | |
<br /> | |
<p> | |
<!--Submit Buttons--> | |
<?php $data = array("value" => "Login", | |
"name" => "submit", | |
"class" => "btn btn-primary"); ?> | |
<?php echo form_submit($data); ?> | |
</p> | |
<?php echo form_close(); ?> | |
<?php endif; ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Register</h1> | |
<p>Please fill out the form below to create an account</p> | |
<!--Display Errors--> | |
<?php echo validation_errors('<p class="text-error">'); ?> | |
<?php echo form_open('users/register'); ?> | |
<!--Field: First Name--> | |
<p> | |
<?php echo form_label('First Name:'); ?> | |
<?php | |
$data = array( | |
'name' => 'first_name', | |
'value' => set_value('first_name') | |
); | |
?> | |
<?php echo form_input($data); ?> | |
</p> | |
<!--Field: Last Name--> | |
<p> | |
<?php echo form_label('Last Name:'); ?> | |
<?php | |
$data = array( | |
'name' => 'last_name', | |
'value' => set_value('last_name') | |
); | |
?> | |
<?php echo form_input($data); ?> | |
</p> | |
<!--Field: Email Address--> | |
<p> | |
<?php echo form_label('Email Address:'); ?> | |
<?php | |
$data = array( | |
'name' => 'email', | |
'value' => set_value('email') | |
); | |
?> | |
<?php echo form_input($data); ?> | |
</p> | |
<!--Field: Username--> | |
<p> | |
<?php echo form_label('Username:'); ?> | |
<?php | |
$data = array( | |
'name' => 'username', | |
'value' => set_value('username') | |
); | |
?> | |
<?php echo form_input($data); ?> | |
</p> | |
<!--Field: Password--> | |
<p> | |
<?php echo form_label('Password:'); ?> | |
<?php | |
$data = array( | |
'name' => 'password', | |
'value' => set_value('password') | |
); | |
?> | |
<?php echo form_password($data); ?> | |
</p> | |
<!--Field: Password2--> | |
<p> | |
<?php echo form_label('Confirm Password:'); ?> | |
<?php | |
$data = array( | |
'name' => 'password2', | |
'value' => set_value('password2') | |
); | |
?> | |
<?php echo form_password($data); ?> | |
</p> | |
<!--Submit Buttons--> | |
<?php $data = array("value" => "Register", | |
"name" => "submit", | |
"class" => "btn btn-primary"); ?> | |
<p> | |
<?php echo form_submit($data); ?> | |
</p> | |
<?php echo form_close(); ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment