PHP breadcrumb menu - replace link text

  • Zwirko
  • Guru
  • Guru
  • User avatar
  • Posts: 1417
  • Loc: 55° N, 3° W

Post 3+ Months Ago

Hi, I'm using a little bit of PHP to generate a breadcrumb menu.
The script is simple:
(Note that the variable $link is just the $_SERVER['HTTP_REFERER']; passed by the referring page.)

Code: [ Select ]
<?php
$url= "$link";
$parsed_url=parse_url($url);
$sub_folder = explode('/',$parsed_url['path']);
$domain=$parsed_url['scheme'].'://'.$parsed_url['host'];
$folder="";
$max=count($sub_folder);
for ($i=0;$i<$max;$i++)
{
  $folder.= ($max-1==$i)? $sub_folder[$i] : $sub_folder[$i].'/';
  $folder_url=  $domain.$folder;
  if (0==$i)
  {
    echo "<li><a href=\"$folder_url\">".$domain.' </a></li>';
  }
  else
  {
    echo "<li>&gt; <a href=\"$folder_url\">".$sub_folder[$i].' </a></li>';
  }
 
}
?>
  1. <?php
  2. $url= "$link";
  3. $parsed_url=parse_url($url);
  4. $sub_folder = explode('/',$parsed_url['path']);
  5. $domain=$parsed_url['scheme'].'://'.$parsed_url['host'];
  6. $folder="";
  7. $max=count($sub_folder);
  8. for ($i=0;$i<$max;$i++)
  9. {
  10.   $folder.= ($max-1==$i)? $sub_folder[$i] : $sub_folder[$i].'/';
  11.   $folder_url=  $domain.$folder;
  12.   if (0==$i)
  13.   {
  14.     echo "<li><a href=\"$folder_url\">".$domain.' </a></li>';
  15.   }
  16.   else
  17.   {
  18.     echo "<li>&gt; <a href=\"$folder_url\">".$sub_folder[$i].' </a></li>';
  19.   }
  20.  
  21. }
  22. ?>


It more or less works in that it creates the breadcrumb with all the correct links to the various directories. However, the link text is not very useful. At the moment it works like this:

referring page = http://localhost/pacific/cook/pukapuka.php

generated breadcrumb = http://localhost > pacific > cook > pukapuka.php

desired outcome = Home > Pacific > Cook Islands > Pukapuka

Any ideas how I can replace the directory names and filename with the section title and topic title? The necessary information is already being passsed to the script but I don't know how to replace the directory and filenames with that text while keeping the links working. The required text is already being passed to the script in the url. The text "Cook Islands" (the directory title) can be got at through the variable "$group_name" and the topic title (Pukapuka) from the variable "$place". Also note that if there is more than one word in "$group_name" or "$place" they are separated by a "+" eg "Cook+Islands".


I'm new to PHP so I hope I've used the right jargon and havn't confused anybody other than myself :D
Moderator Remark: Added [code] tags
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

What about...
PHP Code: [ Select ]
<?php
$url = "$link";
$parsed_url = parse_url($url);
$sub_folder = explode('/',$parsed_url['path']);
$domain = $parsed_url['scheme'].'://'.$parsed_url['host'];
$folder = "";
$max = count($sub_folder);
for($i=0; $i<$max; $i++)
{
 $folder .= ($max-1==$i)? $sub_folder[$i] : $sub_folder[$i].'/';
 $folder_url = $domain.$folder;
 if (0 == $i)
 {
   echo "<li><a href=\"$folder_url\"><span style="font-weight: bold">Home </span></a></li>";
 }
 else
 {
   echo "<li>&gt; <a href=\"$folder_url\">".$sub_folder[$i].' </a></li>';
 }
 
}
?>
  1. <?php
  2. $url = "$link";
  3. $parsed_url = parse_url($url);
  4. $sub_folder = explode('/',$parsed_url['path']);
  5. $domain = $parsed_url['scheme'].'://'.$parsed_url['host'];
  6. $folder = "";
  7. $max = count($sub_folder);
  8. for($i=0; $i<$max; $i++)
  9. {
  10.  $folder .= ($max-1==$i)? $sub_folder[$i] : $sub_folder[$i].'/';
  11.  $folder_url = $domain.$folder;
  12.  if (0 == $i)
  13.  {
  14.    echo "<li><a href=\"$folder_url\"><span style="font-weight: bold">Home </span></a></li>";
  15.  }
  16.  else
  17.  {
  18.    echo "<li>&gt; <a href=\"$folder_url\">".$sub_folder[$i].' </a></li>';
  19.  }
  20.  
  21. }
  22. ?>

