Jump to content

Recommended Posts

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!

 

Link to comment
https://forums.phpfreaks.com/topic/124238-script-not-working/
Share on other sites

Okay firstly, just as a heads up you don't need...

 

<?php include_once('includes/header.php'); ?>
<?php include_once('includes/menu.php'); ?>
<?php include_once('includes/side.php'); ?>
<?php
if (isset($.......

 

you can just use...

 

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

if (isset($...........

 

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

 

Try adding some static text to the echo to see if it it's something to do with setting up the array.. like...

 

if(array_key_exists($_GET['pg'], $breadcrumb)){
     echo 'Breadcrumbs: ' . $breadcrumb[$pg];
} else{
     echo 'This breadcrumb does not exist';
}

 

 

Adam

Link to comment
https://forums.phpfreaks.com/topic/124238-script-not-working/#findComment-641566
Share on other sites

Turned on error reporting 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

 

Not quite sure how to deal with that...?

Link to comment
https://forums.phpfreaks.com/topic/124238-script-not-working/#findComment-641986
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.