Jump to content

[SOLVED] Setting a variable for certain folders


ahenson26

Recommended Posts

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

 

Link to comment
Share on other sites

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;
?>

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.