I want to convert the following php array:
$crumbs = array(
'Financial Accounting' => 'financial',
'Ratio Analysis' => 'ratios',
'Current Ratio' => 'current-ratio'
);
to html breadcrumb as shown below:
<ul>
<li><a href="/">Home</a></li>
<li><a href="/financial/">Financial Accounting</a></li>
<li><a href="/financial/ratios/">Ratios Analysis</a></li>
<li><a href="/financial/ratios/current-ratio">Current Ratio</a></li>
</ul>
Anyone please show me how to do it in php. Thanks !!
UPDATE: Thanks for your help. Following is what worked for me:
<?php
$dotname = basename($_SERVER['PHP_SELF']);
$crumbs = array(
'Financial Accounting' => 'financial',
'Ratio Analysis' => 'ratios',
'Current Ratio' => 'current-ratio'
);
echo("<ul>
<li><a href=\"/\">Home</a> ></li>
");
foreach ($crumbs as $atext => $aloc) {
if ($dotname != 'index.php' && $aloc == end($crumbs)) {
$url .= '/'.$aloc;
echo("<li><a href=\"$url\">$atext</a></li>
");
} else {
$url .= '/'.$aloc;
echo("<li><a href=\"$url/\">$atext</a> ></li>
");
}
}
echo('</ul>');
?>
I'd go with a foreach and a variable to remember the path. I'll whip up an example in a few seconds.
Somthing like that?:
<ul>
<?php
foreach($crumb as $name=>$href){
echo "<a href=/'$href'>$name</a>";
}
?>
<ul>
<li><a href="/">Home</a></li>
<?php
$path = '/';
foreach($crumbs as $name => $href) {
echo '<li><a href="'.$path.$href.'/"></li>';
$path .= $name.'/';
}
?>
</ul>
you can use this
<?php
function ConvertPHPArrayToBreadcrumb($arr) {
//convert the php array to html breadcrumb as required in
//http://stackoverflow.com/questions/8464019/convert-php-array-to-html-breadcrumbs
$crmbs = '<ul> <li><a href="/">Home</a></li>';
foreach ($arr as $key => $val) {
$crmbs .= '<li><a href="' . $val . '/">' . $key . '</a></li>';
}
$crmbs .= "</ul>";
return $crmbs;
}
//define the array
$crumbs = array(
'Financial Accounting' => 'financial',
'Ratio Analysis' => 'ratios',
'Current Ratio' => 'current-ratio'
);
//call the convert function
$html_crumbs = ConvertPHPArrayToBreadcrumb($crumbs);
//echo the reuls
echo($html_crumbs);
?>
function showcrumbs($crumbs) {
echo '<ul><li><a href="/">Home</a></li>';
$path='/';
foreach($crumbs as $name=>$href){
echo '<li><a href="'.$path.$href.'">'.$name.'</a></li>';
$path = $path.$href.'/';
}
echo '</ul>';
}
Usage:
<?php
function showcrumbs($crumbs) {
echo '<ul><li><a href="/">Home</a></li>';
$path='/';
foreach($crumbs as $name=>$href){
echo '<li><a href="'.$path.$href.'">'.$name.'</a></li>';
$path = $path.$href.'/';
}
echo '</ul>';
}
$crumbs = array(
'Financial Accounting' => 'financial',
'Ratio Analysis' => 'ratios',
'Current Ratio' => 'current-ratio'
);
showcrumbs($crumbs);
?>