Jump to content

Bread Crumb Trail


Vivid Lust

Recommended Posts

The code im using for a bread crumb trail is currently not working!

 

Code:

 

<?php
$id = $_GET['id'];
if ( strlen( $id ) < 1 )
	$id = "home";

$pages = array(
	home => array( id=>"home", parent=>"", title=>"Home",
			url=>"index.php" ),
	users => array( id=>"company", parent=>"home", title=>"Company",
			url=>"index.php?page_id=5" ),
	jack => array( id=>"tutorials", parent=>"home", title=>"Tutorials",
			url=>"index.php?page_id=7" )
	);

	function breadcrumbs( $id, $pages )
	{
	  $bcl = array( );
	  $pageid = $id;
	  while( strlen( $pageid ) > 0 )
	  {
		$bcl[] = $pageid;
		$pageid = $pages[ $pageid ]['parent'];
	  }
	  for( $i = count( $bcl ) - 1; $i >= 0; $i-- )
	  {
		$page = $pages[$bcl[$i]];
		if ( $i > 0 )
		{
			echo( "<a href=\"" );
			echo( $page['url'] );
			echo( "\">" );
		}
		echo( $page['title'] );
		if ( $i > 0 )
		{
		echo( "</a> | " );
		}
	  }
   }
   ?>

Breadcrumbs: <?php breadcrumbs( $id, $pages ); ?>

 

Have a look on the following site, click on two other links on the menu and you'll see that the breadcrumb trail always shows home although i had specified the other pages in the above code.... Any ideas upon what the problem is and how it can be fixed?

 

Link: http://deazys-services.com/

 

Thanks in advanced!

Link to comment
https://forums.phpfreaks.com/topic/120823-bread-crumb-trail/
Share on other sites

Simplify the function i.e.

 

function breadPath($path) {
$crumb = array();
foreach($path as $id => $val) {
	$crumb[] = "<a href='index.php".(($id !=0) ? "?pageId=".$id."" : "")."'>".$val."</a>";
}
return implode(" >> ", $crumb);
}

$path = array("Home",5 => "Page 1", 6 => "Page 2");
echo breadPath($path);

Link to comment
https://forums.phpfreaks.com/topic/120823-bread-crumb-trail/#findComment-622934
Share on other sites

Yes. Heres another method.

 

function breadPath($path,$pathTxt) {
$pathArray    = explode("|",$path);
$pathTxtArray = explode('|',$pathTxt);
$link = array();
for($x = 1; $x < count($pathTxtArray); $x++) {
	$link[] = '<a href="index.php?pageId='.$pathArray[$x].'">'.$pathTxtArray[$x].'</a>';
}
return implode(" >> ",$link);
}

// page path: Home|Page 1|Sub Page 1
// page id path: 0|1|69
$path = "0|1|69";
$pathTxt = "Home|Page 1|Sub Page 1";
echo breadPath($path,$pathTxt);

 

I would store the pages in a database table with the following fields:

 

pages

------

pageId

parentId

title

path

pathTxt

 

So if I am on pageId 69 I can get the record for pageId 69. The path may be 0|1|69 (its parent being 1) and the pathText would be Home|Page 1|Sub Page 1. Run the values through the function.

 

So the table records may look like:

0 , -1 , Home , 0 , Home

1 , 0 , Page 1 , 0|1, Home|Page 1

69, 1, Sub Page 1, 0|1|69, Home|Page 1|Sub Page 1

 

Get it?

 

Link to comment
https://forums.phpfreaks.com/topic/120823-bread-crumb-trail/#findComment-622960
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.