Link issue

Discussion in 'PHP' started by fex, Sep 30, 2008.

  1. #1
    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,,
     
    fex, Sep 30, 2008 IP
  2. khu84

    khu84 Active Member

    Messages:
    441
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #2
    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. :)
     
    khu84, Sep 30, 2008 IP
  3. fex

    fex Peon

    Messages:
    89
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It's exactly what I needed, thx for your fast help mate! ;)
     
    fex, Sep 30, 2008 IP
  4. khu84

    khu84 Active Member

    Messages:
    441
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #4
    khu84, Sep 30, 2008 IP
  5. khu84

    khu84 Active Member

    Messages:
    441
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #5
    You are welcome :)
     
    khu84, Sep 30, 2008 IP
  6. fex

    fex Peon

    Messages:
    89
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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,, :D
     
    fex, Sep 30, 2008 IP
  7. khu84

    khu84 Active Member

    Messages:
    441
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #7
    Fex its a good piece of work, Keep it up.
     
    khu84, Sep 30, 2008 IP