jaymc Posted June 2, 2008 Share Posted June 2, 2008 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 More sharing options...
Dragen Posted June 2, 2008 Share Posted June 2, 2008 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 More sharing options...
jaymc Posted June 2, 2008 Author Share Posted June 2, 2008 The title can only come from $page.php header must be echoed before $page is included, so it will never get the $title from $page.php Link to comment https://forums.phpfreaks.com/topic/108399-solved-apply-title/#findComment-555711 Share on other sites More sharing options...
trq Posted June 2, 2008 Share Posted June 2, 2008 Your going to need to arange your code some other way. Its that simple, you can't echo a variable prior to it being defined and expect it to work. Link to comment https://forums.phpfreaks.com/topic/108399-solved-apply-title/#findComment-555713 Share on other sites More sharing options...
Dragen Posted June 2, 2008 Share Posted June 2, 2008 <? $title = 'this is a title..'; include("header.pho"); //page info here include("footer.pho"); ?> Link to comment https://forums.phpfreaks.com/topic/108399-solved-apply-title/#findComment-555715 Share on other sites More sharing options...
discomatt Posted June 2, 2008 Share Posted June 2, 2008 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 More sharing options...
jaymc Posted June 2, 2008 Author Share Posted June 2, 2008 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 More sharing options...
whizard Posted June 2, 2008 Share Posted June 2, 2008 global $varName //This is a global function http://us2.php.net/global HTH Dan Link to comment https://forums.phpfreaks.com/topic/108399-solved-apply-title/#findComment-555838 Share on other sites More sharing options...
discomatt Posted June 2, 2008 Share Posted June 2, 2008 It's not set inside the function. It does work. You should take more time to understand the code when people try to help you. Link to comment https://forums.phpfreaks.com/topic/108399-solved-apply-title/#findComment-555850 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.