<?php

// This function will take $_SERVER['REQUEST_URI'] and build a breadcrumb based on the user's current path
function breadcrumbs($separator = ' &raquo; ', $home = ' <i class="fa fa-home"></i> Home  ') {
    // This gets the REQUEST_URI (/path/to/file.php), splits the string (using '/') into an arradie;
   //echo $_SERVER['REQUEST_URI'];
    $uri = str_replace("","", $_SERVER['REQUEST_URI']);
    $path = array_filter(explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));
    //print_r($path);
   
    // This will build our "base URL" ... Also accounts for HTTPS :)
	if ( isset( $_SERVER["HTTPS"] ) && strtolower( $_SERVER["HTTPS"] ) == "on" ) {
		$base = ($_SERVER['HTTP'] ? 'http' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';
	}else{
		$base = BASE_URL.'/';
	}
    

    // Initialize a temporary array with our breadcrumbs. (starting with our home page, which I'm assuming will be the base URL)
    $breadcrumbs = Array("<a href=\"$base\">$home</a>");

    // Find out the index for the last value in our path array
    $keys = array_keys($path);
		$last = end($keys);

    // Build the rest of the breadcrumbs
    foreach ($path AS $x => $crumb) {
        
        // Our "title" is the text that will be displayed (strip out .php and turn '_' into a space)
        $title = ucwords(str_replace(Array('.php', '-'), Array('', ' '), $crumb));

        // If we are not on the last index, then display an <a> tag
        if ($x != $last){
            $filepath = $_SERVER['DOCUMENT_ROOT']."/".$crumb."/".$crumb.".php"; 
            $filepath2 = $_SERVER['DOCUMENT_ROOT']."/".$crumb."/index.php"; 
           //echo $filepath ;
            $href = $base.$crumb."/".$crumb.".php";
            if(file_exists($filepath2) || file_exists($filepath)){
                $breadcrumbs[] = "<a href=\"$href\">$title</a>";
            }else{
               // $breadcrumbs[] = "";
               $breadcrumbs[] = "<a href=\"#\">$title</a>";
            }
            //$breadcrumbs[] = "<a href=\"$base$crumb\">$title</a>";
        // Otherwise, just display the title (minus)
        }else
            $breadcrumbs[] = $title;
    }

    // Build our temporary array (pieces of bread) into one big string :)
    return implode($separator, $breadcrumbs);
}

?>


<div class="container">
<nav aria-label="breadcrumb">
  <ol class="breadcrumb">
    <li class="breadcrumb-item active" aria-current="page"> <?= breadcrumbs('  /  ') ?></li>
  </ol>
</nav>
</div>
