Query in CI Tutorial - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: Query in CI Tutorial (/showthread.php?tid=72573) |
Query in CI Tutorial - Sovani Shreeniwas - 01-07-2019 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. RE: Query in CI Tutorial - Pertti - 01-07-2019 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. RE: Query in CI Tutorial - php_rocs - 01-07-2019 @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? RE: Query in CI Tutorial - dave friend - 01-08-2019 (01-07-2019, 05:57 PM)php_rocs Wrote: @Sovani Shreeniwas Or, because in this example we do not ask "view" to return a string it could be written PHP Code: $this->load $data only needs to be passed to "view" once because "view" will save a copy of any data it is given. RE: Query in CI Tutorial - Sovani Shreeniwas - 01-09-2019 (01-08-2019, 01:29 PM)dave friend Wrote:(01-07-2019, 05:57 PM)php_rocs Wrote: @Sovani Shreeniwas Thanks all for the replies. Trying to understand the inputs from you. |