Jump to content

CodeIgniter - Tutorial Trouble


ReeceSayer

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

Link to comment
Share on other sites

  • 4 weeks later...

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.