PDA

View Full Version : Script not working?



intothefray2007
September 14th, 2008, 07:12 PM
Someone gave me a hand with a way to load breadcrumbs after the header has loaded, but I'm having trouble getting this to work. Can anyone take a look?

I'm using the following structure:

----------------------
index.php
----------------------
<?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 requested does not exist';
}
} else {
@include ('pages/home.php');
}
?>
<?php include_once('includes/footer.php'); ?>

----------------------
menu.php
----------------------
<?php
$breadcrumb = array('home'=>'Home', 'about'=>'About Us');
if(isset($_GET['pg'])){
if(array_key_exists($_GET['pg'], $breadcrumb)){
echo $breadcrumb[$pg];
} else{
echo 'This breadcrumb does not exist';
}
} else{
echo $breadcrumb['home'];
}
?>

-------------------------------------------------------------------------------
A sample URL would be http://www.mywebsite.com/?pg=about

The original script in the index.php file checks the variable $pg and includes the appropriate PHP file from the /pages directory.
The breadcrumb script in menu.php also checks the $pg variable and sets the breadcrumb accordingly.

Sounds good.... but for some reason it is not printing out anything at all ($breadcrumb), UNLESS I load the URL without a ?pg= value, in which case I get the default value ('home').

Anyone have any ideas? Thanks!

tfg
September 15th, 2008, 06:58 AM
are you getting the "htis breadcrumb does not exist" message at all when you try it with pg set

turn on error reporting in menu.php.

<?php
error_reporting(E_ALL);
$breadcrumb = array('home'=>'Home', 'about'=>'About Us');

nothing strikes me as obviously wrong there. but it is 11 o'clock on a monday morning and i've not woken up properly yet.

intothefray2007
September 15th, 2008, 12:36 PM
No I wasn't getting the "breadcrumb does not exist" message. I turned on error reporting as you suggested and got the following:

Notice: Undefined variable: pg in /home/blackbir/public_html/test/v3/includes/menu.php on line 47
Notice: Undefined index: in /home/blackbir/public_html/test/v3/includes/menu.php on line 47

Any thoughts?




are you getting the "htis breadcrumb does not exist" message at all when you try it with pg set

turn on error reporting in menu.php.

<?php
error_reporting(E_ALL);
$breadcrumb = array('home'=>'Home', 'about'=>'About Us');

nothing strikes me as obviously wrong there. but it is 11 o'clock on a monday morning and i've not woken up properly yet.

eirche
September 16th, 2008, 01:03 AM
Notice: Undefined variable: pg in /home/blackbir/public_html/test/v3/includes/menu.php on line 47
Notice: Undefined index: in /home/blackbir/public_html/test/v3/includes/menu.php on line 47

exactly what it says. $pg is undefined. you need to define/initialize before accessing it.

tfg
September 16th, 2008, 06:44 AM
right. i've found the error.

echo $breadcrumb[$pg];

$pg doesn't exist as a variable so you can't access the breadcrumb array using it. a simple fix would be

$pg = $_GET['pg'];
echo $breadcrumb[$pg];

providing that $_GET['pg'] holds a variable which can be used as the array index.

intothefray2007
September 16th, 2008, 08:27 AM
I just figured that out last night, but thanks for spotting it too! Cheers!