Friday, August 16, 2013

Controller, Method and Argument in Codeigniter

Hi...
Welcome to my next tutorial. Before i go further in update sql statement, there is an important thing to know about what is controller, method and arguments. Open up your browser, go to localhost and folder containing codeigniter. You will have something like this :
http://localhost/ci

For your information, that address comes from this URL :
http://localhost/ci/index.php/welcome/index

Let me show the split of the address above :
  • localhost = domain
  • ci = folder
  • index.php = index file
  • welcome = controller
  • index = method

Welcome controller as you already know comes from the file :
application/controller/welcome.php
So wheres the method comes from? Well, if you open up welcome.php, it will look like this.

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

class Welcome extends CI_Controller {
 public function __construct() {
  parent::__construct();
  $this->load->helper('url');
 }

 public function index() {
  $this->load->view('welcome_message');
 }

 //buat method baru
 public function papar1() {
  echo 'wahaaa';
  echo '<br />'.anchor('', 'home');
 }

 public function papar2() {
  $this->load->view('test');
 }

 public function papar3() {
  //kali ni kita akan pass variable dari controller ke views
  $data['lk'] = 'passing data dari controller ke views';
  $this->load->view('passing', $data);
 }

 public function dbase() {
  //load database
  $this->load->database();
  //load model
  $this->load->model('orang');
  //load helper
  $this->load->helper('form');

  //process a form
  if(isset($_POST['save'])) {
   $nama = $_POST['nama'];
   $tel = $_POST['tel'];
   $result = $this->orang->insert(array('nama' => $nama, 'telefon' => $tel));
   if($result) {
    $data['info'] = 'success insert data';
   } else {
    $data['info'] = 'please try again later';
   }
  }

  $data['query'] = $this->orang->GetAll(NUll, NULL);
  $this->load->view('database', $data);
 }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

Look at the "public function somefunction() {}", that is the method in a controller. So where comes the arguments?
Look at this URL
http://localhost/ci/index.php/welcome/update/1

Its still the same, just follow the concept.
  • localhost = domain
  • ci = folder
  • index.php = index file
  • welcome = controller
  • update = method
  • 1 = argument
How about this 1?
http://localhost/ci/index.php/welcome/update/1/as/qwe/23/56
Its still the same thing, except, whatever comes after method, all of it were an argument..
  • localhost = domain
  • ci = folder
  • index.php = index file
  • welcome = controller
  • update = method
  • 1 = argument
  • as = argument
  • qwe = argument
  • 23 = argument
  • 56 = argument
Usually argument is meant for an "id" to a specific data in a database. I'll show that in my next toturial for update sql statement.

Tuesday, August 13, 2013

Introduction To CRUD (Insert Statement)

Hi,
I've been having a holiday for sometimes due to Hari Raya Aidilfitri. There's 1 time, i had a stomachache due to a lot of ketupat and rendang consuming....
Anyway, this time is a continuation from previous post "Introduction To CRUD". So pls refer to that post if you feels a little bit lost... :P
Insert statement in sql with codeigniter is a little bit fun for some people. Take a look at 13:23.
Also in this post i shared form helper. Its really useful sometimes later when we go further more in this topic if you were asked me why i used form helper. Just stick with me in this matter.

Caution : Please do not use this kind of code for your production. It does have a lot of security issues, i'll be covering that topic later.

Sunday, August 11, 2013

Eid mubarak

Taqabbalallahu minkum... Minna wa minkum.. Taqabbalya karim...
To all muslim friends....