homepage Welcome to WebmasterWorld Guest from 54.196.201.253
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Problem including a page
Want to pass data in the include e.g include 'mypage.php?id=1'
nubbin




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

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




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

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




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

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




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

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




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

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




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

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




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

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




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

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




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

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




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

adb64,

Thanks. That works perfectly.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved