Reputation: 9369
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");
Upvotes: 0
Views: 357
Reputation: 115
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
}
}
Upvotes: 0
Reputation: 1027
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.
Upvotes: 0
Reputation: 24406
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');
Upvotes: 2
Reputation: 7525
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;
.
Upvotes: 2