0
votes

I'm following the instructions in the CodeIgniter Tutorial on the Static Page but the Header does not appear although the tab is labelled "CodeIgniter Tutorial" and the content of the Footer appears on the same line as the content of the Page.

This is the content of my Pages.php - application/controllers/Pages.php

<?php
 class Pages extends CI_Controller
 {
    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);
    }
 }

This is the content of my header.php - application/views/templates/header.php

<html>
    <head>
            <title>CodeIgniter Tutorial</title>
    </head>
    <body>

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

This is the content of my footer.php - application/views/templates/footer.php

            <em>&copy; 2014</em>
    </body>
 </html>

This is the content of my routes.php - application/config/routes.php

$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

This is one URL http://localhost/MyProject/index.php/pages/view

And this is the result;

Home

Hello World! © 2014

And this is the other URL http://localhost/MyProject/index.php/pages/view/about

And this is the result;

About

Hello World! © 2014

I've checked every page numerous times, tried re-booting, and tried another browser, and tried CodeIgniter Forum, all to no avail.

Can somebody explain where I'm going wrong?


Edited

In the header.php file I've changed

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

to

<h3><?php echo $title ?></h3>

which makes the word "Home" decreases in size. The only file I can find the word "home" is Pages.php

public function view($page = 'home')

If I change that word to "CodeIgniter Tutorial" I get a 404 error.

The IE tab is labelled "CodeIgniter Tutorial"

I'm unsure if my expectations are correct, but this is the result that I'm expecting (but with "© 2014" at the bottom of the page);

CodeIgniter Tutorial

Hello World!

© 2014

3
weird.. it should all work. do you have Teamviewer? maybe I can take a look - CodeGodie
Thanks CodeGodie. By you saying "it should all work" means that there is not some stupid mistake on my part. Sorry, I don't have "Teamviewer". If you tell me where you want to look I could possibly find the error. - user225359
Maybe you can zip your whole application and upload it somewhere. I don't think the problem is with the code you're showing. Possibly some path error or similar. Check /application/logs and read any logs. If it's empty, make sure it's writable and set $config['log_threshold'] = 1; in your config file. - André Laszlo
@user225359 I updated my answer its just to do with code styling on view your code is fine. - Mr. ED

3 Answers

0
votes

It seems like the files views/pages/about.php and views/pages/home.php are empty. Try writing something in them, for example:

views/pages/about.php

<i>This is the About page</i>

If the file exist, but is empty, you should get the result you're seeing, because this line will not render anything:

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

If the files don't exist, you should get some 404 output - so that's a bit weird. It could happen if the file application/errors/error_404.php is empty.

0
votes

I tested your project on my localhost and your pages controller is fine.

It just seems to be you need to either use p tags or div etc and then use css style sheets etc.

But instead of

<em>&copy; 2014</em>

</body>
</html>

Try

<p>&copy; 2014</p>

</body>
</html>

Proof

enter image description here

enter image description here

0
votes

I created a file at application/views/pages/CodeIgniter Tutorial.php

and changed

public function view($page = 'home')

to

public function view($page = 'CodeIgniter Tutorial')

in Pages.php

and changed

<em>&copy; 2014</em>

to

<p><br><br><em>&copy; 2014</em></br></br></p>

in footer.php

I can achieve close to my expectations and now realise my expectations were far too much for a simple piece of coding.