If you're in this folder then display this

Hi - I don’t know php but I used to have really good code that said:

if you (the browser) are in the folder “root/juice” then display “text1.php” here at this point in the content
if you’re in the folder “root/blend” then display “text2.php”
etc.

At a specific point in the content, it checks what folder you’re in, then displays unique text there.

All folders are 2nd level one down from root.

please does anyone know how to do this?

I already have at the top of each page a check to see which folder it’s in relative to root (with blank file root.php in root):

<?php if (file_exists("./root.php")) { $rpath = ".";} else { $rpath = ".."; } include("$rpath/1cde/header.php"); ?>

But this time I need the php to say “if you’re in the folder /juice
right now, then add this text here at this point in the content.”

thanks! Val

Try the following function and notice the return value:

echo getcwd();

Hi John - no it’s much longer code, I wish I could find it on my hard drive! I need the full code that says:

if you’re in the /juice folder 2nd level down right now, then print this text.php at this location in the content.

Please are you able to give me the full code for that?

I don’t know the code you originally had, but thought, using John’s suggestion, something like this would work.

$dir = getcwd();
if ($dir == "/root/this") {
  // Do This
}
elseif ($dir == "/root/that") {
   // Do That
}
else {
   //Do Other
}
1 Like

Hi Sam - wow thanks million, this is exactly what I’m looking for! you save me once again :slight_smile: My mobile site is working beautifully now at http://greensmoothie.com – thank you to you and Paul.

