User Tools

Site Tools


codeigniter

Code Igniter Notes

Been looking at nettuts video tutorials and I am going to save some code snippets here so I can pick them up at work.

tutorial list

tutorial 5

controller from lesson 5 CRUD

<?php
 
class Site extends Controller 
{
	function index()
	{
		$data = array();
 
		if($query = $this->site_model->get_records())
		{
			$data['records'] = $query;
		}
 
		$this->load->view('options_view', $data);
	}
 
	function create()
	{
		$data = array(
			'title' => $this->input->post('title'),
			'content' => $this->input->post('content')
		);
 
		$this->site_model->add_record($data);
		$this->index();
	}
 
	function update()
	{
		$data = array(
			'title' => 'My Freshly UPDATED Title',
			'content' => 'Content should go here; it is updated.'	
		);
 
		$this->site_model->update_record($data);
	}
 
 
	function delete()
	{
		$this->site_model->delete_row();
		$this->index();
	}
}

site model

<?php
 
class Site_model extends Model {
 
	function get_records()
	{
		$query = $this->db->get('data');
		return $query->result();
	}
 
	function add_record($data) 
	{
		$this->db->insert('data', $data);
		return;
	}
 
	function update_record($data) 
	{
		$this->db->where('id', 12);
		$this->db->update('data', $data);
	}
 
	function delete_row()
	{
		$this->db->where('id', $this->uri->segment(3));
		$this->db->delete('data');
	}
 
}

view

<?php
 
class Site extends Controller 
{
	function index()
	{
		$data = array();
 
		if($query = $this->site_model->get_records())
		{
			$data['records'] = $query;
		}
 
		$this->load->view('options_view', $data);
	}
 
	function create()
	{
		$data = array(
			'title' => $this->input->post('title'),
			'content' => $this->input->post('content')
		);
 
		$this->site_model->add_record($data);
		$this->index();
	}
 
	function update()
	{
		$data = array(
			'title' => 'My Freshly UPDATED Title',
			'content' => 'Content should go here; it is updated.'	
		);
 
		$this->site_model->update_record($data);
	}
 
 
	function delete()
	{
		$this->site_model->delete_row();
		$this->index();
	}
}

use of row_array()

  /**
     * Note the use of row_array to get
     * array of one row instead of object
     * this will matter later in the whole 
     * array that is passed to the view
     * @param string $wfield
     * the column we want to average
     * @param string $avgname
     * this is for the "as" clause
     * @param string $tablename
     * the name of the table the column is in
     * @return array
     *
     */
    function get_avg($wfield,$avgname,$tablename) {
        $this->db->select_avg($wfield,$avgname);
        $query = $this->db->get($tablename) ;
        $avgdata = $query->row_array() ;
        return $avgdata ;
    }
 
    }

second database

<?php
class Model_old_info extends CI_Controller {
 
       function __construct()
       {
            parent::__construct();
            // Your own constructor code
            $CI =& get_instance() ;
       $CI->oldbp = $this->load->database('oldbp', TRUE);
       $this->oldbp =& $CI->oldbp;
       }
       function get_old_records() {
 
 
       $table_name = 'old_blood_pressure' ;
       $oldbprec = $this->oldbp->get($table_name);
       foreach ($oldbprec->result()as $row) {
             $table_data[] = $row ;
          }
          return $table_data;
   }
       }

'oldbp' is in the codeigniter database config file

javascript - pass data to php and get json array back

been working on this for a while

$(function() {
$('#submit_item').click(function(){
var wpt = $('select#wpt_pick').val();
alert(wpt);
$.post("/coords/index.php/pickcoords/chosen", { chosen: $('select#wpt_pick').val() },
 
       function(data) {
        $("#lat").html(data[0].lat);
       $("#lon").html(data[0].lon);
        },"json")
});
 
});
codeigniter.txt · Last modified: 2011/03/07 21:09 by rfile