Monday, March 28, 2016

Lesson 2 : Passing data to controller from the URL

Create a controller file named 'lessontwo.php' with the following code:

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

class Lessontwo extends CI_Controller {
public function index(){
               echo "<h1> Welcome to Lesson Two.</h1>
}


public function quote($a){

        $quotes['einstein']=
"The world is a dangerous place to live; not because of the
people who are evil, but because of the people who don't do
anything about it. Albert Einstein.";

$quotes['shakespeare']="Love all, trust a few, do wrong to none. William Shakespeare.";

$quotes['twain']="All you need in this life is ignorance and confidence, and then success is                 sure. Mark Twain.";

switch ($a) {
    case 'einstein':
    $data['quote']=$quotes['einstein'];
    break;
    case 'shakespeare':
    $data['quote']=$quotes['shakespeare'];
    break;
    case 'twain':
    $data['quote']=$quotes['twain'];
    break;
        }
$this->load->view('lessontwoview',$data);
}
}


Next, create a view file 'lessontwoview.php' with following code:

<h1>
<?php echo $quote ?>
</h1>

Point to url: http://localhost/codi/lessontwo/quote/shakespeare

You should see the quote by shakespeare.

Change shakespeare to twain, you get Mark Twain's quote. Cool huh!

No comments:

Post a Comment