'breadcrumbs for multi-level url

I want to create multi-level breadcrumbs, like if user go to Home->Step1->step2->step3 page then goes to Step4 : i want to see my breadcrumbs Home:->step1->step2-step3->->step4.

Here is my code :

<?php 
    $path = parse_url($_SERVER['HTTP_REFERER'],PHP_URL_PATH);
    $parts = explode('/',$path);
    $stepname='Step'.$step->step_code;
    if (count($parts) < 2)
    {
        echo("home");
    }
    else
    {
        for ($i = 2; $i < count($parts); $i++)
        {
            if (!strstr($parts[$i],"."))
            {
                echo("<a href=\"");
                for ($j = 0; $j <= $i; $j++) 
                {
                    echo $parts[$j]."/";
                };
                echo("\">". str_replace('-', ' ', $parts[$i])."</a> » ");
            }
        };
    };  
?>

but its show only Home->step3>step4 ,its not show step2 and step3. Actually I want multilevel breadcrumbs.



Solution 1:[1]

I recommend using Yii built in class CBreadcrumbs.

Besides that, you have logic error in your if statement. Home will show only if there are no other items.

Move if after echo home:

echo("home");

if (count($parts) > 2)
{
...

Solution 2:[2]

<?php
# Rewritten Ajmal PraveeN
    $path = parse_url($_SERVER['HTTP_REFERER'],PHP_URL_PATH);
    $parts = explode('/',$path);
    $stepname='Step'.$step->step_code;
    if (count($parts) < 2)
    {
        echo("home");
    }
    else
    {
        for ($i = 0; $i < count($parts); $i++)
        {
            if (!strstr($parts[$i],"."))
            {
                echo("<a href=\"");
                for ($j = 0; $j <= $i; $j++) 
                {
                    echo $parts[$j]."/";
                };
                echo("\">". str_replace('-', ' ', $parts[$i])."</a> » ");
            }
        }
    }
?>

The above code works fine with slash directories ex: If php explodes a path is /home/website/public or Infinite path levels, the script generates what you are looking for ;) Changes I made are $i = 2 to $i = 0 and A suggestion instead of str_replace use CSS styling for the »

Thank you..!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Ajmal Praveen