Welcome to WebmasterWorld Guest from 54.167.5.15

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Problem including a page

Want to pass data in the include e.g include 'mypage.php?id=1'

     

nubbin

6:12 am on Nov 25, 2005 (gmt 0)

10+ Year Member



Hi,

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

Twisted Mind

8:34 am on Nov 25, 2005 (gmt 0)

5+ Year Member



is it really necaccarry to include a html page? if this is regular text only you should try putting it all into a database

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

nubbin

9:02 am on Nov 25, 2005 (gmt 0)

10+ Year Member



Hi,

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

Anyango

9:10 am on Nov 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Nubbin


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.

Twisted Mind

9:12 am on Nov 25, 2005 (gmt 0)

5+ Year Member



ehm why are u including template?id=1? where does id stand for

i think u need something like

if ($_GET[id]== '1')
{
include "userfriendlypage.htm";
}

else i really dont know what u understand

sunveria

1:04 pm on Nov 25, 2005 (gmt 0)

5+ Year Member



I have face the same problem like that a while ago and I have figure it out. Do not use include because include can not pass a parameter.
Use require if you want to pass a parameter.

Hope that Help..

^ ^

nubbin

8:37 pm on Nov 25, 2005 (gmt 0)

10+ Year Member



Anyango, thanks for your very clear explanation.

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

sunveria

1:12 pm on Nov 27, 2005 (gmt 0)

5+ Year Member



I'm using the absolute path....

you can try this...

file index.php
<?php
require_once("http://localhost/menu.php?a=testing");
?>

file menu.php
<?php
$a=$_GET["a"];
echo "$a";
?>

adb64

1:47 pm on Nov 27, 2005 (gmt 0)

10+ Year Member



You don't need to pass parameters like this to your template. If you have them in your main file they are also known in your template.

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

nubbin

4:32 am on Nov 30, 2005 (gmt 0)

10+ Year Member



adb64,

Thanks. That works perfectly.

 

Featured Threads

Hot Threads This Week

Hot Threads This Month