Forum Moderators: coopster
I am building a new site and trying to use a template via an Include to make the site easy to maintain.
My problem is the Include statement I am using to include the template is failing and I don't know why.
- I have a file template.php
- I have a page my-page-with-friendly-url.html
- All I want this page to do is include template.php passing it an id number
- Then template php will use the id number to go to a database and get the page title, meta desc and content for my-page-with-friendly-url.html.
The code I tried was
include 'template.php?id=1';
However this fails with error:
Failed opening 'template.php?id=1' for inclusion (include_path='.;C:\php5\pear')
I would be really grateful if anyone can tell me how to correct this.
Thanks
like:
TABLE PAGES
+--------+---------+--------+-----------+
¦id ¦Title ¦ Link ¦ CONTENT ¦
+--------+---------+--------+-----------+
¦1 ¦pagetitle¦link1 ¦pagecontent¦
in this case u can make a querty wich does a $_GET['id']
and make a query wich lists your id and creates an url like
a href="TEMPLATE.PHP?id=<?echo $id?>"
and then loop this...
so when u press a link it will go to default?id=1 or whatever
and the other querty takes the content and page title
Isnt this easyer? u could make a backand tool to edit it all pretty easy too... but thats not your request
hope this helps
greetz
Thanks for your reply. I am not including an html page. I am including a template generated in php. The reason I am doing this is so that I can give the calling page a search engine and user friendly URL.
- The calling page includes the template, passing an id.
- template uses the id to look up the title,content etc for the calling page in a database table and then formats all the HTML for the calling page.
I have found out that if I write the include like this it works:
include 'http://mysite.com/template.php?id=1';
Whereas this did not work, but I do not understand why:
include 'template.php?id=1';
Does anyone know why the latter did not work?
Cheers
I have found out that if I write the include like this it works:
include 'http://mysite.com/template.php?id=1';Whereas this did not work, but I do not understand why:
include 'template.php?id=1';
Simply because first one is included using HTTP request and when your code requests that page, it's served by your http server and hence included correctly, however in the second one, the include call is being made to file system of the server and filesystem doesnt recognize arguements in filename, on your server's file system mapping your filename is "template.php" and when you call 'template.php?id=1' , it doesn't know that this is the same template.php file
hope that clears the concept.
Sunveria, thanks for your tip. I tried it and it did not work for me. According to Anyango's explanation this would be expected ("...call is being made to file system of the server and filesystem doesnt recognize arguements in filename...") so I am curious to know why your require works.
Thanks all
main.php:
<?php
$id = $_GET['id'];
include('template.php');
?>
template.php:
<?php
echo $id;
?>
You can only include a PHP file in a HTML file if your server parses the HTML file as a PHP file.
Regards,
Arjan