Learn CodeIgniter

What you should do and learn

  • Download latest version of codeigniter
  • Set config/autoload.php to automatically load important and common helpers/library etc
    • helper – form, url
    • library – database
  • Good to start following tutorial under “General Topics” and “Tutorial”
  • Create a new controller (can copy from sample controller named welcome). Edit the class name, change the view to call. Class name should start with capital letter.
  • Create a new view and call from the controller
  • Change the config/routes.php to set as default controller
  • Learn how to pass data from controller to view
  • This is the way to load view, controller, helper etc
    • $this->load->view(‘aname’);
    • $this->load->helper(‘aname’);
    • $this->load->model(‘aname’);
  • Create a form in the view
    • use form_open(‘controller/function_name’) and form_close() function
  • Try pass a value via the form
    • to get data in controller use $arecord = $this->input->post(‘fieldname’);

DB Activities

  • set up DB config in config/database.php
  • set config/autoload.php to auto load database library
  • learn to use standard query and active record to insert/retrieve data from database
    • standard query – $query = $this->db->query(‘SELECT * FROM my_table’);
    • active record
      • $this->db->select(‘field1, field2’);
      • $query = $this->db->get(‘my_table’);  //use this to get all records and fields in a table.
  • retrieve the data result
  • controller to get result from model,
    • $this->load->model(‘model_name’,’avar’); //avar is variable to the model
    • function (in controller) to get the result by calling the function in model – $result = $this->avar->function_name();
    • function in model must do the query and return the result (to be retrieved by other functions from controller)
  • controller to pass the result to view as usual $this->load->view(‘view_name’, $data);
  • to loop the result set in view using foreach ($result as $list): echo $list[‘field_name’]
  • more with active record – complete CRUD and more
  • //save data to db (must load db class)
  • $data = array(
    ‘title’ => ‘My title’ ,
    ‘name’ => ‘My Name’ ,
    ‘date’ => ‘My date’
    $this->db->insert(‘mytable’, $data);

Codeigniter user guide

Leave a Reply

Your email address will not be published. Required fields are marked *