Skip to main content

Posts

Showing posts from 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-

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');

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 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 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

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, 'passw

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

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 = ar

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->dbfo