Tuesday, 15 October 2013

Create Change Password Module in codeigniter

Simple change password in code ignitor,if you r using md5 to store password....

1.In your view folder,make a .php file with name changepassword. and add this content.

<?php $this->load->library('form_validation');?>
<?php echo validation_errors();?>
<div class="content">

<?php echo form_open('main/changepwd'); ?>
<table class=”table table-bordered”>

<tbody>

<tr>
<td><small><?php echo "Old Password:";?></small></td>
<td><?php echo form_password('opassword');?></td>

</tr>
<tr>
<td><small><?php echo "New Password:";?></small></td>
<td><?php echo form_password('npassword');?></td>

</tr>
<tr>
<td><small><?php echo "Confirm Password:";?></small></td>
<td><?php echo form_password('cpassword');?></td>
</tr>
</tbody>
</table>
&nbsp;&nbsp;<div id="some"style="position:relative;"><button type="submit" class="btn btn-primary"><i class=" icon-ok-sign icon-white"></i>&nbsp;Submit</button>

<?php

echo form_close();

?>
</div>

2.Now make a controller to check your password if it is correct or not.Name it main.php.place it in folder 'controller'.add this content.


<?php

class Main extends CI_Controller {

public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->library('form_validation');

}

    public function changepwd()

{

    $this->load->library('form_validation');
    $this->form_validation->set_rules('opassword','Old Password','required|trim|xss_clean|callback_change');
    $this->form_validation->set_rules('npassword','New Password','required|trim');
    $this->form_validation->set_rules('cpassword','Confirm Password','required|trim|matches[npassword]');

    if ($this->form_validation->run() == FALSE)

    {
         
  echo validation_errors();


    }

 
    }


     public function change() // we will load models here to check with database

 
{


     $session_data = $this->session->userdata('logged_in');

     $query=$this->db->query("select * from user where id=".$session_data['id']);

     foreach ($query->result() as $my_info) {

     $db_password = $my_info->password;

     $db_id = $my_info->id;


     }

     if ((md5($this->input->post('opassword',$db_password)) == $db_password) && ($this->input->post('npassword') != '') && ($this->input->post('cpassword')!='')) {

$fixed_pw = md5($this->input->post('npassword'));

     $update = $this->db->query("Update user SET password='$fixed_pw' WHERE id='$db_id'")or die(mysql_error());

     $this->form_validation->set_message('change','<div class="alert alert-success"><a href="#" class="close" data-dismiss="alert">&times;</a>
<strong>Password Updated!</strong></div>');
return false;
   

     }
   else  {
$this->form_validation->set_message('change','<div class="alert alert-error"><a href="#" class="close" data-dismiss="alert">&times;</a>
  <strong>Wrong Old Password!</strong> </div>');

return false;

}
}
}
?>

That's it. now run your view file(Changepassword.php) using an another function.
Please comment if this helpful.Goodluck

Tuesday, 8 October 2013

How to fetch data from two tables in mysql (Codeignitor)?

How to fetch data from two tables in mysql (Codeignitor)?








$query = $this ->db->query("SELECT * FROM Table-1 WHERE id = (SELECT qid FROM Table-2  where post_status ='active' AND post_type= 'quick') ");
return $query->result_array();

Please like n share if this is helpful !!!