Learning Curve- Perfect Tutorial for the beginers
Follow us on Twitter Follow us on Facebook Follow us on Google Plus Follow us on rss
Skip to content
  • Home
  • APS1.0
  • APS2.0
  • PHP
  • SQ L& MYsql
  • Linux Shell Scripting
  • Codeigniter
  • Technical questions
  • Laravel
  • Angularjs
  • Node js
  • English Corner

Simple Login/Edit/Delete/Entry Application using codeigniter

By Admin | July 18, 2014
0 Comment

Database Creation

create a database “my_testdb”. use the below query for generationg tables.

CREATE TABLE IF NOT EXISTS questionandanswers (
ID int(10) NOT NULL AUTO_INCREMENT,
question longtext NOT NULL,
answer1 varchar(250) NOT NULL,
answer2 varchar(250) NOT NULL,
answer3 varchar(250) NOT NULL,
answer4 varchar(250) NOT NULL,
answer5 varchar(250) NOT NULL,
correctanswer varchar(250) NOT NULL,
PRIMARY KEY (ID)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=28 ;
—

CREATE TABLE IF NOT EXISTS users (
id tinyint(4) NOT NULL AUTO_INCREMENT,
username varchar(10) NOT NULL,
password varchar(100) NOT NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

—
— Dumping data for table users
—

INSERT INTO users (id, username, password) VALUES
(2, ‘admin’, ‘123’);

Initial changes in the configuration file database.php

$db[‘default’][‘hostname’] = ‘localhost’;

$db[‘default’][‘username’] = ‘root’;

$db[‘default’][‘password’] = ”;

$db[‘default’][‘database’] = ‘my_testdb’;

$db[‘default’][‘dbdriver’] = ‘mysql’;

Following controllers,models,view files are used in this sample application.

Controllers:

1.login.php

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Login extends CI_Controller{
 
function index($msg = NULL)
{
$data['msg'] = $msg;
$this->load->view('login_view',$data);
}
function checklogin()
{
 
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('pwd', 'Password', 'required');
$this->form_validation->set_rules('name', 'Name', 'callback_testlogin|required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('login_view');
}
else
{
   redirect('home');
}
}
public function testlogin()
{
$this->load->model('login_model');
$result = $this->login_model->validate();
if(! $result){
$this->form_validation->set_message('testlogin', 'Invalid username/password');
return FALSE;
 
}else{
 
                   return true;
}
 
}
public function logout()
{
$this->session->sess_destroy();
redirect('login');
}
}

2. home.php

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
class Home extends CI_Controller{
function __construct(){
parent::__construct();
$this->check_isvalidated();
}
 
public function index(){
 
$this->load->view('home');
 
}
 
 
//inserting into database
function  insertquestions()
{
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules('questions', 'questions', 'required');
$this->form_validation->set_rules('answer1', 'answer1', 'required');
$this->form_validation->set_rules('correctanswer', 'correctanswer', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('home');
}
else
{
  
      $this->load->model('login_model');        
              if($this->input->post('Submit')){
                $this->login_model->insertquestion();                
                }
  
 
$this->session->set_flashdata('successmsg', 'Your question and answers added successfully');
   redirect('home');
}
 
}
 
 
 
 
 
private function check_isvalidated(){
if(! $this->session->userdata('validated')){
redirect('login');
}
}
}
?>

3.listview.php

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
class Listview extends CI_Controller{
function __construct(){
parent::__construct();
$this->check_isvalidated();
}
 
public function index(){
// If the user is validated, then this function will run
$this->load->view('listview');
//echo 'Congratulations, you are logged in.';
}
 
 
 
//list all questions and answers
 
public function listquestions($page_num=1,$sortfield='id',$order='asc') {        
    $this->load->library('pagination');
    $this->load->model('login_model');    
    $page_number = $this->uri->segment(3);
    $config['base_url'] = base_url().'index.php/listview/listquestions/';
    
    
    $config['per_page'] = 10;
    //$config['num_links'] = 5;
    if(empty($page_number)) $page_number = 1;
    $offset = ($page_number-1) * $config['per_page'];
    
    $config['use_page_numbers'] = TRUE;
    
    $data["usersdata"] = $this->login_model->listquestions($config['per_page'],$offset,$sortfield,$order);
    $config['total_rows'] = $this->db->count_all('questionandanswers');
    
    $config['full_tag_open'] = '<div id="pagination">';
    $config['full_tag_close'] = '</div>';        
    
    $this->pagination->cur_page = $offset;
            
    $this->pagination->initialize($config);
    $data['page_links'] = $this->pagination->create_links();
$data['totalrecords']=$config['total_rows'];
          
    $this->load->view('listview',$data);
}
 
public function deletequestions($sortfield='id',$order='asc')
{
$this->load->model('login_model');
$id=$this->uri->segment(3);
$this->login_model->deleterecord($id);
redirect('listview/listquestions/','refresh');
  
}
 
//for edit
public function questionedit()
{
$this->load->model('login_model');
$id=$this->uri->segment(3);
$output=$this->login_model->displayrecord($id);
    $data['id']= $output['ID'];
$data['question']= $output['question'];
$data['answer1']= $output['answer1'];
$data['answer2']= $output['answer2'];
$data['answer3']= $output['answer3'];
$data['answer4']= $output['answer4'];
$data['answer5']= $output['answer5'];
$data['correctanswer']= $output['correctanswer'];
$this->load->view('editview',$data);
}
//for updating data
 
public function updatedata()
{
$this->load->model('login_model');
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules('questions', 'questions', 'required');
$this->form_validation->set_rules('answer1', 'answer1', 'required');
$this->form_validation->set_rules('correctanswer', 'correctanswer', 'required');
$id=$this->input->post('hidenid');
if ($this->form_validation->run() == FALSE)
{
    
$output=$this->login_model->displayrecord($id);
$data['id']= $output['ID'];
$data['question']= $output['question'];
$data['answer1']= $output['answer1'];
$data['answer2']= $output['answer2'];
$data['answer3']= $output['answer3'];
$data['answer4']= $output['answer4'];
$data['answer5']= $output['answer5'];
$data['correctanswer']= $output['correctanswer'];
$this->load->view('editview',$data);
}
else
{
$this->login_model->updaterecord($id);
$output=$this->login_model->displayrecord($id);
 
    $data['id']= $output['ID'];
$data['question']= $output['question'];
$data['answer1']= $output['answer1'];
$data['answer2']= $output['answer2'];
$data['answer3']= $output['answer3'];
$data['answer4']= $output['answer4'];
$data['answer5']= $output['answer5'];
$data['correctanswer']= $output['correctanswer'];
$this->session->set_flashdata('successmsg', 'Your data updated successfully');
$this->load->view('editview',$data);
}
}
private function check_isvalidated(){
if(! $this->session->userdata('validated')){
redirect('login');
}
}
}

View files:

1.login_view.php

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <title>Login Demo-Admin Area</title>
   <style type="text/css">
  
   .error{
color: red;
font-size: 14px;
margin-bottom: -15px;
}
  
   </style>
</head>
<body>
   <h1>Login</h1>
   <h5>User Name</h5>
   <?php
echo form_open('login/checklogin', array('id'=>'form1'));
 
?>
<input type="text" name="name" value="<?php echo set_value('name'); ?>" size="50" /><?php echo form_error('name'); ?>
<h5>Password</h5>
<input type="password" name="pwd" value="<?php echo set_value('pwd'); ?>" size="50" />
<?php echo form_error('pwd'); ?>
<br>
<div><input type="submit" value="Submit" /></div>
</body>
</html>

2.home.php

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <title>Login Demo-Admin Area</title>
    <style type="text/css">
  
   .error{
color: red;
font-size: 13px;
margin-bottom: 65px;
}
.successmsg
{
color: green;
font-size: 16px;
}
  
   </style>
</head>
<body>
   <?php
  
   if($this->session->flashdata('successmsg')) {
   ?>
<p class='successmsg'>
<?php
echo ''.$this->session->flashdata('successmsg').'';?>
</p>
<?php } ?>
 
   <h2>Welcome </h2><br/>
   <a href="<?php echo site_url();?>/login/logout">Logout</a>
   <p>
   <a href="<?php echo site_url();?>/listview/listquestions">List</a></p>
        
<?php
echo form_open('home/insertquestions', array('id'=>'form1'));
?>
<table border="0">
<tr>
<td>Question</td><td><textarea rows="4" cols="50" name="questions"/></textarea></td><td>&nbsp;<?php echo form_error('questions'); ?></td> </tr>
<tr>
<td>Answer1</td><td><input type="text" name="answer1" value=""  /></td><td>&nbsp;<?php echo form_error('answer1'); ?></td>
</tr>
<tr>
<td>Answer2</td><td><input type="text" name="answer2" value=""  /></td><td>&nbsp;</td>
</tr>
<tr>
<td>Answer3</td><td><input type="text" name="answer3" value=""  /></td><td>&nbsp;</td>
</tr>
<tr>
<td>Answer4</td><td><input type="text" name="answer4" value=""  /></td><td>&nbsp;</td>
</tr>
<tr>
<td>Answer5</td><td><input type="text" name="answer5" value=""  /></td><td>&nbsp;</td>
</tr>
<tr>
<td>Correct Answer</td><td><input type="text" name="correctanswer" value=""  /></td><td>&nbsp;<?php echo form_error('correctanswer'); ?></td>
</tr>
<tr>
<td></td><td><input type="submit" value="Submit" name="Submit" /></td><td>&nbsp;</td>
</tr>
</table>
  
</body>
</html>

3.listview.php

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <title>Login Demo-Admin Area</title>
   </head>
<body>
 
   <h2>Welcome </h2><br/>
   <a href="<?php echo site_url();?>/login/logout">Logout</a>
   <p>
   <a href="<?php echo site_url();?>/home">Entry</a></p>
        
   <?php   
  $page_num = (int)$this->uri->segment(3);
  if($page_num==0) $page_num=1;
  $order_seg = $this->uri->segment(5,"asc");
  if($order_seg == "asc") $order = "desc"; else $order = "asc";
  if($totalrecords>0)
  {
  ?>
  
  
  <table cellpadding="0" cellspacing="0">
<tr>
                                <td><b><a href="<?php echo site_url();?>/listview/listquestions/<?=$page_num?>/id/<?=$order?>">ID</a></b></td>
                                <td><b><a href="<?php echo site_url();?>/listview/listquestions/<?=$page_num?>/question/<?=$order?>">Question</a></b></td>
                                <td><b><a href="<?php echo site_url();?>/listview/listquestions/<?=$page_num?>/answer1/<?=$order?>">Answer</a></b></td>
                                <td class="action"><b>Action</b></td>
                            </tr>
                            <?php
$i = 0;
foreach($usersdata as $val) { ?>
                               <tr class="odd">
                                    <td style="text-align:left;"><?=$val["ID"];?></td>
                                    <td style="text-align:left; vertical-align:middle;"><?=$val['question']?></td>
                                    <td style="text-align:left; vertical-align:middle;"><?=$val['correctanswer']?></td>
                                    <td class="action">
<?php echo anchor("listview/questionedit/".$val["ID"],"Edit",array("class"=>"edit")); ?>
&nbsp;&nbsp;
<?php echo anchor("listview/deletequestions/".$val["ID"]."/".$page_num,"Delete",array("class"=>"edit")); ?></td>
                                </tr>
                            <?php } ?>
                                        
                        </table><br /><br />
                        <center><?php echo $page_links; ?></center>
<?php } else { echo "No Entries found"; } ?>
                         <br /><br />  
                                  
<table>
<tr>
 
<tr>
</table>
  
</body>
</html>

4.editview.php

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <title>Login Demo-Admin Area</title>
    <style type="text/css">
  
   .error{
color: red;
font-size: 13px;
margin-bottom: 65px;
}
.successmsg
{
color: green;
font-size: 16px;
}
  
   </style>
</head>
<body>
   <?php
  
   if($this->session->flashdata('successmsg')) {
   ?>
<p class='successmsg'>
<?php
echo ''.$this->session->flashdata('successmsg').'';?>
</p>
<?php } ?>
 
   <h2>Welcome</h2><br/>
   <a href="<?php echo site_url();?>/login/logout">Logout</a>
   <p>
   <a href="<?php echo site_url();?>/listview/listquestions">List</a></p>
        
<?php
echo form_open('listview/updatedata', array('id'=>'form1'));
?>
<table>
<tr>
<input type="hidden" name="hidenid" value="<?php echo $id;?>"  />
<td>Question</td><td><textarea rows="4" cols="50" name="questions"/><?php echo $question;?></textarea></td><td>&nbsp;<?php echo form_error('questions'); ?></td> </tr>
<tr>
<td>Answer1</td><td><input type="text" name="answer1" value="<?php echo $answer1;?>"  /></td><td>&nbsp;<?php echo form_error('answer1'); ?></td>
</tr>
<tr>
<td>Answer2</td><td><input type="text" name="answer2" value="<?php echo $answer2;?>"  /></td><td>&nbsp;</td>
</tr>
<tr>
<td>Answer3</td><td><input type="text" name="answer3" value="<?php echo $answer3;?>"  /></td><td>&nbsp;</td>
</tr>
<tr>
<td>Answer4</td><td><input type="text" name="answer4" value="<?php echo $answer4;?>"  /></td><td>&nbsp;</td>
</tr>
<tr>
<td>Answer5</td><td><input type="text" name="answer5" value="<?php echo $answer5;?>"  /></td><td>&nbsp;</td>
</tr>
<tr>
<td>Correct Answer</td><td><input type="text" name="correctanswer" value="<?php echo $correctanswer;?>"  /></td><td>&nbsp;<?php echo form_error('correctanswer'); ?></td>
</tr>
<tr>
<td></td><td><input type="submit" value="Update" name="Submit" /></td>
</tr>
</table>
</body>
</html>

Models:
1.login_model.php

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
class Login_model extends CI_Model{
function __construct(){
parent::__construct();
}
 
public function validate(){
 
$username = $this->security->xss_clean($this->input->post('name'));
$password = $this->security->xss_clean($this->input->post('pwd'));
 
 
$this->db->where('username', $username);
$this->db->where('password', $password);
 
 
$query = $this->db->get('users');
 
if($query->num_rows() == 1)
{
 
$row = $query->row();
$data = array(
'userid' => $row->id,
'name' => $row->username,
'validated' => true
);
$this->session->set_userdata($data);
return true;
}
 
 
return false;
}
 
 
 
public function insertquestion()
{
$questions = $this->input->post('questions');
$answer1 = $this->input->post('answer1');
$answer2 = $this->input->post('answer2');
$answer3 = $this->input->post('answer3');
$answer4 = $this->input->post('answer4');
$answer5 = $this->input->post('answer5');
$correctanswer = $this->input->post('correctanswer');
                    
                    $data = array(
                        question=>$questions,
                        answer1=>$answer1,
answer2=>$answer2,
answer3=>$answer3,
answer4=>$answer4,            
answer5=>$answer5,
correctanswer=>$correctanswer);
                    $this->db->insert('questionandanswers',$data);  
 
}
 
public function listquestions($per_page,$offset,$sortfield,$order) {
        
        $this->db->select('*')->from('questionandanswers');
        $this->db->order_by("$sortfield", "$order");
        $this->db->limit($per_page,$offset);
        $query_result = $this->db->get();        
        
        if($query_result->num_rows() > 0) {
            foreach ($query_result->result_array() as $row)
            {
                $sdata[] = array('question' => $row['question'],'correctanswer' => $row['correctanswer'],'ID' => $row['ID']);
            }                
            return $sdata;
        } else {
            return false;    
        }
    }
//delete a record
 
public function deleterecord($id)
{
$this->db->delete('questionandanswers', array('ID' => $id));
}
 
//displaying into edit record
 
public function displayrecord($id)
{
 
$query = $this->db->get_where('questionandanswers',array('id'=>$id));
return $query->row_array();
 
}
 
//update in edit section
 
public function updaterecord($id)
{
$questions = $this->input->post('questions');
$answer1 = $this->input->post('answer1');
$answer2 = $this->input->post('answer2');
$answer3 = $this->input->post('answer3');
$answer4 = $this->input->post('answer4');
$answer5 = $this->input->post('answer5');
$correctanswer = $this->input->post('correctanswer');
$data = array(
                        'question'=>$questions,
                        'answer1'=>$answer1,
'answer2'=>$answer2,
'answer3'=>$answer3,
'answer4'=>$answer4,            
'answer5'=>$answer5,
'correctanswer'=>$correctanswer
);
 
$this->db->where('id', $id);
$this->db->update('questionandanswers', $data);
 
 
}
}

Category: Codeigniter Tags: CRUD application codeigniter, Data entry application using codeigniter, Delete in codeigniter, edit in codeigniter, Entry in codeigniter, Free code in codeigniter, login application using codeigniter, tutorials with source code in codeigniter
Post navigation
← creating edit link in codeigniter Find out a word from a string? →

Recent Posts

  • How to change mysql engine from Myisam to Innodb
  • Error: MySQL shutdown unexpectedly in XAMPP
  • nodemon is not working in windows.nodemon -v is showing errors
  • Error [ERR_REQUIRE_ESM]: require() of ES Module E:\node-course\notes-app\node_modules\chalk\source\index.js from E:\node-course\app.js not supported. Instead change the require of index.js in E:\node-course\app.js to a dynamic import() which is available in all CommonJS modules. at Object. (E:\node-course\notes-app\app.js:8:15) { code: ‘ERR_REQUIRE_ESM’
  • Word Power

Recent Comments

  • Shrinivasa on Delete in Laravel with join condition
  • Vasanthan pv on Force Download in laravel-file download in laravel
  • Trey Mhundwa on Force Download in laravel-file download in laravel
  • Vasanthan pv on Link not working in codeigniter, base_url() displaying “”http://::1” why?
  • adil on Link not working in codeigniter, base_url() displaying “”http://::1” why?

Archives

Categories

Tweets about mydoubtsin
Follow @mydoubtsin
web
statistics
Copyright 2016
mydoubts.in
Iconic One Theme | Powered by Wordpress