Jump to content

Archived

This topic is now archived and is closed to further replies.

ReeceSayer

CodeIgniter - Tutorial Trouble

Recommended Posts

My first time using CI and i've run into a snag at the second hurdle (even though it seems quite common from my online searches)

 

I'm completing the second tutorial from the user guide (http://codeigniter.com/user_guide/tutorial/news_section.html)

 

The issue i'm having is similar to that i've seen posted on the CI forums but i'm not seeing any actual response on there.

 

Model (news_model.php):

<?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();
  }
   
  //this seems to be causing an issue
  $query = $this->db->get_where('news', array('slug' => $slug));
  return $query->row_array();
}

}

 

Controller (news.php):

<?php
class News extends CI_Controller {

  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');
}
} 

 

I commented in the model where i think is causing an issue but i'm not sure why. Obviously i'm getting thrown into the 404 error because the data news item is empty but it shouldn't be.

 

The initial index page works and my routes are all defined as they are in the tutorial. I previously replaced the  show_404() with an exit('this is the error'); and it then shows this error.

 

Also, my base url is defined as this: http://mysite/projects/framework which is where the CI installation is.

 

Apologies if this has been answered before but when searching the forum i get an "Unable to access the search daemon" error.

 

Cheers

Share this post


Link to post
Share on other sites

I had the same trouble with the tutorial and got stuck on the same new just like you. Although you have decided to move on but just in case you haven't gotten a solution pass the tutorials this is how mine was resolved.

 

Model: the news_model.php file

 

<?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);
}
}

 

 

Controller: news.php

 

<?php
class News extends CI_Controller {

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');
}
}

}

 

 

Hope you find it useful.

 

Bye

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.