Jump to content

function stupidity


sibrows

Recommended Posts

Hi All

 

I've been playing with a bit of OOP I am trying to write a function to build a breadcrumbs navigation built from the contents of the URL. So far I have this:

 

Call the function:

BreadCrumbs($_SERVER['REQUEST_URI']);

 

The function code:

function BreadCrumbs($URL){
	$URL_PATH = explode("/", $URL);
	$URL_PATHcount = count_all($URL_PATH);
	$Count = 1;
	$SubCount = 1;
	echo '<div class="BreadCrumbs"><a href="/"><img src="/icons/home.png"></a>';
	do {
		echo ' > <a href="';
		do {
			echo '/'. $URL_PATH[$SubCount];
			$SubCount = $SubCount + 1;
		} while ($SubCount <= $Count);
		echo'">' . $URL_PATH[$Count] . '</a>';
		$Count= $Count + 1;
	} while ($Count <= $URL_PATHcount);
	echo '</div>';		
}

 

Currently if the URL is:

http://localhost/Section/SubSection/Page

 

The Output (ignoring the home icon):

> Section > SubSection > Page

 

The issue I have is when trying to build the links for each page. Currently it is pointing to:

Section = 'http://localhost/Section'

SubSection = 'http://localhost/SubSection'

Page = 'http://localhost/Page'

 

I'm now a little stuck with what to try next having spent most of the weekend trying to get it sorted.

 

Any help would be gratefully received.

 

Simon

 

Link to comment
https://forums.phpfreaks.com/topic/132107-function-stupidity/
Share on other sites

here is a cleaner version

 

<?php
function BreadCrumbs($url){
  $parts = explode('/',$url);
  unset($parts[0]);
  $path = '';
  echo '<div class="BreadCrumbs"><a href="/"><img src="/icons/home.png" /></a>';
  foreach($parts as $part){
    $path .= '/'.$part;
    printf(' > <a href="%s">%s</a>',$path,$part);
  }
  echo '</div>';      
}
BreadCrumbs('/Section/SubSection/Page');
?>

Link to comment
https://forums.phpfreaks.com/topic/132107-function-stupidity/#findComment-686633
Share on other sites

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.