Hello, I'm trying to create links on my website which depend on the folder you are in. Like for instance, if I'm at mysite/somepage.php the link would just be index.php and if I'm at mysite/anyfolder/somepage.php the link would be ../index.php. I know I can call the url like this: $url= $_SERVER['PHP_SELF']; But then ? It would be nice if you could take all the text in betwoon two slashes. Like this: let's say your url is mysite/folder/subfolder/page.php and you would be able to split this url into mysite, folder, subfolder and page.php. Is there a command to do this or a work around? Thx in advance,,
Yes this is very easy to split a path into its folders or parts. You have to just use php explode function and you are done. In this case code will be. $path="mysite/anyfolder/somepage.php"; $pathAry= explode("/",$path); //Now above statement will remove slash from path and save each part of text // in between as an array element like following //$pathAry[0] contains "mysite" //$pathAry[1] contains "anyfolder" //and so on. PHP: You can use this function for exploding on base of more than one character. Following link explains it all http://codingtricks.blogspot.com/2008/02/seprate-string-into-array-of-strings-on.html Hope it is helpful.
You can also do this in Javascript, see following link http://codingtricks.blogspot.com/2008/02/seprate-string-into-array-of-strings-on_08.html
Since it's still a forum I'm gonna post my code here to help other people,, Following code is a php file which is being included in all my other pages, I think it's an easy way to manage all your links at once. I use this like a 'top' on my page with a few quick links, news items.. <body> <? function setPath($url){ $path = $_SERVER['PHP_SELF']; $seperated = explode("/",$path); $size = count($seperated); $url_up = "../" . $url; $present = false; for($i=0; $i<$size; $i++){ if(($sep[$i] == "poll") || ($sep[$i] == "admin")){ $present = true; break; } else{ $present = false; } } if($present == true){ echo($url_up); } else{ echo($url); } } ?> <ul> <li><a href="<? setPath("cg_poll.php#poll_2"); ?>" target="_self" class="item_top">» Poll, Party</a></li> <li><a href="<? setPath("cg_poll.php");?>" target="_self" class="item_top">» Test poll added</a></li> <li><a href="<? setPath("index.php#news?");?>" target="_self" class="item_top">» Site online</a></li> </ul> </body> Code (markup): How it works: *The variable $url_up simply takes the path up one folder by adding "../" to the url. You can easily change this variable - let's say to variable $url_down - so it takes you one folder down by adding "foldername/" to it. If you do that, echo($url_up) needs to be echo($url) and echo($url) needs to be echo($url_down). *Read previous posts in this topic, then you'll understand that if(($sep[$i] == "poll") || ($sep[$i] == "admin")) means I'm in the poll or admin folder on my site. So for people with other folders, change/add condition here. *In your actual html code (under href) the php function is being called. Hoop this makes sense,,