Welcome Guest, Not a member yet? Register   Sign In
Query in CI Tutorial
#1

I am studying CI documents wherein I have a query as below in the Tutorial:

public function view($page = 'home')
{
        if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
        {
                // Whoops, we don't have a page for that!
                show_404();
        }
 
        $data['title'] = ucfirst($page); // Capitalize the first letter
 
        $this->load->view('templates/header', $data);
        $this->load->view('pages/'.$page, $data);
        $this->load->view('templates/footer', $data);
}
 
In last 3 lines $data variable – how come same variable has 3 different values, that of content in header.php, footer.php & content of page.php file? Confusion in syntax/ usage of array $data[‘title’] & just $data.
Reply
#2

For 3 different values, I assume you mean 'templates/header', 'pages/'.$page and 'templates/footer'?

This is because first attribute points to template file, so these three lines pass the same data to three different template files.

For $data, views expect second parameter as array, so inside view file you can use array index as variable name to access that value:
$data['title'] = 'aaa' becomes <?= $title ?> in view file. Or $data['myvalue'] could be accessed using $myvalue.
Reply
#3

@Sovani Shreeniwas

Did you know that you could also do this...

$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page);
$this->load->view('templates/footer');

...and it would still work?
Reply
#4

(01-07-2019, 05:57 PM)php_rocs Wrote: @Sovani Shreeniwas

Did you know that you could also do this...

       $this->load->view('templates/header', $data);
       $this->load->view('pages/'.$page);
       $this->load->view('templates/footer');

...and it would still work?

Or, because in this example we do not ask "view" to return a string it could be written

PHP Code:
$this->load
    
->view('templates/header'$data)
 
   ->view('pages/'.$page)
 
   ->view('templates/footer'); 

$data only needs to be passed to "view" once because "view" will save a copy of any data it is given.
Reply
#5

(01-08-2019, 01:29 PM)dave friend Wrote:
(01-07-2019, 05:57 PM)php_rocs Wrote: @Sovani Shreeniwas

Did you know that you could also do this...

       $this->load->view('templates/header', $data);
       $this->load->view('pages/'.$page);
       $this->load->view('templates/footer');

...and it would still work?

Or, because in this example we do not ask "view" to return a string it could be written

PHP Code:
$this->load
    
->view('templates/header'$data)
 
   ->view('pages/'.$page)
 
   ->view('templates/footer'); 

$data only needs to be passed to "view" once because "view" will save a copy of any data it is given.

Thanks all for the replies. Trying to understand the inputs from you.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB