Jump to content

Get a variable from an Array

Recommended Posts

I am trying to write a small script to run a file based on the directory that it is in. I have tried several different ways of doing this and none of them are working?


I do not know what I am doing wrong and I am going insane.

$path_parts = pathinfo($_SERVER['REQUEST_URI']);
$path_parts = explode('/', $path_parts['dirname']);
$path_parts = array_filter($path_parts);
$dir = $path_parts[2];

echo $dir;

if (isset($_GET['page'])) {
    $page = $_GET['page'];
} else if ($dir = 'mail') {
    $page = 'inbox';
} else {
    $page = 'dashboard';

The above code is what I have come up with so far. 

Share this post

Link to post
Share on other sites

We don't know what your input values are.

We don't know what you are expecting from those values.

We don't know what "not working" means.


You might as well try "Alexa! What is the page value?"

Share this post

Link to post
Share on other sites

This doesn't look right:

} else if ($dir = 'mail') {

But, yeah, a tiny bit of information might help. Besides, those directory check gymnastics look very weird and fragile. Why can't you have a proper routing mechanism like with your other pages?

Share this post

Link to post
Share on other sites

This is my file structure


        - cms


             - mail



  • Using $_SERVER['REQUEST_URI'] in the index page. My Url will look like this www.example.dev/admin/mail/index.php?page=inbox
  • if the query string is not attached to the URL. It should automatically place the variable there based on the file being in the mail folder.
  • I am trying to get a variable for the mail folder of the URI. ($dir = $path_parts[2]).
  • If the directory equals mail. It opens the "inbox" portion of the page variable; if not it defaults to the "dashboard".

When I run the script that I currently get a blank page. The page does not get the "inbox" variable.


Share this post

Link to post
Share on other sites

Look, none of us is a mind reader, and we're not sitting in front of your screen. If you want help with your code, we need to actually see that code (not just random fragments), and it's your job to do basic debugging. Like: What is the value of $page after the code above?


Did you read my reply regarding the code error?


And your directory check still makes no sense. Have you symlinked/copied the exact same script into multiple directories? Why would you do that?

Share this post

Link to post
Share on other sites

Join the conversation

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

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.