views:

42

answers:

0

Hi,

I have recently created a breadcrumbs class that fits in with our in-house CMS and it works as I intend but I have a nagging feeling it could have been done better. I was thinking of doing an array_pop close to the end of the construct but I decided to go with an IF statement instead, what do you guys think?

(Oh also $_REQUEST['section'] is say ../categories/ and $_REQUEST['pageid'] is like ../categories/tshirts.html)

Hope that makes sense.

Thanks!

<?

class breadcrumbs {



//Preparing our variables

 var $section;
 var $page;
 var $home;
 var $bread_array;

 //Runs on any function we run inside this class
 function __construct() {

 //Including so I can get the url used in $this->home
 global $shop;

  //We use this each time
  $this->home = '<a href="' . $shop->non_ssl_root . '">Home</a>';

  //Sometimes there will be pages on their own so we use an if statment to use $this->section for multiple purposes.
  if($_REQUEST['section']) {

  //Simple cleaning up of the section variable
   $this->section = $_REQUEST['section'];

   $this->section = trim($this->section);

   $this->section = ucfirst($this->section);

  }

  else {
  //Else we change $this->section to just get the URI and clean that up.
   $unwanted_vars = array('/', '.', 'php', 'html');

   $this->section = $_SERVER['REQUEST_URI'];

   $this->section = str_replace($unwanted_vars, '', $this->section);

   $this->section = trim($this->section);

   $this->section = ucfirst($this->section);

  }

  //Getting the page name to clean up and add a link to
   $this->page = $_REQUEST['pageid'];

   $this->page = trim($this->page);

   $this->page = ucfirst($this->page);

   $this->page = '<a href="' . $_REQUEST['section'] . '/' . $_REQUEST['pageid'] . '.html">' . $this->page . '</a>';

  //If we have a stand alone page we already have $this->section as a backup to sort that. So we don't need $this->page in the array.
  if(!isset($_REQUEST['pageid'])) {

  $this->bread_array = array($this->home, $this->section);

  }

  else {

  $this->bread_array = array($this->home, $this->section, $this->page);

  }

 }

 //Certain designs will require different glue, so we give them the option.
 function final_breadcrumbs($seperator) {

 //Formatting the breadcrumbs
 $breadcrumbs = implode(' ' . $seperator . ' ', $this->bread_array);

  //Echoing the breadcrumbs
  return $breadcrumbs;

 }
}

?>