21 January 2021

How to use constructor in Codeigniter?

 Use constructor in Codeigniter


class Welcome extends CI_Controller {


/**

* Index Page for this controller.

*

* Maps to the following URL

* http://example.com/index.php/welcome

* - or -

* http://example.com/index.php/welcome/index

* - or -

* Since this controller is set as the default controller in

* config/routes.php, it's displayed at http://example.com/

*

* So any other public methods not prefixed with an underscore will

* map to /index.php/welcome/<method_name>

* @see https://codeigniter.com/user_guide/general/urls.html

*/

 public function __construct()

    {

        parent::__construct();

        $this->load->library('session');

        $this->load->helper('html');

        $this->load->helper('url');

        $this->load->helper('form');

        $this->load->helper('file');

        $this->load->library('form_validation');

        $this->load->model('Customer_model');

    }

}

How to use form Validation in Codeigniter?

  Use form Validation in Codeigniter

Paste in Codeigniter

if($this->input->post('login')){

$email=$this->input->post('login_email');

$password=$this->input->post('login_password');

        $this->load->library('form_validation');

$this->form_validation->set_rules('login_email', 'Email', 'required|valid_email'); 

                $this->form_validation->set_rules('login_password', 'Password', 'required|min_length[10]'); 

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

//redirect('dashboard');

//echo 'not-working';

            } 

            else { 

$query = $this->Customer_model->login_detail($email,$password);

if(count($query)>0){

//session_start();

$this->session->set_userdata('ID',$query[0]['ID']);

redirect('dashboard', 'refresh');

}

else{

redirect('register'); 

}  

}


How to create (Use) SESSION in Codeigniter?

 Create (Use) SESSION in Codeigniter

Use in Controller

         $this->load->library('session');

$id=5;

        $this->session->set_userdata('ID',$id);

        redirect('dashboard', 'refresh');



        if ($this->session->userdata('ID') == TRUE)

        {

            //do something

redirect('dashboard');

}

else{

//echo 'hello<br>';

}

How to get values from form in Codeigniter?

Get values from form in Codeigniter

 if ($this->input->post('submit')==true) {
      $data['username']=$this->input->post('username'); // $_POST['username']
      $data['phone']=$this->input->get('phone'); // $_GET['phone']
    
 }

How to definite (Add) pages in Codeigniter?

Definite (Add) pages in Codeigniter

 //$route['page_name'] = 'controller_name/controller_function';

//D:\wamp\www\codelearning\application\config

 $route['dashboard'] = 'welcome/dashboard';

$route['update'] = 'welcome/update_user';

$route['ajax'] = 'welcome/ajax';

$route['upload'] = 'welcome/file_upload';

$route['register'] = 'welcome/register_form';

$route['update/(:any)'] = 'welcome/update_user';

How to get value from URL in Codeigniter?

Get value from URL in Codeigniter


Paste in Controller

$user_id = $this->uri->segment(2);

//$user_id = $this->uri->segment(position_after_index.php);


Paste in View


<?php echo base_url('index.php/update/'.$row['ID']); ?>

//http://localhost/codelearning/index.php/update/26


Paste in Routes


$route['update/(:any)'] = 'welcome/update_user';

$route['page_name/(:any)'] = 'conroller_name/controller_function';

How to call Model function in Controller in Codeigniter?

Call Model function in Controller in Codeigniter


 $query = $this->Customer_model->register_user($person,$phone,$email,$password);


// $query = $this->model_name->model_function($function_perameter,$phone,$email,$password);

How to create functions in model in Codeigniter?

 Create functions in the model in Codeigniter


 Delete, Fetch, Insert and Update functions in Codeigniter


<?php 
class Customer_model extends CI_Model {
 
    /**
    * Responsable for auto load the database
    * @return void
    */
    public function __construct()
    {
        $this->load->database();
        $this->load->helper('url');
    }

    /**
    * Get product by his is
    * @param int $product_id 
    * @return array
    */

   

   public function get_login_data()
    {
$this->db->select('*');
$this->db->from('login_form');
$query = $this->db->get();
return $query->result_array(); 
    }

public function login_detail($email,$password)
    {
$this->db->select('*');
$this->db->from('login_form');
$this->db->where('email',$email);
$this->db->where('password',$password);
$query = $this->db->get();
return $query->result_array(); 
    }
public function get_single_user_detail($user_id)
    {
$this->db->select('*');
$this->db->from('login_form');
$this->db->where('ID',$user_id);
$query = $this->db->get();
return $query->result_array(); 
    }
function register_user($name,$phone,$email,$password){

$data = array(
'name' => $name,
'phone' => $phone,
'email ' => $email,
'password ' => $password
);
$this->db->insert('login_form', $data);
}
function delete_user($user_id){
$this -> db -> where('ID', $user_id);
$this -> db -> delete('login_form');
}


function update_user($user_id,$name,$phone,$email,$password){
$data = array(
'name' => $name,
'phone' => $phone,
'email ' => $email,
'password ' => $password
);
$this->db->update('login_form', $data);
$this -> db -> where('ID', $user_id);
}
}


