If you're in this folder then display this

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

    else {
    $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!


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


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
        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 {

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

elseif ($dir == "$rpath/dhd") {

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


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!";

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") {

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

thank you! - Val


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:

    $dir = getcwd();
    if ($dir == "$rpath/blend") {
    elseif ($dir == "$rpath/dhd") {
    elseif ($dir == "$rpath/eat") {
    elseif ($dir == "$rpath/hydro") {
    elseif ($dir == "$rpath/juice") {
    elseif ($dir == "$rpath/sprout") {
    elseif ($dir == "$rpath/water") {
    else {

I placed your else here:

else {
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):

     if (file_exists("./root.php")) {
     $rpath = ".";}
     else {
     $rpath = ".."; }

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.

Thinking about it, the bit of script that is writing $rpath is likely causing problems.
Rather than using ../ to find your root, use includes like this:

include $_SERVER["DOCUMENT_ROOT"] . "/mypath/myscript.php";

you mean like this?

if ($dir == $_SERVER["DOCUMENT_ROOT"] . "/blend") {
    include $_SERVER["DOCUMENT_ROOT"] . "/1cde/amznbl.php";

Possibly not this bit:

($dir == $_SERVER["DOCUMENT_ROOT"] . "/blend")

but certainly the second line.
Try throwing a very simple test into your directory and run it.

echo '<p>CWD: '.getcwd().'</p>';
echo '<p>Docroot: '.$_SERVER["DOCUMENT_ROOT"].'</p>';

Once you know what they both output, you know what you need to do to make them match up.

$dir = str_replace('bits of path you dont want', '', getcwd() ) ;

Hi - the problem with this solution is that I must have multiple files “amzn*.php”. And if I make a small change in one file, I have to change them all!

I’m trying a different solution now with:

where I can put the search phrases for each folder in a functions.php and have just this on the main page:

<?php echo getPrase(); ?>

will keep you posted…

Hi Val. That link take me to the profile of someone. Was it meant to link to a thread giving you an answer to your issue?

Hi no - that’s the guy I’m paying $35 for the solution. It’s really neat. It’s working great. Will ask his permission to post it here. He’s giving it to me by email not online.

1 Like

Hi – here’s the solution. It works perfectly. Now I can change things like search phrase and amzn_assoc_title in one file, namely, functions.php, instead of having to change things individually in multiple files.

I was lucky, I was able to simply add it to my wordpress functions.php so I never had to add anything to the top of files. Almost all my pages are NOT wordpress, but they all call up the wp environment with:
<?php require '../wp-blog-header.php';?>
so I can use a wordpress testimonials plugin on non-wordpress pages.

So if you have wordpress functions.php, you just add the functions.php code below to it, and if you don’t have wordpress, then you create a functions.php in root.

From Andrew at http://www.phphelp.com/forum/index.php?action=profile;u=72272:

Install functions.php in the root directory (if you don’t have wordpress).

Add the following line to the top of the files where you will be using it:

<?php include $_SERVER['DOCUMENT_ROOT'] . '\functions.php'; ?>
//not needed if you’re using wordpress functions.php

In those same pages, where you currently have the amazon JavaScript code, remove it and add the following:

<?php echo getPhrase(); ?>

here’s the file for functions.php:

function getPhrase() {
    $phrase = array (
            'default' => 'sprouters',
            'blend' => 'countertop blenders',
            'dhd' => 'dehydrators',
            'eat' => 'raw food',
            'hydro' => 'hydroponic towers',
            'juice' => 'auger juicers',
            'sprout' => 'sprouters',
            'water' => 'alkaline water ionpod', 
    $amazonTemplate = <<<TXT
    <script type="text/javascript">
    amzn_assoc_placement = "adunit0";
    amzn_assoc_tracking_id = "gogreen047-20";
    amzn_assoc_ad_mode = "search";
    amzn_assoc_ad_type = "smart";
    amzn_assoc_marketplace = "amazon";
    amzn_assoc_region = "US";
    amzn_assoc_textlinks = "";
    amzn_assoc_linkid = "";
    amzn_assoc_default_search_phrase = "{{phrase}}";
    amzn_assoc_title = "&nbsp;&#62;&#62;&nbsp;Find Today's Top Bargains in Appliances &amp; Raw Foods!";
    $uri = explode ( '/' , $_SERVER ['REQUEST_URI'] );
    $interest = $uri [count ( $uri ) - 2];
    $search = '';
    if (array_key_exists ( $interest, $phrase )) {
        $search = $phrase[$interest];
    } else {
        $search = $phrase['default'];
        return str_replace('{{phrase}}', $search, $amazonTemplate);

cool, eh? To see it in action, go to http://greensmoothie.com/ and click on any of the dropdowns from “Eating Raw” on (ignore Free Stuff & Shop), scroll to bottom of a page and you’ll see in each folder (eat, sprout, blend, etc) it has a different search phrase and thereby displays amazon products that are unique to that folder (raw food, sprouters, blenders, etc).

thanks for everyone’s input! – Val

1 Like

@valarcher Many thanks for sharing your solution with us. Glad you got a resolution.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.