0

I am having trouble getting my include() function to work in my code. Basically I have a the $order array that has the order that my pages will be shown in.

In this case: page numbers: 1,2,3,4,5,6 as seen in the $order array.

If I just post 6 include() function's with the exact path, the pages are shown, however if I try to include them under this for() loop it doesn't work.

$order Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 )

$fields = 6;

The weird part is if I make the array 4,5,6,1,2,3 it work's perfectly:

$order Array ( [0] => 4 [1] => 5 [2] => 6 [3] => 1 [4] => 2 [5] => 3 )

Only show's first 3 pages

for($x = 0; $x < $fields; $x++)
{
      $page_info = mysql_query("SELECT * FROM pages WHERE id='" . $order[$x] ."'");
      $pageInfo = mysql_fetch_array($page_info);
      $pageNum = $pageInfo['id'];
      if($pageNum <= 6)
      {
           $pagePath = "page" . $pageNum . ".php";
           include($pagePath);
      }
}

What is confusing is I know it is reading each $pageInfo['id'] because this is the following output:

Output: 123456

for($x = 0; $x < $fields; $x++)
{
      $page_info = mysql_query("SELECT * FROM pages WHERE id='" . $order[$x] ."'");
      $pageInfo = mysql_fetch_array($page_info);
      echo $pageInfo['id'];
}

This works

include("page1.php");
include("page2.php");
include("page3.php");
include("page4.php");
include("page5.php");
include("page6.php");
14
  • Maybe try this? $pagePath = "page" . trim($pageNum) . ".php"; Commented Nov 20, 2013 at 2:08
  • 1
    @bryan, including inside a loop is not a problem. echo $pagePath inside the loop, check that it is displaying the correct file. Commented Nov 20, 2013 at 2:14
  • 1
    Turn on error reporting and use require instaed of include, it'll help you to debug. Commented Nov 20, 2013 at 2:19
  • 1
    Good suggestion of RCV, most probably there is an error. Add the following at the beginning of your script: error_reporting(E_ALL|E_STRICT); ini_set('display_errors', true); Commented Nov 20, 2013 at 2:21
  • 1
    @RustyFausak actually showed me the careless reason why it wasn't working. Thank you RCV, Matthew, and everyone else who were trying to help me! Commented Nov 20, 2013 at 2:22

4 Answers 4

2

You probably have $x set to some number inside one of the includes. If it is only showing the first three, then perhaps in page3.php you have something like $x = 7;.

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

1 Comment

This solved it! You were absolutely right Rusty, thank you! Thank you to everyone else who tried to help! It is greatly appreciated.
2

You obviously have the numbers coming out that you're expecting to see (1-6), because you are printing them to the screen. You've tried a suggestion of using trim which didn't get you any further.

My presumption is that your echo used for debugging is places outside your if statement, and that your if statement isn't running every time you expect it to. Firstly, try putting your echo inside the if statement to make sure that your output is the same, then put var_dump($pagePath); into it as well to ensure that your variable is what you're expecting.

Another thing you could try, you could make sure the file exists:

echo (file_exists($pagePath)) ? 'Exists' : 'Does not exist...';

You could post us the code in your included files to check that you aren't overwriting variables like $x or $pageNum etc from your includes - variables are global between includes and will overwrite eachother.

Finally, I know you'll have a good explanation for this, but this looks pretty longwinded to me, in this particular application, you could just do this:

for($i = 1; $i <= 6; $i++) { include 'page' . $i . '.php'; }

SIDE NOTES:

mysql_* functions are deprecated and you should be using either PDO or mysqli. Resources: - http://php.net/manual/en/book.pdo.php - http://php.net/manual/en/book.mysqli.php

PHP's include function is a language control structure, not a function, so shouldn't be used with brackets: (for you downvoters, I'm not saying it can't, I'm saying it shouldn't)

// good:
include 'filename.php';
// bad:
include('filename.php');

4 Comments

Nah this doesn't matter
Tryed include $pagePath; but still only got first 3 pages
It is not a problem and it doesn't answer the question at all.
doesn't matter PHP handle a lot of things in many different ways :)
0

1) I would not recommend fetching the DB on a loop but keeping your example: You could try inside your loop something like

if($pageNum ){ //lets omit the <=6 for the test.
  $pagePath = "page" . $pageNum . ".php";
  echo "include($pagePath);";
}

and check the results, maybe you are getting something different to what you are expecting.

Comments

0

Try this

for($x = 0; $x < count($order); $x++)
{
      $page_info = mysql_query("SELECT * FROM pages WHERE id=" . $order[$x]);
      $pageInfo = mysql_fetch_array($page_info);
      foreach($pageInfo as $p_info)
      {
          $pageNum = $p_info['id'];
          break;
      }

  if($pageNum <= 6)
  {
       $pagePath = "page" . $pageNum . ".php";
       require_once($pagePath);//If it can't find the file then it will kill page and show corresponding error
  }

}

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.