Jump to content

[SOLVED] APPLY TITLE


jaymc

Recommended Posts

My page is laid out like this

 

<html><head>
<title><? $title_should_go_here ?></title>
</head>

<body>

SOME STATIC CONTENT HERE

<div> <? include("$page.php"); ?></div>

</body>
</html>

The problem is, I can only really get the correct title from $page.php, but <title></title> is called before the include

 

Im just thinking of the best way to sort this out?

 

By the way $page.php has php code within, that must be parsed, so the likes of $content = file_get_contents("$page.php"); is no good

Link to comment
https://forums.phpfreaks.com/topic/108399-solved-apply-title/
Share on other sites

do it the other way around.

Have each page include the header and footer. That way you can store the title ina variable on each page:

<? include("header.pho"); ?>
$title = 'this is a title..';
<? include("footer.pho"); ?>

 

then in header.php:

<html><head>
<title><? echo $title; ?></title>
</head>

<body>

SOME STATIC CONTENT HERE

<div>

 

and footer.php:

</div>

</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/108399-solved-apply-title/#findComment-555705
Share on other sites

Define the dynamic content within a function, or something similar.

 

An example of $page.php

 


$pageTitle = 'Photos';

# Make sure another page hasnt already be included
if ( !function_exists( 'displayPage' ) )
    function displayPage () {

        # The regular content of $page.php goes here #

    }
else
    echo 'Display function already defined!';

 

Then on your main index page, use something like this...

 

<?php
if ( file_exists( $page . '.php' ) )
    include( $page . '.php' );
?>
<html><head>
<title><?php echo ( !empty($pageTitle) ? $pageTitle : 'Default Title' ) ?></title>
</head>

<body>

SOME STATIC CONTENT HERE

<div> <?php if ( function_exists( 'displayPage' ) ) displayPage(); ?></div>

</body>
</html>

 

As it becomes more complex and there's more variables being referenced, use classes :)

Link to comment
https://forums.phpfreaks.com/topic/108399-solved-apply-title/#findComment-555717
Share on other sites

Define the dynamic content within a function, or something similar.

 

An example of $page.php

 


$pageTitle = 'Photos';

# Make sure another page hasnt already be included
if ( !function_exists( 'displayPage' ) )
    function displayPage () {

        # The regular content of $page.php goes here #

    }
else
    echo 'Display function already defined!';

 

Then on your main index page, use something like this...

 

<?php
if ( file_exists( $page . '.php' ) )
    include( $page . '.php' );
?>
<html><head>
<title><?php echo ( !empty($pageTitle) ? $pageTitle : 'Default Title' ) ?></title>
</head>

<body>

SOME STATIC CONTENT HERE

<div> <?php if ( function_exists( 'displayPage' ) ) displayPage(); ?></div>

</body>
</html>

 

As it becomes more complex and there's more variables being referenced, use classes :)

 

That did not work, because you cant use a variable outside of a function, when the variable was set within the function

 

Unless there is a way to set the variable so it can be used GLOBALLY outside of the function?

Link to comment
https://forums.phpfreaks.com/topic/108399-solved-apply-title/#findComment-555831
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.