Is this how the final should look? Or is it “then include(”$rpath/…" – does there need to be a “then” somewhere to go with the “if”?

<?php
     if (file_exists("./root.php")) {
     $rpath = ".";}
     else {
     $rpath = ".."; }
include("$rpath/1cde/header-with-doctype-etc.php");
?>
<head>
<title>*</title>
</head>
<body>
<p>content</p>

<p>
<?php
    $dir = getcwd();
    if ($dir == "$rpath/blend") {
      include("$rpath/1cde/file01.php");
    }
    elseif ($dir == "$rpath/water") {
       include("$rpath/1cde/file02.php");
    }
    else {
       include("$rpath/1cde/file-generic.php");
    }
?>

</p>
<p>content</p>
</body>
</html>

thank you! - Val

It looks OK, you will have to try it.

No. The statements within the “curly” brackets should run if the condition in the proceeding brackets is true.
The elseif is a second condition to test for, if the initial if is not met. You can have any number of alternative elseif conditions following an if.
Finally the else is the fallback if none of the above conditions are met.
No need for any thens.

Hi - there’s a problem. It’s not recognizing this:

    $dir = getcwd();
    if ($dir == "$rpath/blend") {

whatever folder it’s in, it’s just doing the same thing, namely this:

else {
        include("$rpath/wp-content/themes/1green/1cde/amzngen.php");
    }

can you perhaps see an error?

Hi Val, have you tried

$echo $dir;

? It could help to debug this…

echo $rpath

too, they need to match, but clearly don’t.

sorry how do I do that? I tried ending it like this:

    else {
        include("$rpath/wp-content/themes/1green/1cde/amzngen.php");
    }
    $echo $dir;
    $echo $rpath;
?>

but that doesn’t work…

The $rpath is definitely correct, it’s how I call up all my includes in all files in all subfolders and they’re all working!

Everything looks 100% correct here…

That’s one long variable name!

Try

include "{$rpath}/wp-content/themes/1green/1cde/amzngen.php";

or

include $rpath . "/wp-content/themes/1green/1cde/amzngen.php";

* include isn’t a function, no parentheses needed.

If you do not know how to check your PHP error-log then try adding this DEBUG script and take note of the error messages.

    else {
// START DEBUG: display error messages
        error_reporting(-1); 
        ini_set('display_errors', true );
        $echo '$dir ===  ' .$dir .'<br>'; 
        $echo '$rpath ===  ' .$rpath  .'<br>'; 
// FINISH DEBUG: display error messages

      // require thows an error if the file does not exist
       require "$rpath/wp-content/themes/1green/1cde/amzngen.php" ;
    }

Hi John - thank you! I tried adding that debug at the end but get this error msg:

" Parse error: syntax error, unexpected ‘else’ (T_ELSE) in C:\wamp\www\1gn\wp-content\themes\1green\1cde\btmad.php on line 40"

Line 40 is the start of the debug script.

Hi Mittineague - the problem is include works across the board, e.g. this:

<?php include("$rpath/1cde/naveat.php"); ?>

will display that file naveat.php in the browser. And this last line:

else {
        include("$rpath/wp-content/themes/1green/1cde/amzngen.php");
    }

is working. So it’s understanding $rpath. But it’s all the elseif lines that aren’t working:

elseif ($dir == "$rpath/dhd") {
        include("$rpath/1cde/amzndhd.php");
    }

It’s not seeing it’s in /dhd when it IS there. Here’s a sample:

http://greensmoothie.com/dhd/1mst.php

It should recognize it’s in /dhd and display $rpath/1cde/amzndhd.php which is an amazon script like this:

<script type="text/javascript">
amzn_assoc_default_search_phrase = "dehydrators";
amzn_assoc_title = "&nbsp;&#62;&#62;&nbsp;Find Today's Top Bargains in Dehydrators!";
</script>

Instead it’s displaying the final else $rpath/wp-content/themes/1green/1cde/amzngen.php which is an amazon script like this:

amzn_assoc_default_search_phrase = "sprouters";
amzn_assoc_title = "&nbsp;&#62;&#62;&nbsp;Find Today's Top Bargains in Appliances &amp; Raw Foods!";

You see how it’s ignoring:

$dir = getcwd();
    if ($dir == "$rpath/dhd") {
        include("$rpath/1cde/amzndhd.php");

The file amzndhd.php is definitely in root/1cde!

thank you! - Val

@valarcher

Hi John - thank you! I tried adding that debug at the end but get this error msg:

Only the code starting and finishing with DEBUG was to be inserted into your else statements.

If you do not know where to insert the DEBUG script then please supply the full file so we can insert the script in correctly. You will then be able to copy & paste the results.

Hi John - here it is:

<?php
    $dir = getcwd();
    if ($dir == "$rpath/blend") {
        include("$rpath/1cde/amznbl.php");
    }
    elseif ($dir == "$rpath/dhd") {
        include("$rpath/1cde/amzndhd.php");
    }
    elseif ($dir == "$rpath/eat") {
        include("$rpath/1cde/amzneat.php");
    }
    elseif ($dir == "$rpath/hydro") {
        include("$rpath/1cde/amznhyd.php");
    }
    elseif ($dir == "$rpath/juice") {
        include("$rpath/1cde/amznjcr.php");
    }
    elseif ($dir == "$rpath/sprout") {
        include("$rpath/1cde/amznspr.php");
    }
    elseif ($dir == "$rpath/water") {
        include("$rpath/1cde/amznwf.php");
    }
    else {
        include("$rpath/wp-content/themes/1green/1cde/amzngen.php");
    }
?>

I placed your else here:

else {
            include("$rpath/wp-content/themes/1green/1cde/amzngen.php");
        }
else {
// START DEBUG: display error messages...
    ?>

thank you!

What is the value of $rpath?

Please note, I am not using a desktop at the moment and would appreciate if anybody else can test this script.

value is set at the top of each file (root.php is a blank file in root):

<?php
     if (file_exists("./root.php")) {
     $rpath = ".";}
     else {
     $rpath = ".."; }
include("$rpath/1cde/header-with-doctype-etc.php");
?>

You can only have one else as the final alternative condition.
So you always start with an if, which may be optionally followed by any number of elseifs, optionally followed by a single else for the case where none of the above conditions are met.
You cannot have:

if (condition 1) {}
elseif (condition 2) {}
elseif (condition 3) {}
else {}  // None of the above
else {} // This dose nothing and will give an error!

I see, makes sense! but then I can’t use the debug because I need amzngen.php to display for all other sub-folders.

Then put the debug in the final else or possibly out of any brackets.
You need to see the result of getcwd() to see how it differs from your path string. It may be giving you a full path that needs trimming.