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
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
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.