The changed code is bolded...
  • Zwirko
  • Guru
  • Guru
  • User avatar
  • Posts: 1417
  • Loc: 55° N, 3° W

Post 3+ Months Ago

Thanks for looking.

I've changed things a little and have made some further progress but still have a few niggles here and there.

I'm now creating the menu like this:

Code: [ Select ]
<div id="menu">
<ul>
<?php gmv_menu(); ?>
<?php echo "<li><a href=\"$link\">$place</a> &gt; </li>"; ?>
<li>hi-res view</li>
</ul>
</div>
  1. <div id="menu">
  2. <ul>
  3. <?php gmv_menu(); ?>
  4. <?php echo "<li><a href=\"$link\">$place</a> &gt; </li>"; ?>
  5. <li>hi-res view</li>
  6. </ul>
  7. </div>



The gmv_menu function looks like this:


Code: [ Select ]
<?php
function gmv_menu(){
$trail = explode('/', $_SERVER['HTTP_REFERER']);
$url = '/';
$returnString = "<li><a href='$url'>Home</a> &gt;";
for($i=1;$i<count($trail)-1;$i++){
$url.=$trail[$i].'/';
$returnString .= "<li><a href='$url'>"
.tidy($trail[$i])."</a> &gt; ";
}
echo $returnString.'</li>';
}
function tidy($dir){
$directory['arctic']='Arctic Ocean';
$directory['atlantic']='Atlantic Ocean';
$directory['indian']='Indian Ocean';
$directory['pacific']='Pacific Ocean';
    $directory['aleutian']='Aleutian Islands';
    $directory['baker']='Baker Island';
    $directory['banaba']='Banaba';
    $directory['caroline']='Caroline Islands';
    $directory['chatham']='Chatham Islands';
    $directory['clipperton']='Clipperton Atoll';
    $directory['cook']='Cook Islands';
    $directory['coralsea']='Coral Sea Islands &amp; Reefs';
    $directory['daito-retto']='Dait&#333;-rett&#333;';
$directory['southern']='Southern Ocean';
if($directory[$dir]!=''){
$dir=$directory[$dir];
}
return $dir;
}
?>
  1. <?php
  2. function gmv_menu(){
  3. $trail = explode('/', $_SERVER['HTTP_REFERER']);
  4. $url = '/';
  5. $returnString = "<li><a href='$url'>Home</a> &gt;";
  6. for($i=1;$i<count($trail)-1;$i++){
  7. $url.=$trail[$i].'/';
  8. $returnString .= "<li><a href='$url'>"
  9. .tidy($trail[$i])."</a> &gt; ";
  10. }
  11. echo $returnString.'</li>';
  12. }
  13. function tidy($dir){
  14. $directory['arctic']='Arctic Ocean';
  15. $directory['atlantic']='Atlantic Ocean';
  16. $directory['indian']='Indian Ocean';
  17. $directory['pacific']='Pacific Ocean';
  18.     $directory['aleutian']='Aleutian Islands';
  19.     $directory['baker']='Baker Island';
  20.     $directory['banaba']='Banaba';
  21.     $directory['caroline']='Caroline Islands';
  22.     $directory['chatham']='Chatham Islands';
  23.     $directory['clipperton']='Clipperton Atoll';
  24.     $directory['cook']='Cook Islands';
  25.     $directory['coralsea']='Coral Sea Islands &amp; Reefs';
  26.     $directory['daito-retto']='Dait&#333;-rett&#333;';
  27. $directory['southern']='Southern Ocean';
  28. if($directory[$dir]!=''){
  29. $dir=$directory[$dir];
  30. }
  31. return $dir;
  32. }
  33. ?>


