Welcome Guest, Not a member yet? Register   Sign In
tutorial code modification
#1

[eluser]Unknown[/eluser]
Hello.
I am new to codeigniter so I started to do the user guide tutorial. All went well until the 'news_section' part, where I had to modify a link in the '/views/news/index.php' to get it working:
Code:
<?php foreach ($news as  $news_item): ?>

    <h2>&lt;?php echo $news_item['title'] ?&gt;</h2>
    <div class="main">
        &lt;?php echo $news_item['text'] ?&gt;
    </div>
    <p><a href="news/&lt;?php echo $news_item['slug'] ?&gt;">View article</a></p>
&lt;?php endforeach ?&gt;
I had to delete the 'news/' from the link because the script was generating 'news/slug' so I ended with a wrong link that looks like that 'http://localhost/ci/index.php/news/news/slug'. I would like to know if I did something wrong and the correct output from the script would be just 'slug' .
Here is the rest of the code:
Model:
Code:
&lt;?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:
Code:
&lt;?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');
}
//the below code is from another tutorial---------------------------------------------------------------------------------------
        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/succes');
}
    }
}
I don't seem to be getting any replies, if there is something wrong with my question please tell me Smile.




Theme © iAndrew 2016 - Forum software by © MyBB