How to connect database in Codeigniter?

 Connect database in Codeigniter


$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost', //add server
'username' => 'root', //username
'password' => '',
'database' => 'codelearning', //database name
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);

How to link CSS and Javascript File in Codeigniter?

 Link CSS and Javascript File in Codeigniter


Paste in view

<link rel="stylesheet" href="<?php echo base_url(); ?>assets/css/style.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script  src="<?php echo base_url(); ?>assets/js/custom.js" ></script>


Definite base URL in config.php file (D:\wamp\www\codelearning\application\config)

$config['base_url'] = 'http://localhost/codelearning/';


How to user logout (destroy session) in codeigniter?

 User logout (destroy session) in Codeigniter


Paste in Controller


public function logout(){
                $this->load->library('session');
$this->session->unset_userdata('ID');
$this->session->sess_destroy();
redirect('register');
}


Paste in View

<div class="site_header"><h2><a href="<?php echo base_url('index.php/Welcome/logout'); ?>">Logout</a></h2></div>

Note:- Welcome is a controller name

How to use ajax properly in Codeigniter?

Use ajax properly in Codeigniter


Paste In view


<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script  src="<?php echo base_url(); ?>assets/js/custom.js" ></script>
<script>
$(document).ready(function(){
$(".ajax_bt").click(function(e) {
e.preventDefault();

    var person = $('#person').val();
    var phone = $('#phone').val();
    var email = $('#email').val();
    var password = $('#password').val();
//alert(person+''+''+phone+''+email+''+password);

  if(person != '' && phone != '' && email != '' && password != ''){
  
      $.ajax({
        url: "<?php echo base_url('index.php/Welcome/insert_data_with_ajax'); ?>",
        data: {
'person':person,
'phone':phone,
'email':email,
'password':password
}, // change this to send js object
        type: "post",
        success: function(data){
           //document.write(data); just do not use document.write
           console.log(data);
   $('#ajax_response').html(data);
   
        },
        error:function(data){
           //document.write(data); just do not use document.write
           console.log(data);
   $('#ajax_response').html('<p style="color:Red; margin:0px">Data not inserted...</p>');
   
        }
      });
       
    }
else{
$('#ajax_response').html('<p style="color:Red; margin:0px">Please fill all fields...</p>');
}


});
});
</script>



Paste In Controller


class Welcome extends CI_Controller {

 public function __construct()
    {
        parent::__construct();
        
        $this->load->library('session');
        $this->load->helper('html');
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->helper('file');
        $this->load->library('form_validation');
      
        $this->load->model('Customer_model');

       
    }


public function insert_data_with_ajax() {
$person = $this->input->post('person');
$phone = $this->input->post('phone');
$email = $this->input->post('email');
$password = $this->input->post('password');
$query = $this->Customer_model->register_user($person,$phone,$email,$password);
if($this->db->affected_rows() > 0){
 
echo 'Data inserted successfully';
 
}
else{  
echo 'Data Not inserted';
die();
}

public function ajax()
{
if ($this->session->userdata('ID') == FALSE)
        {
            
redirect('register'); //if session is not there, redirect to login page
        }
$data['main_content']='ajax';
$this->load->view('template',$data);  
}
 
}

Paste In Model

class Customer_model extends CI_Model {

    public function __construct()
    {
        $this->load->database();
        $this->load->helper('url');
    }


function update_user($user_id,$name,$phone,$email,$password){
$data = array(
'name' => $name,
'phone' => $phone,
'email ' => $email,
'password ' => $password
);
$this->db->update('login_form', $data);
$this -> db -> where('ID', $user_id);
}

}

20 January 2021

How to use ajax in Codeigniter?

