Jump to content

Need help with bread crumbs nav links array/path


PHPBear

Recommended Posts

I just converted a query that displays "bread crumbs" style navigation links to PDO:

 

				function get_path($dbh,$node,$TopnavTable, $TopnavName) {
				$stmt = $dbh->prepare("SELECT name FROM $TopnavTable WHERE $TopnavName = ?");
				$stmt->bindValue(1,$node);
				$stmt->execute();
				$stmt->setFetchMode(PDO::FETCH_ASSOC);
				$row = $stmt->fetch();
				$path = array_merge(get_path($pdo,$row['Parent'], $TopnavTable, $TopnavName), $path);
				return $path ;
				}
				

 

I also figured out a way to display the results:

 

 

$Path2 = explode("/", $path);
				$Path2[1] = '<a href="/Topics">'.$Path2[1].'</a>';
				$Path2[2] = '<a href="/Topics/'.$Path2[2].'">'.$Path2[2].'</a>';
				$Path2[3] = '<span class="navHere"><b>'.$Path2[3].'</b></span>';
				
				echo join( $Path2, ' > ' );
				

 

However, it only works only if I'm working with an array consisting of three segments. For example, I'm viewing the URL MySite/Topics/Washington/Governor, which displays the following bread crumbs trail:

Topics > Washington > Governor

If I view MySite/Washington, it should display...

Topics > Washington

But I get an error message: Undefined offset: 3, plus there's a trailing arrow (>) after Washington.

So I'm trying to figure out how to make this work with any number of segments - 2, 3, 6, etc. Regardless of the number of segments, I'd like the last segment to be unlinked. (I'm going to further put it inside a span.) Does anyone have any tips? (If there's a completely different way of doing it that's better, I'd love to hear about it.)

Link to comment
Share on other sites


$str = 'path1/path2/path3';
$path = explode('/',$str);
$size = count($path);

for($i = 0; $i < $size; $i++) {

if($i == $size - 1) {

$link = htmlentities('<span class="navHere"><b>' . $path[$i] . '</b></span>');
}
else {

$link = htmlentities('<a href="/Topics">' . $path[$i] . '</a>');
}

$path[$i] = $link;
}

echo join($path, ' &gt ');
Link to comment
Share on other sites

Thanks, Hansford. I think that's the ticket, though I'm doing something wrong. I modified your code a bit to match my query and came up with this:

function get_path($dbh,$node,$TopnavTable, $TopnavName) {
    $stmt = $dbh->prepare("SELECT name FROM $TopnavTable WHERE  $TopnavName = ?");
    $stmt->bindValue(1,$node);
    $stmt->execute();
    $stmt->setFetchMode(PDO::FETCH_ASSOC);  
    $row = $stmt->fetch();  
    $path = array_merge(get_path($pdo,$row['Parent'], $TopnavTable, $TopnavName), $path);
    return  $path ;  
}
$Path2 = explode("/", $path);
$size = count($Path2);
 
for($i = 0; $i < $size; $i++) {
 
if($i == $size - 1) {
 
$link = htmlentities('<span class="navHere"><b>' . $Path2[$i] . '</b></span>');
}
else {
 
$link = htmlentities('<a href="/Topics">' . $Path2[$i] . '</a>');
}
 
$Path2[$i] = $link;
}
 
echo join($Path2, ' &gt ');

It works fine except that the links display as plain text. Can you see what I did wrong?

Edited by PHPBear
Link to comment
Share on other sites

I just discovered that the links are properly hidden in the code if I remove the htmlentities thing...

$link = '<span class="navHere"><b>' . $Path2[$i] . '</b></span>';

However, in the string Topics > Washington > Governor, Topics and Washington are both linked to /Topics.

Edited by PHPBear
Link to comment
Share on other sites

Yeah, I like to store in non-html chars. This code will remove the last span tag if it's not necessary.

//keep all paths with '/' symbol - even the last one
$str = 'path1/path2/path3/path4'; 

$path = explode('/',$str);

//remove last array element with / symbol and no path
array_pop($path);

$size = count($path);

for($i = 0; $i < $size; $i++) {
	
	//if on the first and last array element - don't add > symbol and break loop
	if($size == 1) {
		
		$link = htmlentities('<a href="/Topics">' . $path[$i] . '</a>');
		$path[$i] = $link;
		break;
	}
	//if on the last array element - don't add > symbol
	if($i == $size - 1) {
		
		$link = '<span class="navHere"><b>' . $path[$i] . '</b></span>';
	}
	else {
	
	//else add > symbol
		$link = '<a href="/Topics">' . $path[$i] . '</a> > ';
	}
	
	$path[$i] = $link;
}
//no need to join with &gt symbol
echo join($path, '');
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.