Jump to content

Variables on include()'d pages


ntnwwnet

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

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. :-)

Link to comment
Share on other sites

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; ?>

.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.