Jump to content

Loading variables, after they've been parsed?


intothefray2007

Recommended Posts

Hello everyone!

 

Let's start off by saying that I don't have years of experience with PHP programming, though I do understand the language.

 

I have an index.php page that includes the following:

 

---------------------------------------------------------------------

<?php include_once('includes/header.php'); ?>

<?php include_once('includes/menu.php'); ?>

<?php include_once('includes/side.php'); ?>

 

<?php

if (isset($_GET['pg']) && $_GET['pg'] != "") {

$pg = $_GET['pg'];

if (file_exists('pages/'.$pg.'.php')) {

@include ('pages/'.$pg.'.php');

} elseif (!file_exists('pages/'.$pg.'.php')) {

echo 'The page you are requesting does not exist';

}

} else {

@include ('pages/home.php');

}

?>

 

<?php include_once('includes/footer.php'); ?>

---------------------------------------------------------------------

 

After my includes load, the middle part checks which ?id's been sent through the URL so that it loads the appropriate file in my /pages directory (such as about.php). In each of these subpages, I set the "$pagetitle" variable that is called (echo) in header.php.

 

Problem is... I'm calling $pagetitle (in header.php) BEFORE the subpage is called, so the variable gets missed... How do I go about changing the page title AFTER it is originally parsed?

 

Also, I was trying to implement a breadcrumb script in menu.php but I'm not getting anywhere because of this same problem (the breadcrumb script loads before the subpage does).

 

Anyone have a clue on how I can get around this?

 

Thank you!

You can't do much on this as long as you use this system, which is quite ok. What I would do is change the header.php code to display the right title. Let's go for an example:

 

header.php

<?php
$urls = array('home'=>'This is the home page', 'about'=>'Wanna know about me?');
if(isset($_GET['page'])){
     if(array_key_exists($_GET['page'], $urls)){
          echo '<title>' . $urls[$page] . '</title>';
     } else{
          echo '<title>Page does not exist</title>';
     }
} else{
     echo '<title>' . $urls['home'] . '</title>';
}
?>

 

Basically you print the title independent from the included file, but with the proper validation so they match. Hope this gives you the right idea.

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.