Sjak M
Sjak M

Reputation: 63

PHP Dynamic Breadcrumbs based on URL

I'm working on trying to get breadcrumbs to work correctly.

Right now they work, but the urls in the onclick are not correct.

<?php
//$url = strstr($_SERVER["REQUEST_URI"], 'admin');
$url = strstr('/content/modal/admin/content/polls.php', 'admin'); // admin/content/polls.php
$parse_url = preg_replace('/\.[^.\s]{3,4}$/', '', $url); // admin/content/polls

$array = explode("/", $parse_url);

$counter = 0;
foreach($array as $item) {
    echo '<a href="javascript:;"  onclick="initializemodal(\'' . $array[$counter] . '\')">' . $item . '</a> / ';
    $counter++;
}

This shows as:

<a href="javascript:;"  onclick="initializemodal('admin')">admin</a> / 
<a href="javascript:;"  onclick="initializemodal('content')">content</a> /
<a href="javascript:;"  onclick="initializemodal('polls')">polls</a> / 

The result I'm trying to archieve:

<a href="javascript:;"  onclick="initializemodal('admin')">admin</a> / 
<a href="javascript:;"  onclick="initializemodal('admin/content')">content</a> /
<a href="javascript:;"  onclick="initializemodal('admin/content/polls')">polls</a> / 

How can I edit my script so it works like the above example?

Upvotes: 1

Views: 1055

Answers (2)

Jeffrey
Jeffrey

Reputation: 1804

Couple of remarks

$counter = 0;
foreach($array as $item) {
    echo '<a href="javascript:;"  onclick="initializemodal(\'' . $array[$counter] . '\')">' . $item . '</a> / ';
    $counter++;
}

Foreach can be used like

foreach($array AS $index => $value)

Meaning your $counter is not needed, you can simply use $index.

Furthermore, you now have $counter++; as a single line, however you can combine it with the line above:

    ... initializemodal(\'' . $array[$counter++] . '\')"...

Do note that if you put the ++ behind the variable, it will increase AFTER the variable has been used.

$i = 0;
echo $i++; // This will print 0
echo $i++; // This will print 1
$i = 0;
echo ++$i; // This will print 1
echo ++$i; // This will print 2

To answer your question, you need to store the previous paths.

$path = []
foreach($array as $value) {
    $path[] = $value; // This array will first contain only one item, then two, then three, etc.
    echo '<a href="javascript:;"  onclick="initializemodal(\'' . implode('/', $path) . '\')">' . $item . '</a> / ';
}

implode will glue all values in $path. First there is nothing to glue, so it will result in admin. The second iteration it will glue admin with content, and adds a / between them. etc.

Upvotes: 1

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

You need to glue all elements up to current value of $counter. Right now you are just using what $counter points to, so you always get the last element, but not the whole "path". You basically need another loop like (not tested):

$counter = 0;
foreach($array as $item) {
    $tmp = [];
    for ($i=0; $i<=$counter; $i++) {
       $tmp[] = $array($i);
    }
    $path = implode('/', $tmp);
    echo '<a href="javascript:;"  onclick="initializemodal(\'' . $path . '\')">' . $item . '</a> / ';
    $counter++;
}

Upvotes: 0

Related Questions