The gmv_menu function gives a nice breadcrumb through the directories BUT not to the referring page. The little php echo of $link and $place after the function creates the link back to the referring page within the last directory (the 2nd last item in the menu shown below).


End result:

Code: [ Select ]
Home > > localhost > Pacific Ocean > Cook Islands > Pukapuka > hi-res view



However, the big problem arises when the last directory the menu points to only has one file in it called index.php. In such a situation I end up with a breadcrumb that looks like this (note the repetition of "Baker Island" twice.)

Code: [ Select ]
Home > > localhost > Pacific Ocean > Baker Island > Baker Island > hi-res view


The first occurrence of "Baker Island" comes from the gmv_menu function printing out and linking the directory name (=good); the second occurrence comes from the echo of $link and $place (=bad).

It should look like this:

Code: [ Select ]
Home > > localhost > Pacific Ocean > Baker Island > hi-res view



What I'd like to do is tell the menu not to echo out the $link and $place if the file ends in "index.php"... or something.


I was thinking along the lines of:
Code: [ Select ]
if "$link" ends in "index.php", do not echo "<li><a href=\"$link\">$place</a> &gt; </li>"; else carry on as normal.


Is there a way to say that in php?
  • Zwirko
  • Guru
  • Guru
  • User avatar
  • Posts: 1417
  • Loc: 55° N, 3° W

Post 3+ Months Ago

Figured that last problem with;

Code: [ Select ]
<?php
if(strstr($link,'.php'))
echo "<li><a href=\"$link\">$place</a> &gt; </li> <li>hi-res view</li>";
else
echo "<li>hi-res view</li>";
?>
  1. <?php
  2. if(strstr($link,'.php'))
  3. echo "<li><a href=\"$link\">$place</a> &gt; </li> <li>hi-res view</li>";
  4. else
  5. echo "<li>hi-res view</li>";
  6. ?>



My last remaining problem is to get the first part of the breadcrumb looking good


I need to get from this:

Code: [ Select ]
Home > > localhost > Pacific Ocean > Baker Island > hi-res view


to this:


Code: [ Select ]
Home > Pacific Ocean > Baker Island > hi-res view



That is to remove the "> localhost" part of the breadcrumb.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

The problem occurs in the tidy function: Below is my test code:
Code: [ Select ]
<html>
    <head>
        <style type="text/css">
            ul, li {
                display: inline;
            }
        </style>
    </head>
    <body>

