0

I want to include a file, based on a value I get from my database. Inside class page in page.php I have this function to get the data I want :

function getPage(){
    $page=array(
      'title'=>$this->title,
        'content'=>$this->content,
        'module'=>$this->module
    );
    return $page;
}

And in pageview.php I call it like this : $elements=$page->getPage(); The function works just fine and I get my content , which I can manipulate like this for example(I know heredox is not the way to go but please ignore it, it's not the problem) :

 echo <<<EOT
    <div class='row-fluid'>
    <H1> title:$elements[title]</H1></br>
    $elements[content]
    $elements[module]
    </div>
EOT;

Now here comes the problem. I have a function called includeModule, which is like this :

function includeModule($page){

    $page=strtolower($page);

    if(file_exists("modules/".$page."php")) include("/modules/".$page."php");
    else Echo "No such Module Exists :".$page." </br>";

}

Now lets say I want to include a page named "tax.php". If I use include("/modules/tax.php)"; it works just fine. If I try though to use include("/modules/".$elements[module].".php)"; it does nothing ( an yes $elements[module] does contain only the word "tax" ). I even tried assigning the value of $elements[module] to another variable but nothing.

The strangest part of all is that if I try to use my function ( includeModule), even if I manually set the $page variable to "tax", it still doe not include anything and says that the file does not exist ( even though it does) ?

Any help on this ?

edit: i already tried removing the / but if i do it includes nothing again

edit: i've change the function a litle so i can get some feedback

if(file_exists("modules/".$page.".php")){
        echo "<p>file exists</p>";
        include("modules/".$page."**.**php");
    }else{
        echo getcwd();
         Echo "<p>No such Module Exists :".$page."</p>";
    }

OK solved. thanks for all the answers. By my mistake the trailing slash as well as the dot right before the php extension were left out . thanks for helping :)

10
  • 1
    You're using relative paths so it's going to depend on the current working directory to find your files (in addition to your include_path). You can check to make sure it's correct and what you're expecting by using the getcwd() function. Commented Jan 13, 2013 at 17:34
  • 2
    modules/page.php != /modules/page.php! (well, probably not anyway) Check your absolute path Commented Jan 13, 2013 at 17:34
  • $elements[module] Quote your array keys correctly too, $elements['module'] to avoid nasty errors. Commented Jan 13, 2013 at 17:36
  • @Cups $elements[module] is correct when including in a parsed string (but nowhere else). Commented Jan 13, 2013 at 17:38
  • Well, I will go away and look that up, new on me! Cheers. ps here is a reference in case anyone else makes the same error as me oreilly.com/catalog/progphp/chapter/ch05.html Commented Jan 13, 2013 at 17:39

3 Answers 3

1

if i use include("modules/tax.php)"; it works just fine.

In your code, you're using

include("/modules/".$page."php");
         ^-- Note the leading / 

Try removing the /

Sign up to request clarification or add additional context in comments.

Comments

0

You are seeing if "modules/".$page."php" exists but trying to include "/modules/".$page."php" which are probably not the same things (notice the leading / on the latter).

if(file_exists("modules/".$page."php")) include("modules/".$page."php");

Comments

0

OK ive solved it thanks guys:) each answer was helpfull :) ( the answer was a combination of your answers+a litle more atttention needed from me . I forgot the . right before the php extension .. lolz.. guess when you are tired you have to take a litle break off word in order to be able to concetrate right ). thanks again guys :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.