[eluser]Atown[/eluser]
So 3 days latter, 4 attempts at the tutorial, and 2 pages of forum posting searches and I'm finally registered and posting for help. I've used Wordpress and Drupal for a long time but I'm really trying to learn more how this kind of stuff works and operates.
So here is the url:
http://loadersequence.com/ci/index.php/news
routes:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------------
| URI ROUTING
| -------------------------------------------------------------------------
| This file lets you re-map URI requests to specific controller functions.
|
| Typically there is a one-to-one relationship between a URL string
| and its corresponding controller class/method. The segments in a
| URL normally follow this pattern:
|
| example.com/class/method/id/
|
| In some instances, however, you may want to remap this relationship
| so that a different class/function is called than the one
| corresponding to the URL.
|
| Please see the user guide for complete details:
|
| http://ellislab.com/codeigniter/user-guide/general/routing.html
|
| -------------------------------------------------------------------------
| RESERVED ROUTES
| -------------------------------------------------------------------------
|
| There area two reserved routes:
|
| $route['default_controller'] = 'welcome';
|
| This route indicates which controller class should be loaded if the
| URI contains no data. In the above example, the "welcome" class
| would be loaded.
|
| $route['404_override'] = 'errors/page_missing';
|
| This route will tell the Router what URI segments to use if those provided
| in the URL cannot be matched to a valid route.
|
*/
$route['default_controller'] = 'pages/view';
$route['404_override'] = 'errors/page_missing';
$route['news/create'] = '/news/create';
$route['news/(:any)'] = '/news/view/$1';
$route['news'] = '/news';
/*
scaffolding is just a temp term and can be named anything, its just a place holder
defining what we're doing
$route['default_controller'] = "pages";
*/
/* End of file routes.php */
/* Location: ./application/config/routes.php */
model:
Code:
<?php
class News_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
}
public function get_news($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}
public function set_news()
{
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'text' => $this->input->post('text')
);
return $this->db->insert('news', $data);
}
Code:
<?php
class News extends CI_Controller {
public function view($page = 'index')
{
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
show_404();
}
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
}
public function index()
{
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');
}
public function view($slug)
{
$data['news_item'] = $this->news_model->get_news($slug);
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = $data['news_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
public function create()
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Create a news item';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'text', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('news/create');
$this->load->view('templates/footer');
}
else
{
$this->news_model->set_news();
$this->load->view('news/success');
}
}
}
View
Code:
<?php
echo '<h2>'.$news_item['title'].'</h2>';
echo $news_item['text'];
I'm extremely grateful for the help and really just kind of frustrated because I know I'm not that ignorant and everyone keeps saying these tutorials and userguide are awesome but I've been struggling with CI for a couple weeks and made it through most things but just this basic blog thing has gotten me stumped.