All pastes #914699 Raw Edit

Stuff

public text v1 · immutable
#914699 ·published 2008-02-23 02:56 UTC
rendered paste body
<?php

class Blog extends Controller {

	function Blog()
	{
		parent::Controller();
		
		//Scaffolding should be enabled for development only
		$this->load->scaffolding('blog_entries');
	}
	
	function index()
	{
		// We don't want a short URL
		$this->home();
		exit;
	}
	
	function home($page = null)
	{
		// Load essential classes
		$this->load->database();
		
		// If there is no page defined, get the first 10 results from the database
		if ($page == null)
		{
			// This query gets the wanted information
			$entries = $this->db->query('SELECT b.*, u.id as userid, u.username FROM blog_entries as b INNER JOIN blog_users as u ON b.author = u.id ORDER BY date LIMIT 10');
			
			// The useful information is loaded into an array called $info
			$data["entries"] = $entries->result_array();
			
			// Load the home view, and hand over the information
			$this->load->view('blog_home', $data);
		}
	}
	
	function view($id)
	{
		if ($id == null || !is_int($id))
		{
			// No id, dump to home
			$this->home();
			exit;
		}
		
		// id is set, so load the needed classes
		$this->load->database();
		
		// Check to see if the id exists
		$entry = $this->db->query("SELECT b.*, u.id as userid, u.username FROM blog_entries as b INNER JOIN blog_users as u ON b.author = userid WHERE b.id = ".$id." LIMIT 1");
		
		// Don't continue if there isn't a result
		if ($entry->num_rows() > 0)
		{
			// There's a result, prepare information for the view
			$data["entry"] = $entry->row_array();
			
			// Go to the view
			$this->load->view('blog_view', $data);
		}
		else
		{
			// No result, dump to home
			$this->home();
			exit;
		}
	}
}
?>