Home  • Framework • CodeIgniter

Make user registration application using CodeIgniter

codeigniter apps

Create required database table for your application

usermodel.sql
use test;

drop table if exists user;
create table user(
 id int(10) primary key auto_increment,
 username varchar(20) not null,
 email varchar(20),
 password varchar(50)
);

Create User Model

user.php
<?php
class UserModel extends Model{
  
  function UserModel(){
    parent::Model();
    $this->load->database();
  }
  
  function add_user($username,$password,$email){
    $this->db->query("insert into user(username,email,password)values('$username','$password')");
  }
  
  function delete_user($id){  
   $this->db->query("delete from user where id='$id'");
  }
  
  function get_users(){
   $result=$this->db->query("select * from user");
   return $result;
   
  }
  
  function edit_user($id,$username,$email,$password){
    $this->db->query("update user set username='$username',password='$password',email='$email' where id='$id'");
  }
 
}

Create User Controller

<?php
class User extends Controller{
  function User(){
     parent::Controller();
	
  }
  
  function index(){
	$this->load->model("usermodel"); 	
	$user_model=$this->usermodel;	
	$this->load->view("user_view",array("user_model"=>$user_model)); 
	
	
	
  }
  
  function create(){
	$username=$this->input("txtUsername");
	$password=$this->input("pwdPassword");
	$email=$this->input("email");	  	  
	$this->user->add_user($username,$email,$password);  
	  
  }
    
  function delete(){
	$id=$this->input("txtId");
	$this->user->delete_user($id);
  }
  
 
}

Create User View

user_view.php
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
 
<body>

<?php
 include("nav_user.php");
?>

<?php

 if($page==1){
	 
	include("include/get_user.php"); 
	 
 }else if($page==2){
	  
	include("include/create_user.php");  
 }

?>
</body>
</html>

Includes in view

include/get_user.php
<?php
 
  $users=$user_model->get_users();
 
echo "<table  cellpadding='4' cellspacing='4'>"; 
echo "<tr><th>ID</th><th>Name</th><th>Email</th></tr>";  
foreach($users->result_array() as $row){
 echo "<tr><td>".$row["id"]."</td><td>".$row["username"]."</td><td>".$row["email"]."</td></tr>";
}
echo "</table>";
 
?>
To be continue ...

Comments 2


thanks dear sir...
Thumbs up

Share