Welcome Guest, Not a member yet? Register   Sign In
Very Basic Question - Dynamic Title
#1

[eluser]Unknown[/eluser]
Ok well I am brand new to CodeIgniter as you can see and I've just watched both video tutorials. I followed the first one and came up with this:

home.php
Code:
<?php

class Home extends Controller {

    function Home()
    {
        parent::Controller();    
    }
    
    function index()
    {
        $data['title'] = "My Page Title";
    
        $this->load->view('home_view');
    }
}

?>
So I've just copied the tutorial but when I try to add the $title variable to home_view.php it presents an error. This is what I'm trying to do but it gives an error in the page title instead (Message: Undefined variable: title).

Snippet from home_view.php
Code:
<title><?php echo $title; ?></title>
#2

[eluser]slowgary[/eluser]
You forgot to send the $data to your view...
Code:
$this->load->view('home_view', $data);

BTW, I'm pretty new to CodeIgniter too and I gotta say I love it! So if you hit some snags along the way please don't be discouraged, the forums are full of knowleadgeable people that are eager to help.

One thing that is probably a good practice in this case would be to check if $title isset() in your view, maybe even replace it with a default value if not. It would just be a little more bulletproof. The alternative is to make sure there will always be a title in your editing part of the software as opposed to the display part (probably better performance this way as you'll likely be adding an article one, and many people would be viewing it).

Good luck with your project.
#3

[eluser]Unknown[/eluser]
How stupid of me. Thanks.
#4

[eluser]Unknown[/eluser]
i found same error and already pass the data,

Code:
<?php

class Welcome extends Controller {
    
    function Welcome()
    {
        parent::Controller();    
    }
    
    function index()
    {
        $data = array(
               'title' => 'My Title',
               'heading' => 'My Heading',
               'message' => 'My Message'
        );
        $this->load->view('welcome_message', $data);
    }
    
    
}

Quote:<body>


<?
echo $data['title'];
?>

</body>

and still error.
how to fix it ? thx
#5

[eluser]Zeeshan Rasool[/eluser]
[quote author="madzao" date="1249381884"]i found same error and already pass the data,

Code:
<?php

class Welcome extends Controller {
    
    function Welcome()
    {
        parent::Controller();    
    }
    
    function index()
    {
        $data = array(
               'title' => 'My Title',
               'heading' => 'My Heading',
               'message' => 'My Message'
        );
        $this->load->view('welcome_message', $data);
    }
    
    
}

Quote:<body>


<?
echo $data['title'];
?>

</body>

and still error.
how to fix it ? thx[/quote]

You are printing array here. Just echos $title
Code:
<?
echo $title;
?>




Theme © iAndrew 2016 - Forum software by © MyBB