By breaddawson on
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
You can use; <?php print
You can use;
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
it doesn't work to Alias for
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
...Hmmmm what does arg(1)
...Hmmmm what does arg(1) return?
Pobster
------url-------alias----
------url-------alias----
/node/11 /abc
arg(0) return node
arg(1) return 11
Okay;
Okay;
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
arg() will always return the
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/
.dan. is the New Zealand Drupal Developer working on Government Web Standards
Q
so if your url is http://www.example.com/qwerty, then $path_pieces[0] would print "qwerty"
the function explode()
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...
That's not helpful...
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...
Just try using;
Just try using;
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
it
it worked!
drupal_get_path_alias($_GET['q']) just return the alias of the path!
thank u very much for your help!!
$_SERVER['REQUEST_URI'] instead of $_GET['q']
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.
thank u for your
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?
I was steering clear of that
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
combining the reply advices
Ok, then just combine two replies (http://drupal.org/node/227849#comment-750198 and http://drupal.org/node/227849#comment-750216) together.
Hope we covered it this time ;)
I was able to use this well.
I was able to use this well. Thanks for the snippet.
Could this peace of code
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
The node should than be:
This is working, great! Any issues with this may be?
greetings,
Martijn
$_SERVER['SCRIPT NAME'];
$_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
Could you give an example
Could you give an example please of using $_SERVER['SCRIPT_NAME']; ?
Thanks in advance,
greetings,
Martijn
ok: <?php $thisPageURL =
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
i meant QUERY STRING and not
... 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/
.dan. is the New Zealand Drupal Developer working on Government Web Standards