Jump to content

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

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.