What am I doing wrong here? php include

I am using php include. For my a href I am using this:

<a href="main.php?id=buttons/linkus">Buttons</a>

main.php is the page where all the content goes to. Click the link and it goes to the table where I have this:

This is what the server reads when it first loads the site:
[php]<?php if($id !=""){ readfile("$id.html");} else{ readfile("http://www.XXXXXXXXXXX.com/php-bin/news/news.php");} ?>[/php]

That will load the news page right off the bat. Now that first part of it says read _______.html

That tells the server that when a link is clicked it should read that content from the page.

Example:

www.XXXXXXXXX.com/main.php?id=buttons/linkus

When that link is clicked it says hey go here:

www.XXXXXXXXX.com/buttons/linkus.html

Read that content. Then this takes over:

[php]<?php include("./$id.html");?>[/php]

That says hey include that file in here.

But it isn’t doing that. It will load the “else readfile” but not the content I point the link to. It does this:

Warning: include(./.html) [function.include]: failed to open stream: No such file or directory in /home/XXXXXXXX/public_html/main.php on line 246

Warning: include(./.html) [function.include]: failed to open stream: No such file or directory in /home/XXXXXXXX/public_html/main.php on line 246

Warning: include() [function.include]: Failed opening ‘./.html’ for inclusion (include_path=’.:/usr/lib/php:/usr/local… in /home/XXXXXXXXXX/public_html/main.php on line 246

I would rethink your approach.

Pass the two variables individually, check the values then go where you need to.

You will also what to check for the existence of the file,
[php]<?php
if( file_exists("./$id.html") )
include("./$id.html");
?>[/php]

Well the thing is that the file does exist. And it is in place where it should be. The “else readfile” is what it is supposed to load when going to the website. After a link is clicked it inserts the content/html into:
[php]<?php include("./$id.html");?>[/php]

It allows me to only have one main page where I only need to edit menus and the layout once.

I can create a whole new layout and insert the same code and it will still insert the content.

Do you have the same directories for the files and are the filenames in those directories different. What about this approach?

[php]function test($filename)
{
$directories = [
‘buttons’,
‘somethingElse’,
‘home’,
‘test’,
‘/’,
];
try {
foreach ($directories as $dir) {
if (file_exists("{$_SERVER[‘DOCUMENT_ROOT’]}/$dir/$filename.html")) {
include “{$_SERVER[‘DOCUMENT_ROOT’]}/$dir/$filename.html”;
break;
}
}
return false;
} catch (Exception $e){
echo $e->getMessage();
}
}
[/php]

No I have different directories:

[php]Buttons[/php]

Where the file is directed to be displayed:
[php]<a href="main.php[/php]

Target file identification wildcard:
[php]?id=[/php]

Folder Location: can list more than one directory (i.e. content/files/buttons)
[php]buttons/[/php]

File name for wildcard:
[php]linkus[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service