ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Newbie Frustration

November 12, 2012 6:32pm

Subscribe [1]
  • #16 / Nov 14, 2012 7:27am

    mikeklon

    8 posts

    Thank you for your replies Aken and PhilTem.

    I’ve proceeded to the next page of the tutorial.  I see the line in the news_model.php for setting $slug.  As for it’s purpose, I assumed it was a search field, but perhaps it’s not (there are other ways to access and keep track of a table record) .  Perhaps this will become clear later on (perhaps not).

    Anyway, the “next page of the tutorial” has introduced a new error for me.  When I access the news/create view page, the form is displayed, and I’m able to enter information into the two fields. 

    OK - never mind.  I found the mistake (which of course I introduced).  Sorry for the erroneous post.


    view/news/create.php:

    <h2>Create a news item</h2>
    <p><?php echo validation_errors(); ?></p>
    
    <p><?php echo form_open('news/create') ?></p>
    
    <p> <label for="title">Title</label><br />
     <input type="input" name="title" /> </p>
    
    <p> <br />
     <label for="text">Text</label><br />
     <textarea name="text"></textarea> </p>
    
    <p> <br />
     <input type="submit" name="submit" value="Create news item" /><br />
     <br />
    </form>

    controllers/news.php:

    <?php
    class News extends CI_Controller {
     
     public function __construct()
      {
      parent::__construct();
      $this->load->model('news_model');
      } // end function __construct()
      
     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');
      } // end function index()
      
     public function view($slug)
      {
      $data['news'] = $this->news_model->get_news($slug);
      
      if (empty($data['news']))
      {
       show_404();
       } // end if
      
      $data['title'] = $data['news']['title'];
      
      $this->load->view('templates/header', $data);
      $this->load->view('news/view', $data);
      $this->load->view('templates/footer');
      } // end function view()
      
     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');
       }
      
      
     } //end function create()
      
    } // end class News

    models/news_model.php:

    <?php
    
    class News_model extends CI_Model {
     public function __construct()
      {
       $this->load->database(); 
      } //end function __construct()
     
     public function get_news($slug = FALSE)
      {
       if ($slug === FALSE)
        {
        $query = $this->db->get('news');
        return $query->result_array();
       } //end if
     
      $query = $this->db->get_where('news', array('slug' => $slug));
      return $query->row_array();
      
      } // end function get_news()
     
     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);
      
      } // end function set_news()
     
    } // end class News_model

    config/routes:

    $route['news/create'] = 'news/create';
    $route['news/(:any)'] = 'news/view/$1';
    $route['news'] = 'news';
    $route['(:any)'] = 'pages/view/$1';
    $route['default_controller'] = 'pages/view';
    $route['404_override'] = 'errors/page_missing';


    Thank you again for any guidance.

  • #17 / Nov 14, 2012 7:55am

    mikeklon

    8 posts

    Well, that was a bit more painful than I was expecting, however the “news” tutorial is complete.

    I’m hoping there are other example and/or tutorial sources. 

    I read manuals and documentation, but I’m a learner by doing and seeing examples.

    Any other CI tutorials out there?  Can someone point me in that direction please. 

    Thanks again for your help for a CI newbie.

  • #18 / Nov 14, 2012 8:11am

    Narf

    155 posts

    Googling for CodeIgniter tutorial should give you enough tutorials to start with.

    The downside is that most of them have been made for CodeIgniter 1.7.x and have not been updated since. You should ignore anything related to ‘scaffolding’, which used to be something like a mini version of phpMyAdmin but is now obsolete.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases