-->

Notification

×

Iklan

Iklan

Indeks Berita

Tag Terpopuler

CODE IGNITER LEVEL 3

Friday, August 3, 2012 | Friday, August 03, 2012 WIB Last Updated 2012-10-07T00:36:39Z
Assalamualaiu...
Malem nie gak tidur kayaknya...
banyak sekali tugas yang belum selesai....

S E M A N G A D.....
Mengapa judul artikel saya ini CODE IGNITER LEVEL 3...?!
itu karena ini adalah ke tiga kalinya saya belajar Code Igniter,,,

Kali ini saya akan share lanjutan artikel kemarin yang untuk menampilkan data dari database ke Code Igniter....
disini saya tambahkan sedikit bumbu CSS..hehehe

Meskipun masih ada yang kurang,,, Tombol Delete-nya belum berfungsi....hehehe

langsung sajalah....

Tampilannya seperti ini:



Dan untuk form input beritanya seperti ini:

Okeh...
Untuk struktur databasenya bisa di baca di artikel sebelumnya karena disini saya hanya fokuskan untuk input datanya aja....

Buat file view_news_input.php di Application/view/news/ :

<html>
<head>
<title>My Form</title>
</head>
<body bgcolor="#999900">

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

<h5>INPUT BERITA</h5><br/>

<h5>Title</h5>
<?php echo form_error('title'); ?>
<input type="text" name="title" value="<?php echo set_value('title'); ?>" size="50" />

<h5>Content</h5>
<?php echo form_error('content'); ?>
<textarea name="content" cols="50"><?php echo set_value('content'); ?></textarea>

<div><input type="submit" value="Submit" /></div>

</form>

</body>
</html>

Selanjutnya buat file dengan nama view_sukses.php di Application/view/news/

<html>
<head>
<title>My Form</title>
</head>
<body>

<h3>Your form was successfully submitted!</h3>

<p><?php echo anchor('news/input', 'Try it again!'); ?></p>

</body>
</html>

Buat file modelnews.php di Application/models/

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

class ModelNews extends CI_Model {

 function __construct()
     {
         parent::__construct();
     }
 
 function getAllNews(){
  $q="SELECT * FROM news"; 
  return $this->db->query($q);
 }
 function delete(){
  $q="DELETE from news where id='$id'"; 
  return $this->db->query($q);
 }
}

Buat file dengan nama news.php di Application/controller/

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

class News extends CI_Controller {

 
 public function index()
 {
  $data = array('title' => 'WEBSITE CODE IGNITER',
        'heading' => 'Copyright &copy 2012. Powered by <i>Indra Al Sasak</i>',
             'message' => 'Test Message');

  $this->load->view('view_header');
  $this->load->view('view_news_show', $data);
  $this->load->view('view_footer');;
 }

 public function show()
 {
   $data['n'] = $this->ModelNews->getAllNews();
   $this->load->view('news/view_show_page', $data);
 }
 public function input()
 {
     $this->load->helper(array('form', 'url'));

  $this->load->library('form_validation');
  $this->form_validation->set_rules('title', 'title', 'required');
  $this->form_validation->set_rules('content', 'content', 'required');

  if ($this->form_validation->run() == FALSE)
  {
   $this->load->view('news/view_news_input');
  }
  else
  {
   $this->input->post('title');
   $this->input->post('content');
   $this->ModelNews->simpan();
   $this->load->view('news/view_sukses');
  }
    
 }
 public function delete($id)
 {
  $this->db->delete('news', array('id' => $id));
   redirect('news/index');
  $this->ModelNews->deleteByid($id);
 }
 public function simpan()
 {
   echo "tersimpan";
 }
}

Semoga Bermanfaat.....