Forum Moderators: coopster
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 connectionif (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
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.
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...
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
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';
}
It won't if you put it this way : "('index.ihtml')
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
include $p_base . $_GET['page'] .'.ihtml';
eg: if you're linking index.php?page=contact_form.ihtml then it should be,
include $p_base . $_GET['page'];
include $p_base . $_GET['page'] .'.ihtml';
The code you added looks fine..
if (empty($page)) {
$page = 'index.php';
}
if(file_exist($p_base.$page)){
include $p_base . $page;
}
Let me know how it goes..
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
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! :)