User Tools

Site Tools


codeigniter

This is an old revision of the document!


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();
	}
}
codeigniter.1274920723.txt.gz · Last modified: 2010/05/26 19:38 by rfile