ntnwwnet Posted June 11, 2008 Share Posted June 11, 2008 I'm building a website that consists of several php files that are included in the index.php file. My index.php file: <body> <!-- include header --> <?php include "./includes/header.inc.php"; ?> <!-- include sidebar --> <?php include "./includes/sidebar.inc.php"; ?> <!-- include the content --> <? $page = $_GET['p']; $includePage = "./pages/$page.php"; if ($page) { if ( file_exists($includePage) ) { include $includePage; } else { include "./pages/404.php"; } } else { include "./pages/home.php"; } ?> <!-- include footer --> <?php include "./includes/footer.inc.php"; ?> </div> </body> My header.inc.php includes a breadcrumbs feature that tracks the user's progress throughout the website. The included content page contains a variable titled $breadcrumb that contains the breadcrumbs to displayed in the header. My problem is that the header.inc.php is included before the content page, so the header.inc.php file never sees the $breadcrumb variable (because it hasn't been declared/included yet). Is there any way to have the header.inc.php get the $breadcrumb variable from the content page before it is included? I've tried including the content page before the header, but that also displays the content inside the header. Any help would be greatly appreciated! Quote Link to comment Share on other sites More sharing options...
bluejay002 Posted June 11, 2008 Share Posted June 11, 2008 i dont know if i am getting it right... but what if you create a function (or a class - better) inside that header script and call/instantiate it right after the breadcrumb thing has been initialized? is that what you mean? Quote Link to comment Share on other sites More sharing options...
ntnwwnet Posted June 11, 2008 Author Share Posted June 11, 2008 if you create a function (or a class - better) inside that header script and call/instantiate it right after the breadcrumb thing has been initialized? I gave that a try, but by calling the displayBreadcrumb() function inside the content page, the function displays the breadcrumb inside the content page. My page loads/displays in this order: header (where the breadcrumbs are to be displayed) sidebar content page (contains the proper breadcrumbs) footer The problem is that the header is loaded/executed before the content page. Because it is loaded/executed first, the $breadcrumb variable has yet to be initialized. I'm looking for a way to have the header load the $breadcrumb variable from the content page before it is included into the page. Quote Link to comment Share on other sites More sharing options...
btherl Posted June 11, 2008 Share Posted June 11, 2008 The simplest but somewhat dodgy fix to this is to use output buffering. This allows you to display things in whatever order you want. Eg <?php ob_start(); include "./includes/header.inc.php"; $header = ob_get_clean(); ?> Now you can do echo $header at any time to display the header. Similarly you can buffer the main content by putting ob_start() at the top and $content = ob_get_clean() at the end. Quote Link to comment Share on other sites More sharing options...
tapos Posted June 11, 2008 Share Posted June 11, 2008 then why dont you take the breadcrumb variable in the header section? BTW you can do it easily using a simple javascript though it is not the standard way. //in header.php <div id="breadcrumb"></div> //in content.php after getting the $breadcrumb variable <script> document.getElementById('breadcrumb').innerHTML = <?php echo $breadcrumb;?>; </script> Quote Link to comment Share on other sites More sharing options...
bluejay002 Posted June 11, 2008 Share Posted June 11, 2008 well, i usually use JavaScript on breadcrumbs... especially if my content is using an iframe. but if you insist on using pure PHP then do all breadcrumb things before the header since you can still display it in the content... output buffering... kinda hear that just now... am gonna check it.. thanks! all the previous posts were nice suggestions. :-) Quote Link to comment Share on other sites More sharing options...
ntnwwnet Posted June 11, 2008 Author Share Posted June 11, 2008 The simplest but somewhat dodgy fix to this is to use output buffering. This allows you to display things in whatever order you want. Eg <?php ob_start(); include "./includes/header.inc.php"; $header = ob_get_clean(); ?> Now you can do echo $header at any time to display the header. Similarly you can buffer the main content by putting ob_start() at the top and $content = ob_get_clean() at the end. Your solution worked perfectly! Thank you very much! In case you're interested, here's the working code: <!-- Breadcrumbs --> <div class="header-breadcrumbs"> <ul> <li><a href="/?p=home">Main</a></li> <?php ob_start(); $page = $_GET['p']; $includePage = "./pages/$page.php"; if ($page) { if ( file_exists($includePage) ) { include $includePage; } else { include "./pages/404.php"; } } else { include "./pages/home.php"; } $content = ob_get_clean(); if($breadcrumb) { echo "<li>" . $breadcrumb . "</li>"; } else { echo "<li>broken</li>"; } ?> <!-- end breadcrumbs --> </ul> Then, in the index page, instead of the if/else that includes the page, all that's needed is <?php echo $content; ?> . Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.