i want the theme to display different elements for different urls,
for example, if the url is /a/* ,it prints a "hello, a"
if the url is /b/* ,it prints a "hello, b"
but how can i get the words "a" or "b" from the url?
is that a function?

Comments

pobster’s picture

You can use;

  print arg(1);

http://api.drupal.org/api/function/arg/6

Obviously that only works if you know which part of the url holds the information you want to display... If you don't then pay close attention to how arg creates the result (from the api page above).

Pobster

breaddawson’s picture

it doesn't work to Alias
for example, i have a link: /node/11
then i made an alias called /abc to it
then arg(0) just return node, not abc

pobster’s picture

...Hmmmm what does arg(1) return?

Pobster

breaddawson’s picture

------url-------alias----
/node/11 /abc

arg(0) return node
arg(1) return 11

pobster’s picture

Okay;

  print $_GET['q'];

What does that return?

...Incidentally... When you're running the previous print statement, are you trying it from both /node/11 and /abc? You are able to access both...

Pobster

dman’s picture

arg() will always return the system path (node/n)
that makes it actually useful for coders, no matter what aliases have been doing.

$_REQUEST['q'] and similar will still have the aliased versions.
You'll have to split() it yourself or something probably.

.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/

tormu’s picture

$path_pieces = explode('/', $_GET['q']);
print_r($path_pieces);

so if your url is http://www.example.com/qwerty, then $path_pieces[0] would print "qwerty"

breaddawson’s picture

the function explode() return an array?
i tried ur code and ouput $path_pieces ,then i get an array
when i output $path_pieces[0], i still get "node"
maybe it's because /node/11 is the real address?
when drupal get /abc it then alter to /node/11...

omg....the alias confused me...

pobster’s picture

That's not helpful... Follow the thread, the Drupal function arg() uses that very same $_GET['q'] with explode to create its result. It's the alias he's after which I'm wondering whether he's actually set up correctly or not?...

A more helpful post would have been to just get him to use drupal_get_path_alias...

Pobster
edit: Okay so you replied before I did... Really... Just ignore that post...

pobster’s picture

Just try using;

  print drupal_get_path_alias($_GET['q']);

It's not entirely ideal... But then I'm not entirely sure exactly what you're trying to achieve. Anyways, it'll certainly tell me whether you've set the alias up correctly or not...

Pobster

breaddawson’s picture

it worked!
drupal_get_path_alias($_GET['q']) just return the alias of the path!
thank u very much for your help!!

tormu’s picture

hehe, I was about to put $_SERVER['REQUEST_URI'] instead of $_GET['q'] to my example code, but then read your post about it and $_GET['q'] gave the same thing - guess it was just a bad test node :)
$_SERVER['REQUEST_URI'] should always return the path, like when writing this it would print "comment/reply/227849/750216" (not sure about the leading slash though) - maybe that is also faster, though of course we're talking about fractions of milliseconds on getting the alias from the database.

breaddawson’s picture

thank u for your reply...
but my situation is that ,i want to get part of the path, for example, the url is /abc/def
i just want to get abc
can $_server['request_uri'] do this for me?
and....u mean $_GET['q'] can not always return the right value?

pobster’s picture

I was steering clear of that since it's only available with Apache. When giving advice for code you should never assume anything.

The code I gave using $_GET['q'] will work with any web server and whilst being a slightly slower way to do it, it's more compatible should someone be searching for this issue and they're using something else...

Pobster

tormu’s picture

Ok, then just combine two replies (http://drupal.org/node/227849#comment-750198 and http://drupal.org/node/227849#comment-750216) together.

  //explode() will split the given string with another string and put the pieces into an array
  $path_pieces = explode('/', drupal_get_path_alias($_GET['q']));

  //so if your path is /abc/def, $path_pieces is now like Array ( [0] => abc [1] => def )

  //Therefore, $path_pieces[0] prints 'abc' that you wanted
  print $path_pieces[0];

Hope we covered it this time ;)

Rob T’s picture

I was able to use this well. Thanks for the snippet.

summit’s picture

Could this peace of code also be used in node-body, to have a sort of solution like a template.
So have normal tekst and because of the url, have some text inserted.
Example:
url is www.example.com/economics

This is a example text about [peace of code]

The node should than be:

This is a example text about economics

This is working, great! Any issues with this may be?

greetings,
Martijn

kevinquillen’s picture

$_SERVER['SCRIPT NAME']; will do this as well.

Trust me, I do this on all our websites to display certain elements in certain sections or pages. It works.

===========
read my thoughts

summit’s picture

Could you give an example please of using $_SERVER['SCRIPT_NAME']; ?
Thanks in advance,
greetings,
Martijn

kevinquillen’s picture

ok:

<?php $thisPageURL = str_replace("q=", "", $_SERVER['QUERY_STRING']); $thisPageURL = explode("/", $thisPageURL); ?>

To see what it grabs, do print_r($thisPageURL);. Explode puts each word into an array.

edit: sorry, i meant QUERY STRING and not SCRIPT NAME

===========
read my thoughts

dman’s picture

i meant QUERY STRING and not SCRIPT NAME

... um, yeah. I was really starting to wonder how you'd be getting THAT to work ;-)

.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/