ahenson26 Posted April 8, 2008 Share Posted April 8, 2008 I am new to PHP, so please go easy on anything I say that sounds newbish. I want to set a variable that controls which tab is "current" on the main horizontal navigation. Each section, other than the home page, is located in a folder, and all files for that section will be in the folder. For example: /index.php /products/ /products/page1.php /products/folder2/page3.php /about/ /about/history.php Right now, I am setting the current page with a variable "$n_id" on each page. However, I would like to use some kind of if or switch to set the $n_id automatically depending on the directory. So that if the file is / is under: /index.php --set $n_id = "home" /products/ (and any file under this folder) --set $n-id = "products" /about/ (and any file under this folder) --set $n-id = "about" There are no additional sections past the first level of folders. This seems like it might be a bit complicated and I haven't been able to correctly put it into words to search on Google. Thanks in advance for any help. Amanda Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted April 8, 2008 Share Posted April 8, 2008 So you just want the highest level folder? e.g. If someone was on page3.php, you would want $n_id to be equal to products? The following would assume the files are stored in the web root. If they're not, increase the values of 2 and 1 by the number of folders deep they are stored. <?php $location = $_SERVER["SCRIPT_NAME"]; $parts = explode('/',$location); if(count($parts) == 2){ $n_id = 'home'; }else{ $n_id = $parts[1]; } echo $n_id; ?> Quote Link to comment Share on other sites More sharing options...
discomatt Posted April 8, 2008 Share Posted April 8, 2008 Alternatively, without having to use arrays: <?php $cur = substr( pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_DIRNAME), 1 ); if ($cur == '') echo 'Home'; else echo ucfirst($cur); ?> Quote Link to comment Share on other sites More sharing options...
ahenson26 Posted April 8, 2008 Author Share Posted April 8, 2008 GingerRobot: Thanks! The code works perfectly in my global template, even several folders deep. I just took off the echo after testing it. How does the == 2 signify the index file in the root folder and the 1 any other folder? discomatt: I couldn't get this to work straight away, I think because I need the $n_id to be set automatically. This echos the correct name, but doesn't set $n_id. I changed it to: if ($cur == '') $n_id = "home"; else $n_id = $cur; And it worked, but only for the highest level folder and not any folders below it. Thanks again, Amanda Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted April 8, 2008 Share Posted April 8, 2008 The explode function splits the location string by forward slashes. If there's only one slash, that corresponds to the web root. Therefore, there are two pieces to the array (one either side of the slash - though the first is empty, since the slash is the first character). Therefore, with two pieces, the file we are viewing is in the root directory and we want 'home' If there are more than one forward slashes, there are more than two pieces. The piece we want is the 2nd piece (since indexing starts at 0, this is the element with key 1). Hopefully that makes more sense than I think it does! Quote Link to comment Share on other sites More sharing options...
ahenson26 Posted April 8, 2008 Author Share Posted April 8, 2008 I think I understand. With the == 2, the first piece is the / and the second is a file? So slash =1, file = 1, and slash + file = 2. Lol, does that make any sense? With the 1 --- Because of the indexing, it ONLY looks at the highest level folder when set to 1 (as opposed to 0, which would look at the root?) and that's why it works even several folders deep. If it were set to 2, then it would look two folders deep and ignore the highest level folders. I've just started going through tutorials and such intermittently over the past two weeks, so much of this is still a little above me. Thanks for the explanation. Amanda Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted April 8, 2008 Share Posted April 8, 2008 You might find it helpful for your understanding to print out the array created by the explode function: <?php $location = $_SERVER["SCRIPT_NAME"]; $parts = explode('/',$location); echo '<pre>'.print_r($parts,1).'</pre>'; if(count($parts) == 2){ $n_id = 'home'; }else{ $n_id = $parts[1]; } echo $n_id; ?> You'll then see you weren't quite correct - the forward slashes themselves are not elements of the array. Its every thing inbetween that is. Quote Link to comment Share on other sites More sharing options...
ahenson26 Posted April 8, 2008 Author Share Posted April 8, 2008 No slashes, eh? I was having a bit of a laugh on that with your correction. That does make sense now. Thanks! Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted April 8, 2008 Share Posted April 8, 2008 Haha. No problem, glad to help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.