8 Replies - 9759 Views - Last Post: 08 January 2012 - 02:16 PM Rate Topic: -----

#1 raghucse2010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 14-June 11

Breadcrumb navigation in php

Posted 06 January 2012 - 11:53 PM

Hi EveryBody,
I Wonder If some one could suggest me if how i can achieve breadcumb navigation in php where all my web pages are included into index.php

for ex:

index.php?page=home for showing home page and
index.php?page=contact us for showing contact us page ....etc

and i want navaigation to be like this

home->tutorials->breadcumb.php

and i don't find any kind of help from the internet as my case is entirely different and it could get it done it will be helpful for many people who are looking for this kind of script

waiting for some suggestions guys......

Is This A Good Question/Topic? 0
  • +

Replies To: Breadcrumb navigation in php

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: Breadcrumb navigation in php

Posted 07 January 2012 - 06:22 AM

I think you're asking about bootstrapping. Something like this should work:
<?php
$page = array("home"=>"Some_home_page.php", "contact"=>"contact_us.php", "bread"=>"breadcrumb.php");
if(isset($_GET['page']) && in_array($_GET['page'], $page)) {
	include $page[$_GET['page']];
}
?>

Was This Post Helpful? 1
  • +
  • -

#3 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 943
  • View blog
  • Posts: 2,353
  • Joined: 15-February 11

Re: Breadcrumb navigation in php

Posted 07 January 2012 - 08:10 AM

@CTphpnwb That's if he's using a MVC framework. Might be mistaken but I don't hear about it anywhere else.

Breadcrumbs depend on either the URI or the actual directory. If you're going to be using Apache's URL rewriting module then I'd suggest basing your breadcrumb navigation on the URI. To get the URI of a page this is what I used.
implode(array_slice(explode('/', $_SERVER['REQUEST_URI']), 0, -1), '/') . '/'
This cuts off the file name as the end but since we don't need the entire string but simply the parts we don't need to run the implode function on it.
array_slice(explode('/', $_SERVER['REQUEST_URI']), 0, -1)

For a URI like /tutorials/php/oop/tut1.php you will get an array like tutorials | php | oop. You could make the your breadcrumb navigation from just that but more than likely you need something a bit more user-friendly. My suggestion is that you simply store the string you wish to be shown in either a database, XML file or JSON. It's up to you.
Was This Post Helpful? 1
  • +
  • -

#4 xxxjj18  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 53
  • View blog
  • Posts: 167
  • Joined: 30-November 11

Re: Breadcrumb navigation in php

Posted 07 January 2012 - 11:48 AM

Both examples are good. But the first example can be used outside of MVC; it's just an array, and all the code does is check the $_GET["page"] content, search the array for the $_GET["page"] value, and if it's found, it includes a certain page.

@codeprada : Your example is good, but sort of overcomplicates the steps needed to be taken, at least if the programmer were a beginner.

This post has been edited by xxxjj18: 07 January 2012 - 11:48 AM

Was This Post Helpful? 0
  • +
  • -

#5 cilaes  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 73
  • Joined: 12-December 11

Re: Breadcrumb navigation in php

Posted 07 January 2012 - 11:06 PM

I don't think he's looking for URL design, although I think that's a plus, too...

For breadcrumb navigation you'd have to design some sort of database for the information to come from, such as this page[b] is a page of [b]this category etc, then loop through... I'd use a database but I'll throw a small array work around of what I'm talking about just to give you an idea of where I'd head with this...

<?php
$page_dox = array(
	'home' => array('home.php', 'Homepage', ''),
	'contact' => array('contact.php', 'Contact Us', 'home');
	'contact_joe' => array('contact_joe.php', 'Contact Joe!', 'contact');
);

function show_crumbs($page, $page_dox = array()) {
	if($page_dox[$page][2] !== "") { //this is a sub cat to a category
		//do the same function for the category to loop and get its parent categories
		$parent = $page_dox[$page][2];
		show_crumbs($page_dox[$parent], $page_dox);
		
		echo $page_dox[$parent][1] . ' > ';
	}
	//else it has no parent, so do nothing.
}

if(isset($_GET['page'])) {
	$thisPage = trim($_GET['page']);
	show_crumbs($thisPage, $page_dox);
	echo $page_dox[$thisPage][1];
	
	//then do includes wherever?
}
?>


You'll have to tweak it to make it your own, but I didn't really want to give you a solution (hell, I haven't even tested that code so it probably doesn't work) just wanted to give your gears a boost into a plausible solution. Good luck!
Was This Post Helpful? 0
  • +
  • -

#6 cilaes  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 73
  • Joined: 12-December 11

Re: Breadcrumb navigation in php

Posted 07 January 2012 - 11:16 PM

...or if I'm trying to dig into your thoughts too deeply and am wrong as to the perception the others have taken, a simple google search brought up a great article on not only how to split the URL into different variables, but the basics of MOD_REWRITE as well. Here it is.
Was This Post Helpful? 0
  • +
  • -

#7 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 943
  • View blog
  • Posts: 2,353
  • Joined: 15-February 11

Re: Breadcrumb navigation in php

Posted 08 January 2012 - 10:39 AM

@xxxjj18 The thing is when using an array you'll have to manually enter each page and when handling medium to large sites the less you have to worry about the better. In all my projects I personally prefer the flexible method.

@cilaes That example too uses the $_SERVER['REQUEST_URI'] method.
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: Breadcrumb navigation in php

Posted 08 January 2012 - 12:06 PM

There's no reason that array couldn't be read from a table in a database or even a flat file.
Flexibility is up to the developer. ;)
Was This Post Helpful? 0
  • +
  • -

#9 cilaes  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 73
  • Joined: 12-December 11

Re: Breadcrumb navigation in php

Posted 08 January 2012 - 02:16 PM

@codeprada i know that my second post does, in case that's what he was looking for... but I didn't think by how he asked his question that he'd want to reconstruct his URLs. Also, wanted to throw more ideas on how he could go about it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1