Forum Moderators: coopster

Message Too Old, No Replies

PHP Template

Need help changing page content.

         

Spook

10:39 pm on Sep 7, 2004 (gmt 0)

10+ Year Member



I've been following the recent threads regarding php templates and decided that this is the way to go for my next site. However, I need some help/advice on how to change the page content.

I am sure this is simple, but the penny won't drop.

I have the following code in my index.php file:


<?php
// Load Required Files
require ('db/cfg.php'); //config data
require ('db/db.php'); //server/db connection

if (empty($page))
{
$page = 'index' .'.ihtml';
}
//Load template
include 'modules/templates/header.php';
include $p_base . $page; // Page content
include 'modules/templates/left_menu.php';
include 'modules/templates/right_menu.php';
include 'modules/templates/banner.php';
include 'modules/templates/footer.php';

?>

This builds the page template and initially loads the index.ihtml page which holds the page contents.

Now, in my left_menu.php I want to put a link, such that only the content of the page will change. Say I want to include "contact_form.php" for example.

I believe this is done by calling the page in the link, something like?page = contact_form.php, but I am not sure how to implement this.

Can anyone offer advice, or point me to a tutorial?

Your help is appreciated.

Spook

Zipper

10:52 pm on Sep 7, 2004 (gmt 0)

10+ Year Member



>> Now, in my left_menu.php I want to put a link, such that only the content of the page will change. Say I want to include "contact_form.php" for example.

can u make it a bit more clear? what exactly do u need to put in left_menu.php. Is it a link or actual page content (contact_form.php)?

>> I believe this is done by calling the page in the link, something like?page = contact_form.php

If you want to include the page this way, here's the code.
if(file_exists($_GET['page']))[
include($_GET['page']);
}

Also, why have you concatenated $page? ('index' .'.ihtml'). There is no need for it.

Spook

7:53 am on Sep 8, 2004 (gmt 0)

10+ Year Member



Hi Zipper, many thanks for your reply.

My page template is a 3 col layout [Banner, Footer, Left/right columns and content area in the centre]. At this point in time I am just trying to change the content area of the page by clicking a link which is in the left column.

The code you posted is similar to what I was trying before I posted here - but I guess I am missing something!

I now have in my code:


//Load template
include 'modules/templates/header.php';

if(file_exists($_GET['page']))
{
include $p_base .($_GET['page']).'.ihtml'; //THIS IS LINE 21
}
else
{
include $p_base .'index'.'.ihtml';
}

include 'modules/templates/left_menu.php';
etc...


But get the following error:

PHP Warning: Failed opening 'c:\phpdev\www\domain\modules\user\content\/.ihtml' for inclusion (include_path='.;C:/phpdev/php/includes;C:/phpdev/php/class') in c:\phpdev\www\domain\index.php on line 21

If I substitute the above code with

include $p_base .'index'.'.ihtml';

the content is shown as expected.

>>Also, why have you concatenated $page? ('index' .'.ihtml'). There is no need for it.

Well, I had been studying another site whilst trying to learn how to do this, and this is a "carry over" from there. However, without it, the above include $p_base .'index' statement automatically looks for index.php.

Spook

>>ADDED<<

Ok, I've played with this a little more now and seem to have solved the problem - but what do you think?

$page = ($_GET['page']);

if (empty($page))
{
$page = 'index.php';
}
//Load template
include 'modules/templates/header.php';
include $p_base . $page;
etc...

And this pulls in the index page content [now changed from .ihtml to .php].

I have yet to modify the link, to pull in the contact form instead of the index page content.

Regards

Spook

Zipper

8:47 am on Sep 8, 2004 (gmt 0)

10+ Year Member



In your first post u were trying to include "contact_form.php", and in the second a file with the "ihtml" extension. Could you give an example of a filename which you are trying to include along with the exact path in the server..

For the code, You have to do a query string check for the 'page' parameter. try something like this.


if(isset($_GET['page'])){
if(file_exist($_GET['page'])){
include $p_base . $_GET['page'] .'.ihtml';
} else {
include $p_base .'index.ihtml';
}
} else {
include $p_base .'index.ihtml';
}

>> Well, I had been studying another site whilst trying to learn how to do this, and this is a "carry over" from there. However, without it, the above include $p_base .'index' statement automatically looks for index.php.

It won't if you put it this way : "('index.ihtml')

Spook

9:29 am on Sep 8, 2004 (gmt 0)

10+ Year Member



Ok, sorry for the confusion, all of my included content 'now' has the .ihtml file extension.

The file I am trying to include is contact_form.ihtml and resides in the c:\phpdev\www\domain\modules\user\content\ directory.

In my cfg.php file I have set the $p_base variable to:

$p_base='c:\phpdev\www\domain\modules\user\content\/'

Thanks for your time.

Spook

Zipper

10:25 am on Sep 8, 2004 (gmt 0)

10+ Year Member



You can take off the forward slash(/) at the end of $p_base or use "modules/user/content/" for $p_base instead. and change the following line,

include $p_base . $_GET['page'] .'.ihtml';

in the previous code depending on the way you set "page" in the query string.

eg: if you're linking index.php?page=contact_form.ihtml then it should be,


include $p_base . $_GET['page'];

or if it's index.php?page=contact_form then,

include $p_base . $_GET['page'] .'.ihtml';

Zipper

10:41 am on Sep 8, 2004 (gmt 0)

10+ Year Member



darn.. once again it's a matter of php & ihtml..

The code you added looks fine..


if (empty($page)) {
$page = 'index.php';
}

But make sure you check whether the file exists before including it.

if(file_exist($p_base.$page)){
include $p_base . $page;
}

Also be aware that using $_GET['page'] directly will throw a notice depending on your error display settings.

Let me know how it goes..

Spook

3:15 pm on Sep 8, 2004 (gmt 0)

10+ Year Member



Hi Zipper.

I think I now have this sorted with the following code :)

// Get page
$page = ($_GET['page']);

if (empty($page))
{
$page = 'index.ihtml';
}

//Load template
include 'modules/templates/header.php';
if(file_exists($p_base .$page))
{
include $p_base .$page; // Page content
}

etc...

One final question [I think]! Why is it necessary to check if the file exists before including it?

Many thanks for your help in walking me through this.

Spook

Zipper

5:13 pm on Sep 8, 2004 (gmt 0)

10+ Year Member



>>Why is it necessary to check if the file exists before including it?

Nothing major, but since you're using the page names within the links, if by chance a page is misspelled or deleted you would get those annoying php exceptions which fills your error log. You can add an 'else' statement to include the 'index' page or a custom '404' page to make it fully work. (I use it as a safety measure.)

>>Many thanks for your help in walking me through this.

you're welcome! :)