Welcome Guest, Not a member yet? Register   Sign In
Simple form to save data to db
#3

Model:
PHP Code:
<?php 
class Test_model extends CI_Model {

  private 
$table 'test_table';

  public function 
get_record($id)
  {
     
$query $this->db->from($this->table)->where('id',$id)->get();
    if ( 
$query->num_rows() != ) {
       return 
$query->row_array();
    }
    else {
       return 
FALSE;
    }
  }

  public function 
save_record($id,$data)
  {
     if (
$id 0) {
      
$this->db->insert($this->table,$data);
    }
    else {
      
$this->db->update($this->table,$data,'id=' $id);
    }
  }


Controller:
PHP Code:
<?php
class Testcontroller extends CI_Controller {

  public function 
__construct() 
  {
     
parent::__construct();
     
$this->load->model('test_model');
  }

  public function 
show_testform() {
    
$this->load->helper('form');
    
$this->load->library('form_validation');
    
$this->form_validation->set_rules('username','User name','required');
    
$this->form_validation->set_rules('country','Country','required');
    
$this->form_validation->set_rules('description','Description','required');
    
$this->form_validation->set_rules('has_passport');
    if (
$this->form_validation->run() == FALSE) {
      
$data['countries'] = array(
         
'' => '(select a country)',
         
'USA' => 'USA',
         
'UK' => 'UK'
      
);
      
$this->load->view('test_form',$data);
    }
    else {
       
$id 0;
       
$data = array(
         
'username'=>$this->input->post('username'),
         
'country' => $this->input->post('country'),
         
'has_passport' => isset($this->input->post('has_passport')),
         
'description' => $this->input->post('description')
       );
       
$this->test_model->save_record($id,$data);
  }


View:
PHP Code:
<?php
echo form_open();
echo 
'User name: ' .  form_input('username',NULL) . '<br />';
echo 
'Country: ' form_dropdown('country',$countries,NULL) . '<br />';
echo 
'Has passport: ' form_checkbox('has_passport',1FALSE) . '<br />';
echo 
'Description:<br />';
echo 
form_textearea( array('rows'=>8'cols' => 60'name' =>'description') . '<br />';
echo 
form_submit('submit','Submit form');
echo 
form_close(); 

That's a basic setup based on MVC in CI.
Reply


Messages In This Thread
Simple form to save data to db - by meOmy - 04-30-2016, 06:29 AM
RE: Simple form to save data to db - by InsiteFX - 04-30-2016, 10:19 AM
RE: Simple form to save data to db - by Wouter60 - 04-30-2016, 11:10 AM
RE: Simple form to save data to db - by meOmy - 04-30-2016, 04:46 PM
RE: Simple form to save data to db - by Code4fun - 04-30-2016, 04:06 PM
RE: Simple form to save data to db - by meOmy - 05-06-2016, 05:04 AM
RE: Simple form to save data to db - by Code4fun - 05-06-2016, 05:29 PM
RE: Simple form to save data to db - by meOmy - 05-06-2016, 05:31 PM
RE: Simple form to save data to db - by meOmy - 05-09-2016, 06:04 AM
RE: Simple form to save data to db - by Code4fun - 05-09-2016, 06:37 PM
RE: Simple form to save data to db - by Wouter60 - 04-30-2016, 11:39 PM



Theme © iAndrew 2016 - Forum software by © MyBB