Gossamer Forum
Home : General : Perl Programming :

need a perl (or php) script to display the path

Quote Reply
need a perl (or php) script to display the path
I'm looking for a perl or php script to print out the path of the page you're on. You know how some sites show you exactly where you are in their site, like this?


Home | Catalog | Category | etc...

Does anyone here know of such a script?

Thanks!

Quote Reply
Re: need a perl (or php) script to display the path In reply to
You could adapt the following codes from Links 2.0:

Code:

sub build_linked_title {
# --------------------------------------------------------
# Returns a string of the current category broken up
# by section, with each part linked to the respective section.

my $input = shift;
my (@dirs, $dir, $output, $path, $last);

@dirs = split (/\//, $input);
$last = &build_clean(pop @dirs);

$output .= qq| <TABLE BORDER=\"0\" WIDTH=\"80%\" CELLPADDING=\"0\" CELLSPACING=\"0\">|;
$output .= qq| <TR><TD VALIGN=\"TOP\"><FONT FACE=\"Verdana\" SIZE=\"2\"><B><A HREF="$build_root_url/">Home</A> :</FONT></B>|;
foreach $dir (@dirs) {
$path .= "/$dir";
$dir = &build_clean ($dir);
$output .= qq| <FONT FACE=\"VERDANA\" SIZE=\"2\"><B><A HREF="$build_root_url$path/">$dir</A> :</B></FONT>|;
}
$output .= qq|</TD></TR></TABLE><TABLE BORDER=\"0\" WIDTH=\"80%\" CELLPADDING=\"0\" CELLSPACING=\"0\">|;
$output .= qq|<TR><TD VALIGN=\"TOP\" BGCOLOR=\"FFCC00\"><FONT FACE=\"Arial\" SIZE=\"3\"><B>$last</B></FONT></TD></TR></TABLE>|;

return $output;
}


Then in your script...use these codes:

Code:

&build_linked_title ("Something/Category/Something");


Of course, there would be tons of other tweaks that you would have to apply...but this gives you an idea, I hope.

Although I believe there are some powerful solutions with PHP...you might want to check out the documentation at:

http://www.php3.com

A good place to get code snippets and find out more about PHP is at the following web page:

http://www.php3.com/links.php

Regards,

Eliot Lee

Quote Reply
Re: need a perl (or php) script to display the path In reply to
here is a little script to work based on what directory the file is in...

Code:
#!/usr/bin/perl

@dirs = split /\//, $ENV{DOCUMENT_URI};
shift @dirs; pop @dirs;

print "Content-type: text/html\n\n";
print qq~<a href="/"><b>Home</b></a> : ~;
print join " : ", map {
$path .= "/$_";
$_ = qq~<a href="$path/"><b>~ . join (" ", map { lc; ucfirst } split (/[\s\_\-\+]+/)) . qq~</b></a>~;
} @dirs;
put it into your root www directory..

and by adding this to files....



it will return the path of the page you are on..

it formats directory names.. like http://www.domain.com/category/sub_category/

would turn into..

Home : Category : Sub Category (all linked of course)..

Jerry Su
Quote Reply
Re: need a perl (or php) script to display the pat In reply to
Thank you so much! It fits my need perfectly.

I appreciate your help!

DH :-)

Quote Reply
a little fix....? In reply to
Jerry Su,

Again, many thanks for the script. How should I modify the script to not hyperlink the current page. For instance, if the output of the script is:

Home : Category : Sub Category

then I'd like the home and category to be links, but not the last one...the sub category. That way users won't be able to click on it and just load the current page again.

Thanks!

DH :-)

Quote Reply
Re: a little fix....? In reply to
In Reply To:
Again, many thanks for the script. How should I modify the script to not hyperlink the current page. For instance, if the output of the script is:
Yes please...I could go for that also Jerry...and thank you in advance. Regardless of Your answer or not you have proven to be one of the TRUE programmers and not one of the cut-n-paste artists who claim they are programmers which includes idiot ......

thank you

easy does it
Quote Reply
Re: a little fix....? In reply to
This script does the same kind of thing:

http://www.alug.org/free_stuff/path.txt

--
http://www.webarchitects.co.uk/
Quote Reply
Re: a little fix....? In reply to
hahaha.. i've never seen that.. looking at that guys code i put his 8 line sub raw_mod into half a line in my script.. which proves map is probably the most powerful function of perl.. yet.. not many people use it.. oh well.. i'm using my knowledge from perlschool.. :) i passed with 97% total 99% on final.. woohoo..

anyways.. here is the code.. i forgot about that..

Code:
#!/usr/bin/perl
@dirs = split /\//, $ENV{DOCUMENT_URI};
shift @dirs; pop @dirs; $last = pop @dirs;
print "Content-type: text/html\n\n";
print qq~<a href="/"><b>Home</b></a> : ~;
print join " : ", map {
$path .= "/$_";
$_ = qq~<a href="$path/"><b>~ . join (" ", map { lc; ucfirst } split (/[\s\_\-\ ] /)) . qq~</b></a>~;
} @dirs;
print qq~ : <b>~ . map { lc; ucfirst } $last . qq~</b>~;
Jerry Su
Quote Reply
Re: a little fix....? In reply to
map is definitely a powerful command. but in this case, a substitution will run much faster and with less code:

my $p = $ENV{'REQUEST_URI'};
my $dr = '<your root URL>';
$p =~ s!([^/]+)/!{$dr .= "/$1"; print qq~<a href="$dr">~.ucfirst(lc($1)).qq~</a> :~;}!ge;

I ran it through a benchmark with 10 thousand iterations and got these results:

Benchmark: timing 10000 iterations of Map, Subs...
Map: 8 wallclock secs ( 8.18 usr + 0.25 sys = 8.43 CPU)
Subs: 1 wallclock secs ( 0.04 usr + 0.00 sys = 0.04 CPU)
(warning: too few iterations for a reliable count)

-- Gordon


s/(\d{2})/chr($1)/ge + print if $_ = '8284703280698276687967';