 Use ajax in Codeigniter


Paste in View

<script>
$(document).ready(function(){
$(".ajax_bt").click(function(e) {
e.preventDefault();  
    var person = $('#person').val();
    var phone = $('#phone').val();
    var email = $('#email').val();
    var password = $('#password').val();
//alert(person+''+''+phone+''+email+''+password);
    $.ajax({
        url: "<?php echo base_url('index.php/Welcome/insert_data_with_ajax'); ?>",
        data: {
'person':person,
'phone':phone,
'email':email,
'password':password
}, // change this to send js object
        type: "post",
        success: function(data){
           //document.write(data); just do not use document.write
           console.log(data);
        }
      });
});
});
</script>

Paste in Controller



class Welcome extends CI_Controller {

 public function __construct()
    {
        parent::__construct();
        
        $this->load->library('session');
        $this->load->helper('html');
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->helper('file');
        $this->load->library('form_validation');
      
        $this->load->model('Customer_model');

       
    }
 

public function insert_data_with_ajax() {
echo $person = $this->input->post('person');
echo 'jassi';
//$data['main_content']='ajax';
//$this->load->view('template',$data);
}


}

How to upload file in codeigniter?

Upload file in Codeigniter?


Paste this code in controller

if($this->input->post('upload_file'))

{

$config['upload_path']          = 'uploads';

                $config['allowed_types']        = 'gif|jpg|png';

               // $config['max_size']             = 100;

               // $config['max_width']            = 1024;

               // $config['max_height']           = 768;


                $this->load->library('upload', $config);

// $upload_data = $this->upload->data('file_name'); 

                if ( ! $this->upload->do_upload('files'))

                {

                        $error = array('error' => $this->upload->display_errors());


                        //$this->load->view('upload', $error);

echo 'FIle Error';

                }

                else

                {

                         $data = array('upload_data' => $this->upload->data());


                       // $this->load->view('upload', $data);

echo 'FIle Uploaded Successfully';

$upload_name = $this->upload->data('file_name');

                } 

}

Paste this code in View

<div class="user_update_form">

<form method="post" action="" enctype="multipart/form-data" />

<?php echo validation_errors(); ?>  

<?php //echo $error; ?>  

<?php echo form_open_multipart('upload/do_upload');?>

<h1 class="register_lable">Upload Files</h1>

<label>Files</label>

<input type="file" class="input_field" name="files" value="">

<input type="submit" name="upload_file" class="upload_file" value="Update">


</form>

</div>

19 January 2021

How to delete data from database in MySql in codeigniter?

Delete data from database in MySql in codeigniter 


if($this->input->post('delete_user'))

{

$user_id = $this->input->post('user_id');

    $this -> db -> where('ID', $user_id);

$this -> db -> delete('login_form');

redirect(dashboard);

}

How to fetch data from Database in Codeigniter?

Fetch data from Database in Codeigniter



$query = $this->db->get('login_form');
foreach ($query->result() as $row)
{
        echo $row->ID.'<br>';
        echo $row->name.'<br>';
        echo $row->phone.'<br>';
        echo $row->email.'<br>';
        echo $row->password.'<br><hr>';
}

How to insert data into database in MySql in Codeigniter?

 Insert data into the database in MySql in Codeigniter


if($this->input->post('register')){
$name=$this->input->post('person');
$phone=$this->input->post('phone');
$email=$this->input->post('email');
$password=$this->input->post('password');
$data = array(
        'name' => $name,
        'phone' => $phone,
        'email ' => $email,
        'password ' => $password
);
$this->db->insert('login_form', $data);
if($this->db->affected_rows() > 0){
 
echo 'Data inserted successfull';
 
}else{
 
  echo 'Data Not inserted';
}
}

How to Create table into Database in MySQL in Codeigniter?

Create table into Database in MySQL in Codeigniter



if ($this->db->table_exists('members'))
{
  // table exists some code run query
}
else{
 

   
$this->load->dbforge();

// define table fields
$fields = array(
  'memid' => array(
    'type' => 'INT',
    'constraint' => 9,
    'unsigned' => TRUE,
    'auto_increment' => TRUE
  ),
  'firstname' => array(
    'type' => 'VARCHAR',
    'constraint' => 30
  ),
  'lastname' => array(
    'type' => 'VARCHAR',
    'constraint' => 30
  ),
  'email' => array(
    'type' => 'VARCHAR',
    'constraint' => 60,
    'unique' => TRUE
  ),
  'password' => array(
    'type' => 'VARCHAR',
    'constraint' => 40
  )
 );

$this->dbforge->add_field($fields);

// define primary key
$this->dbforge->add_key('memid', TRUE);

// create table
$this->dbforge->create_table('Members');

}

How to add re-captcha v3 on all Elementor forms using coding?

 Add re-captcha v3 on all Elementor forms using coding add_action('wp_footer',function(){     ?> <script src="https://www...