PHPBear Posted January 5, 2014 Share Posted January 5, 2014 0down votefavorite 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.) Quote Link to comment Share on other sites More sharing options...
hansford Posted January 5, 2014 Share Posted January 5, 2014 $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, ' > '); Quote Link to comment Share on other sites More sharing options...
PHPBear Posted January 5, 2014 Author Share Posted January 5, 2014 (edited) 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, ' > '); It works fine except that the links display as plain text. Can you see what I did wrong? Edited January 5, 2014 by PHPBear Quote Link to comment Share on other sites More sharing options...
PHPBear Posted January 5, 2014 Author Share Posted January 5, 2014 (edited) 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 January 5, 2014 by PHPBear Quote Link to comment Share on other sites More sharing options...
hansford Posted January 5, 2014 Share Posted January 5, 2014 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 > symbol echo join($path, ''); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.