Welcome Guest, Not a member yet? Register   Sign In
Basic Tutorial Help
#6

(This post was last modified: 12-31-2016, 06:13 PM by in2tech.)

News Section tutorial I am working on. Can display news items manually input into the database, but can not add items through the Create form:

News.php

Code:
<?php
class News extends CI_Controller {

       public function __construct()
       {
               parent::__construct();
               $this->load->model('news_model');
               $this->load->helper('url_helper');
       }      

       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 = NULL)
       {
               $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');
       }
       }
     // Original bracket below
 
}

News_model.php

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

// Original bracket below

}

create.php (in the views/news folder)

Code:
<h2><?php echo $title; ?></h2>

<?php echo validation_errors(); ?>

<?php echo form_open('news/create'); ?>

   <label for="title">Title</label>
   <input type="input" name="title" /><br />

   <label for="text">Text</label>
   <textarea name="text"></textarea><br />

   <input type="submit" name="submit" value="Create news item" />

</form>

routes.php (changes from tutorial)

Code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

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

All I get is a blank page when trying to insert a new record. I can manually add news items to the database through phpMyAdmin but not through the Create form!

Here is the error as it is not connecting to the database:

http://www.screencast.com/t/8NFaonzVBvW

Thanks for your help!

Update: It was the config base_url, and now is inserting data into the database via the create.php file, but the success message has an error I need to try and fix. I kind of figured it was something simple. Now to see if I can fix the new error.

When or if I figure this one out, I'll let you know.

Success Error:

http://www.screencast.com/t/VASIK9juls

All done. It helps if you include the success.php file. Everything working now. It's the little obvious things that always get me, having the wrong database name in the database.php file, not realizing the future code in the tutorial goes inside of the controller or model code, NOT below it. Not having the correct base_url in the config file, and not putting the success.php file in the views/news/ folder. Now to style it and see if I can come up with some changes to make my own.

Thanks and good luck everyone new to PHP and CodeIgniter like I am.
Reply


Messages In This Thread
Basic Tutorial Help - by in2tech - 12-27-2016, 11:59 PM
RE: Basic Tutorial Help - by InsiteFX - 12-28-2016, 05:52 AM
RE: Basic Tutorial Help - by in2tech - 12-28-2016, 10:49 AM
RE: Basic Tutorial Help - by InsiteFX - 12-29-2016, 04:37 AM
RE: Basic Tutorial Help - by in2tech - 12-30-2016, 12:23 AM
RE: Basic Tutorial Help - by in2tech - 12-31-2016, 01:45 PM
RE: Basic Tutorial Help - by InsiteFX - 01-01-2017, 03:23 AM



Theme © iAndrew 2016 - Forum software by © MyBB