Double form pada tutorial codeigniter pemula bagian memasukkan data

Saya bingung kenapa bisa jadi double form seperti diatas. Padahal saya udah cek berulang kali dan kode nya sama dengan yang ada di tutorial.

Mohon bantuannya ya...

avatar faishal9seven
@faishal9seven

18 Kontribusi 4 Poin

Diperbarui 7 tahun yang lalu

3 Jawaban:

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

    $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'),
      'text' => $this->input->post('text'),
      'slug' => $slug
    );

    return $this->db->insert('news', $data);
  }

}

News.php

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

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'] = 'Arsip berita';

		$this->load->view('news/index', $data);
	}

	public function view($slug = NULL)
	{
    $data['news_item'] = $this->news_model->get_news($slug);
		$this->load->view('news/view', $data);
	}

  public function create()
  {
    $this->load->helper('form');
    $this->load->library('form_validation');

    $this->form_validation->set_rules('title', 'Judul', 'required');
    $this->form_validation->set_rules('text', 'Text', 'required');

    if ($this->form_validation->run() === FALSE) {
      $this->load->view('news/create');
    }else {
      $this->news_model->set_news();
      redirect('news');
    }
    $this->load->view('news/create');
  }

}

create.php

<?php echo validation_errors(); ?>

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

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

  <label for="text">Text</label>
  <textarea name="text" rows="8" cols="40"></textarea> <br>

  <input type="submit" name="submit" value="Kirim!">

<?php echo form_close(); ?>

routes.php

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

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

$route['default_controller'] = 'halaman/view';
$route['(:any)'] = 'halaman/view/$1';

$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

Mohon bantuannya ya ^_^

avatar faishal9seven
@faishal9seven

18 Kontribusi 4 Poin

Dipost 7 tahun yang lalu

Oh.. seharusnya baris code ini

$this->load->view('news/create');

Di letakkan di dalam "else" ya?

avatar faishal9seven
@faishal9seven

18 Kontribusi 4 Poin

Dipost 7 tahun yang lalu

Ok mas, terima kasih banyak atas bantuan dan info untuk perubahan kode nya :)

avatar faishal9seven
@faishal9seven

18 Kontribusi 4 Poin

Dipost 7 tahun yang lalu

Login untuk ikut Jawaban