-4

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>\n<li><a href=\"/\">Home</a>&nbsp;&nbsp;&gt;</li>\n");
foreach ($crumbs as $atext => $aloc) {
    if ($dotname != 'index.php' && $aloc == end($crumbs)) {
        $url .= '/'.$aloc;
        echo("<li><a href=\"$url\">$atext</a></li>\n");
    } else {
        $url .= '/'.$aloc;
        echo("<li><a href=\"$url/\">$atext</a>&nbsp;&nbsp;&gt;</li>\n");
    }
}
echo('</ul>');
?>
Irfanullah Jan
  • 3,336
  • 4
  • 24
  • 34
  • 1
    This isn't "how to solve a problem" but "show me codez" type of question. All I can say is that you'll need foreach and a temporary string which will contain previous breadcrumbs. – Dalibor Filus Dec 11 '11 at 12:42
  • 1
    What have you tried already? Please post your existing code, even if it is broken. Let us know which particular part you're getting stuck on and we'll help you fix that. – Merlyn Morgan-Graham Dec 11 '11 at 12:46
  • 1
    And it looks like another kid gets away with letting someone else do their homework for them. For free no less... – Merlyn Morgan-Graham Dec 11 '11 at 12:50
  • 1
    Seems you need to learn coding first. Did you even try any thing? if yes, show the code. May be we can fix it. – Shiplu Mokaddim Dec 11 '11 at 12:54
  • It's kinda funny. There are 5 answers and only one got it right. :D Score-hunters! :D – Dalibor Filus Dec 11 '11 at 13:08

5 Answers5

1

I'd go with a foreach and a variable to remember the path. I'll whip up an example in a few seconds.

A Working Can be Found Here!

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • 4
    Please don't, let him struggle with that. – Joost Dec 11 '11 at 12:44
  • Your solution helped me a lot. Thank you so much. I modified it a little bit to suit my site structure because it does not have trailing slashes in deep pages. – Irfanullah Jan Dec 12 '11 at 03:27
  • Sure thing. Make sure to read the manual next time, and maybe consider taking a few lessons. What you asked is a very basic string manipulation. @erfan – Madara's Ghost Dec 12 '11 at 14:59
1

Somthing like that?:

<ul>
<?php
foreach($crumb as $name=>$href){
   echo "<a href=/'$href'>$name</a>"; 
}

?>

josegil
  • 365
  • 1
  • 8
1
<ul>
<li><a href="/">Home</a></li>
<?php
$path = '/';
foreach($crumbs as $name => $href) {
    echo '<li><a href="'.$path.$href.'/"></li>';
    $path .= $name.'/';
}
?>
</ul>
haynar
  • 5,961
  • 7
  • 33
  • 53
1

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);
?>
Mohammed Shannaq
  • 806
  • 3
  • 8
  • 21
0
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);
?>