<?php
function gmv_menu()
{
    $trail = explode('/', $_SERVER['PHP_SELF']);
    $url = '/';
    $returnString = "<li><a href='$url'>Home</a> &gt; ";
    for($i=1; $i<count($trail)-1; $i++)
    {
        $url .= $trail[$i].'/';
        $returnString .= "<li><a href='$url'>" . $trail[$i]."</a> &gt; ";
    }
    echo $returnString.'</li>';
}
function tidy($dir)
{
    $directory['arctic']='Arctic Ocean';
    $directory['atlantic']='Atlantic Ocean';
    $directory['indian']='Indian Ocean';
    $directory['pacific']='Pacific Ocean';
        $directory['aleutian']='Aleutian Islands';
        $directory['baker']='Baker Island';
        $directory['banaba']='Banaba';
        $directory['caroline']='Caroline Islands';
        $directory['chatham']='Chatham Islands';
        $directory['clipperton']='Clipperton Atoll';
        $directory['cook']='Cook Islands';
        $directory['coralsea']='Coral Sea Islands &amp; Reefs';
        $directory['daito-retto']='Dait&#333;-rett&#333;';
    $directory['southern']='Southern Ocean';
    if($directory[$dir]!='')
    {
        $dir = $directory[$dir];
    }
    return $dir;
}
?>
<div id="menu">
<ul>
<?php gmv_menu(); ?>
<li>hi-res view</li>
</ul>
</div>
  1. <html>
  2.     <head>
  3.         <style type="text/css">
  4.             ul, li {
  5.                 display: inline;
  6.             }
  7.         </style>
  8.     </head>
  9.     <body>
  10. <?php
  11. function gmv_menu()
  12. {
  13.     $trail = explode('/', $_SERVER['PHP_SELF']);
  14.     $url = '/';
  15.     $returnString = "<li><a href='$url'>Home</a> &gt; ";
  16.     for($i=1; $i<count($trail)-1; $i++)
  17.     {
  18.         $url .= $trail[$i].'/';
  19.         $returnString .= "<li><a href='$url'>" . $trail[$i]."</a> &gt; ";
  20.     }
  21.     echo $returnString.'</li>';
  22. }
  23. function tidy($dir)
  24. {
  25.     $directory['arctic']='Arctic Ocean';
  26.     $directory['atlantic']='Atlantic Ocean';
  27.     $directory['indian']='Indian Ocean';
  28.     $directory['pacific']='Pacific Ocean';
  29.         $directory['aleutian']='Aleutian Islands';
  30.         $directory['baker']='Baker Island';
  31.         $directory['banaba']='Banaba';
  32.         $directory['caroline']='Caroline Islands';
  33.         $directory['chatham']='Chatham Islands';
  34.         $directory['clipperton']='Clipperton Atoll';
  35.         $directory['cook']='Cook Islands';
  36.         $directory['coralsea']='Coral Sea Islands &amp; Reefs';
  37.         $directory['daito-retto']='Dait&#333;-rett&#333;';
  38.     $directory['southern']='Southern Ocean';
  39.     if($directory[$dir]!='')
  40.     {
  41.         $dir = $directory[$dir];
  42.     }
  43.     return $dir;
  44. }
  45. ?>
  46. <div id="menu">
  47. <ul>
  48. <?php gmv_menu(); ?>
  49. <li>hi-res view</li>
  50. </ul>
  51. </div>

Run on:
http://127.0.0.1/New%20Folder/Another%20page/index.php

and the outcome is:
Home > New Folder > Another page > hi-res view

And that PHP file is the only file there is...

Maybe I misunderstood your problem though..
  • Zwirko
  • Guru
  • Guru
  • User avatar
  • Posts: 1417
  • Loc: 55° N, 3° W

Post 3+ Months Ago

Finally get things working. Thanks to those that helped or even just thought about it.

Final code:

Code: [ Select ]
<?php
function gmv_menu(){
$start_dir = 3;
$url = '/';
$trail = explode('/', $_SERVER['HTTP_REFERER']);
$returnString = "<li><a href=\"$url\">Home</a> &gt;</li>\n";
for($i=$start_dir;$i<count($trail)-1;$i++){
$url.=$trail[$i].'/';
$returnString .= "<li><a href=\"$url\">".tidy($trail[$i])."</a> &gt;</li>\n";
}
echo $returnString;
}
function tidy($dir){
$directory['localhost']='Home';
etc etc
?>
  1. <?php
  2. function gmv_menu(){
  3. $start_dir = 3;
  4. $url = '/';
  5. $trail = explode('/', $_SERVER['HTTP_REFERER']);
  6. $returnString = "<li><a href=\"$url\">Home</a> &gt;</li>\n";
  7. for($i=$start_dir;$i<count($trail)-1;$i++){
  8. $url.=$trail[$i].'/';
  9. $returnString .= "<li><a href=\"$url\">".tidy($trail[$i])."</a> &gt;</li>\n";
  10. }
  11. echo $returnString;
  12. }
  13. function tidy($dir){
  14. $directory['localhost']='Home';
  15. etc etc
  16. ?>


A bit of tidying up on the other part of the code has the thing producing nice valid html too.

If anyone wants to see the completed thing in action, then you can use this link - just click on the "hi-res view" link on the right under the "detailed imagery" section.

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 61 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.