I have the following question: How to put a string at the beginning of a breadcrumb?
The case is, I want to search for a keyword Test 4.
The breadcrumb that will be generated is: Test 1 > Test 2 > Test 3 TEST 4
The 2nd keyword that I am searching for is Test 7
The breadcrumb that I want to be generated is: ... Test 4 > Test 5 > Test 6 TEST 7
How can I put the dots (...) in at the beginning of the breadcrumb?
This is my current code:
public function getPathNames($node_id, $id_tag) {
$node_ids=$this->getPath($node_id);
$r = array();
foreach($node_ids as $id){
$NodeObject = NodeObject::where('id','=',$id)->firstOrFail();
if ($this->getCurrentUserGroup() == 4) {
$NodeRevision = NodeRevision::where('id','=',$NodeObject->node_revision)->firstOrFail();
} else {
if (empty($NodeObject->node_revision_draft)) {
$NodeRevision = NodeRevision::where('id','=',$NodeObject->node_revision)->firstOrFail();
} else {
$NodeRevision = NodeRevision::where('id','=',$NodeObject->node_revision_draft)->firstOrFail();
}
}
$r[]= '<a id="'.$id_tag.$NodeObject->id.'" href="#" class="search_path_click">'.$NodeRevision->name . '</a> <i class="fa fa-chevron-right" style="color: #000; font-size: 0.5em;"></i>';
}
// only show the last 3 names in the breadcrumb
return array_slice($r, -3, 3, false);
}
EDITED:
To add the three dots to the begging of the first item in the array if the array size is equal to or greater than 3, all you have to do is
// only show the last 3 names in the breadcrumb
$r = array_slice($r, -3, 3, false);
if( count($r) >= 3 ) {
$r[0] = '… '.$r[0]; // we know the keys won't be preserved as you used `false` in `array_slice` function, so we can safely assume first array element will be 0
}
return $r
example:
return array(0 => "...") + array_slice($r, -4, 4, false);
that's what you want?
Mahmoud, thanks for your response. I want to add a string into the first array item. I don't want to populate an extra array item ;-). Maby I wasn't clear in that. So what Im trying to do now is this:
$r = array_slice($r, -3, 3, false); if (count($r) >= 3) { $rr = array(); foreach ($r as $key => $oneR) { if ($key == 0) { $rr = '<span style="color: #000;">...</style>' . $oneR; } else { $rr = $oneR; } } return $rr; } else { return $r; }
Thanks guys for your reponse! I combined your codes and this is the result that I want to achieve!
$arraySliced = array_slice($r, -4, 4, false);
if (count($arraySliced) > 3) {
return array(0 => "<span style='color: #000;'>...</span>") + $arraySliced;
}else{
return $arraySliced;
}