PhpDig.net

Go Back   PhpDig.net > PhpDig Forums > How-to Forum

Reply
 
Thread Tools
Old 01-02-2004, 12:40 PM   #1
gman
Green Mole
 
Join Date: Dec 2003
Location: Texas
Posts: 7
Question PHP includes in templates

I have read through the forums, but still need help using PHP includes in the templates. Can someone please give detailed instructions on how to do this with some examples.

Maybe, somehow show how to include a header include and a footer include in the template file.

I would love to use this excellent app... just need a little help to do it.
gman is offline   Reply With Quote
Old 01-02-2004, 09:18 PM   #2
Charter
Head Mole
 
Charter's Avatar
 
Join Date: May 2003
Posts: 2,539
Hi. Say you want to include header.html and footer.html in a template.

First add the following function to the top of the search_function.php file:
PHP Code:
function get_my_includes($filename) {
  if (
file_exists($filename)) {
    
$buffer "";
    
$handle fopen($filename"r");
    while (!
feof($handle)) {
      
$buffer .= fgets($handle4096);
    }
    
fclose($handle);
    return 
$buffer;
  }

Next in search_function.php replace:
PHP Code:
if ($template == 'array' || is_file($template)) {
    
$phpdig_version PHPDIG_VERSION;
    
$t_mstrings compact('powered_by_link','title_message','phpdig_version','result_message','nav_bar','ignore_message','ignore_commess','pages_bar','previous_link','next_link','templates_links');
    
$t_fstrings phpdigMakeForm($query_string,$option,$limite,SEARCH_PAGE,$site,$path,'template',$template);
    if (
$template == 'array') {
        return 
array_merge($t_mstrings,$t_fstrings,array('results'=>$table_results));
    }
    else {
        
$t_strings array_merge($t_mstrings,$t_fstrings);
        
phpdigParseTemplate($template,$t_strings,$table_results);
    }

with the following:
PHP Code:
// check that the paths are correct or use full paths
$my_page_header get_my_includes("../../dir/header.html");
$my_page_footer get_my_includes("../../dir/footer.html");

if (
$template == 'array' || is_file($template)) {
    
$phpdig_version PHPDIG_VERSION;
    
$t_mstrings compact('my_page_header','my_page_footer','powered_by_link','title_message','phpdig_version','result_message','nav_bar','ignore_message','ignore_commess','pages_bar','previous_link','next_link','templates_links');
    
$t_fstrings phpdigMakeForm($query_string,$option,$limite,SEARCH_PAGE,$site,$path,'template',$template);
    if (
$template == 'array') {
        return 
array_merge($t_mstrings,$t_fstrings,array('results'=>$table_results));
    }
    else {
        
$t_strings array_merge($t_mstrings,$t_fstrings);
        
phpdigParseTemplate($template,$t_strings,$table_results);
    }

Finally in the template use:
Code:
<phpdig:my_page_header/>
<phpdig:my_page_footer/>
Remember to remove any "word" wrapping in the above code.
__________________
Responses are offered on a voluntary if/as time is available basis, no guarantees. Double posting or bumping threads will not get your question answered any faster. No support via PM or email, responses not guaranteed. Thank you for your comprehension.
Charter is offline   Reply With Quote
Old 01-05-2004, 06:12 AM   #3
gman
Green Mole
 
Join Date: Dec 2003
Location: Texas
Posts: 7
Wow, thanks for the in-depth reply. Exactly the response I was looking for. I think this will help a lot of people out including myself.
gman is offline   Reply With Quote
Old 01-05-2004, 03:03 PM   #4
gman
Green Mole
 
Join Date: Dec 2003
Location: Texas
Posts: 7
By the way, it worked like a charm. Thanks again.
gman is offline   Reply With Quote
Old 04-01-2004, 07:52 PM   #5
gomac
Green Mole
 
Join Date: Mar 2004
Posts: 1
RE: PHP includes in templates

This is brilliant! Any way to modify this so that it can include AND parse any PHP in the include?
gomac is offline   Reply With Quote
Old 04-01-2004, 08:01 PM   #6
Charter
Head Mole
 
Charter's Avatar
 
Join Date: May 2003
Posts: 2,539
Hi. Using the same method as above, you could include header.php, sidebar.php, and footer.php in a template, or you could also make your own custom PHP search page as follows.

In the search.php file do the following:
PHP Code:
$arrayout phpdigSearch($id_connect$query_string$option$refine,
                       
$refine_url$lim_start$limite$browse,
                       
$site$path$relative_script_path'array');

if (
is_file("custom_search.php")) {
    include 
"custom_search.php";
} else { exit(); } 
Save the following in the same directory as the search.php file:
PHP Code:
<?php
// custom_search.php

if (eregi("custom_search.php",$_SERVER['SCRIPT_FILENAME']) || 
    
eregi("custom_search.php",$_SERVER['REQUEST_URI'])) {
    exit();
}

echo 
"<html><body>";

echo 
$arrayout['result_message'],
     
$arrayout['powered_by_link'],
     
$arrayout['title_message'],
     
$arrayout['phpdig_version'],
     
$arrayout['nav_bar'],
     
$arrayout['pages_bar'],
     
$arrayout['next_link'],
     
$arrayout['form_head'],
     
$arrayout['form_title'],
     
$arrayout['form_field'],
     
$arrayout['form_select'],
     
$arrayout['form_button'],
     
$arrayout['form_radio'],
     
$arrayout['form_foot'];

if (!empty(
$arrayout['results'])) {
    
$num_out count($arrayout['results']);
} else { 
$num_out 0; }
$num_start $lim_start 1;
$num_end $lim_start $num_out;

for (
$i=$num_start$i<=$num_end$i++) {
     echo 
$i.". ";
     
$arrayout2 $arrayout['results'][$i];
     echo 
$arrayout2['weight'],
          
$arrayout2['img_tag'],
          
$arrayout2['page_link'],
          
$arrayout2['limit_links'],
          
$arrayout2['filesize'],
          
$arrayout2['update_date'],
          
$arrayout2['complete_path'],
          
$arrayout2['link_title'],
          
$arrayout2['text'];
     echo 
"<br><br>";
}

echo 
"</body></html>";

?>
Remember to remove any "word" wrapping in the above code.
__________________
Responses are offered on a voluntary if/as time is available basis, no guarantees. Double posting or bumping threads will not get your question answered any faster. No support via PM or email, responses not guaranteed. Thank you for your comprehension.
Charter is offline   Reply With Quote
Old 06-03-2004, 01:33 PM   #7
jclabaugh
Green Mole
 
Join Date: Jun 2004
Posts: 3
Charter:

I followed your code above, modifying search_function.php and the template. I included a header and footer file.

My scripts
search_function.php
Code:
I added to the top:
function get_my_includes($filename) {
  if (file_exists($filename)) {
  $buffer = "";
  $handle = fopen($filename, "r");
  while (!feof($handle)) {
    $buffer .= fgets($handle, 4096);
  }
  fclose ($handle);
  return $buffer;
 }
}
I modified the later bits as such.
Code:
$my_page_header = get_my_includes("./includes/header_aux.inc.php");
$my_page_footer = get_my_includes("./includes/footer_aux.inc.php");

if ($template == 'array' || is_file($template)) {
    $phpdig_version = PHPDIG_VERSION;
    $t_mstrings = compact('my_page_header','my_page_footer','powered_by_link','title_message','phpdig_version','result_message','nav_bar','ignore_message','ignore_commess','pages_bar','previous_link','next_link','templates_links');
    $t_fstrings = phpdigMakeForm($query_string,$option,$limite,SEARCH_PAGE,$site,$path,'template',$template);
    if ($template == 'array') {
        return array_merge($t_mstrings,$t_fstrings,array('results'=>$table_results));
    }
    else {
        $t_strings = array_merge($t_mstrings,$t_fstrings);
        phpdigParseTemplate($template,$t_strings,$table_results);
    }
}
And to the template I added:
Code:
<phpdig:my_page_header/>
<phpdig:my_page_footer/>
No problems, except the included files aren't being parsed, just included as code. Any ideas on how to get these included files to parse?

Cheers,

JC
jclabaugh is offline   Reply With Quote
Old 06-03-2004, 06:54 PM   #8
vinyl-junkie
Purple Mole
 
Join Date: Jan 2004
Posts: 694
Quote:
Originally posted by jclabaugh

No problems, except the included files aren't being parsed, just included as code. Any ideas on how to get these included files to parse?
Did you see this thread? This method will parse your files. If you have any questions about it, I'll be glad to help.

<edit>
BTW, I forgot to welcome you to the forum. Glad you could join us!
</edit>
vinyl-junkie is offline   Reply With Quote
Old 06-04-2004, 05:50 AM   #9
Charter
Head Mole
 
Charter's Avatar
 
Join Date: May 2003
Posts: 2,539
Hi. To parse PHP files, remove the get_my_includes function, as that reads but does not parse PHP files, and then replace:
PHP Code:
$my_page_header get_my_includes("./includes/header_aux.inc.php");
$my_page_footer get_my_includes("./includes/footer_aux.inc.php"); 
with the following:
PHP Code:
$my_page_header = include("/path/to/includes/header_aux.inc.php");
$my_page_footer = include("/path/to/includes/footer_aux.inc.php"); 
Then just keep the rest the same.
__________________
Responses are offered on a voluntary if/as time is available basis, no guarantees. Double posting or bumping threads will not get your question answered any faster. No support via PM or email, responses not guaranteed. Thank you for your comprehension.
Charter is offline   Reply With Quote
Old 06-04-2004, 08:39 AM   #10
jclabaugh
Green Mole
 
Join Date: Jun 2004
Posts: 3
Ok, Pat and Charter, I have tried both of your code examples. Thanks for the advice but I am still not having success... I decided to go with Charter's example for simplicity. I removed the get_my_includes function and changed the other code in search_function.php to

Code:
$my_header_file = include("./includes/header_search.inc.php");
$my_footer_file = include("./includes/footer_search.inc.php");

if ($template == 'array' || is_file($template)) {
    $phpdig_version = PHPDIG_VERSION;
    $t_mstrings = compact('my_header_file','my_footer_file','powered_by_link','title_message','phpdig_version','result_message','nav_bar','ignore_message','ignore_commess','pages_bar','previous_link','next_link','templates_links');
and now it parses the PHP of my includes. Great, but though the structure of the template is still

Code:
<phpdig:my_header_file/>
normal phpdig code here 
<phpdig:my_footer_file/>
it prints out both the header and footer before the phpdig search stuff. I have changed around the order of the strings being called in 'compact' above (putting 'my_footer_file' after 'template_links') but it made no difference.

Any other ideas on how I can get it to print a header then the phpdig content then the footer, in that order?

Cheers,

JC
jclabaugh is offline   Reply With Quote
Old 06-04-2004, 08:45 AM   #11
Charter
Head Mole
 
Charter's Avatar
 
Join Date: May 2003
Posts: 2,539
Hi. Try taking a look at the HTML source from your page and then in the template just move <phpdig:my_header_file/> and <phpdig:my_footer_file/> so that the order is correct.
__________________
Responses are offered on a voluntary if/as time is available basis, no guarantees. Double posting or bumping threads will not get your question answered any faster. No support via PM or email, responses not guaranteed. Thank you for your comprehension.
Charter is offline   Reply With Quote
Old 06-04-2004, 09:16 AM   #12
jclabaugh
Green Mole
 
Join Date: Jun 2004
Posts: 3
Charter, thanks. There appeared to be no way to reorder the way they are listed to provide the desired result, given the order is
Code:
<phpdig:my_header_file/>
normal phpdig code here 
<phpdig:my_footer_file/>
moving the footer up or the header down in relation to the phpdig code block made no difference.

I'm on a time limit so I gave up and hardwired the HTML into the template and got the result I 'wanted'. Thanks for your help.
jclabaugh is offline   Reply With Quote
Old 08-12-2004, 10:32 AM   #13
subscript
Green Mole
 
Join Date: Aug 2004
Posts: 1
I was having same problem as jclabaugh - order of template header and footer.

Found that this ordering is not adjusted in the HTML template file, it must be adjusted via the order of elements appearing in /libs/search_function.php

Code should read something like this to place footer below PHPDig search results:

PHP Code:
if ($template == 'array' || is_file($template)) {

// use full paths to header
$my_page_header = include("../../dir/search_header.php");

    
$phpdig_version PHPDIG_VERSION;
    
$t_mstrings =  compact('my_page_header','my_page_footer','powered_by_link','title_message'
,'phpdig_version','result_message','nav_bar','ignore_message','ignore_comme
ss'
,'pages_bar','previous_link','next_link','templates_links');
    
$t_fstrings =  phpdigMakeForm($query_string,$option,$limite,SEARCH_PAGE,$site,$path,'templ
ate'
,$template);
    if (
$template == 'array') {
        return 
array_merge($t_mstrings,$t_fstrings,array('results'=>$table_results));
    }
    else {
        
$t_strings array_merge($t_mstrings,$t_fstrings);
        
phpdigParseTemplate($template,$t_strings,$table_results);
    }

// use full paths footer
$my_page_footer = include("../../dir/search_footer.php");

}
else { ... 
subscript is offline   Reply With Quote
Old 08-12-2004, 06:17 PM   #14
vinyl-junkie
Purple Mole
 
Join Date: Jan 2004
Posts: 694
Welcome to the forum, subscript.

Your method of including a custom header/footer is certainly a pretty simple way of doing it. Thanks for sharing. It's nice to have someone post one more way of doing it.
vinyl-junkie is offline   Reply With Quote
Old 12-03-2004, 11:02 PM   #15
Charter
Head Mole
 
Charter's Avatar
 
Join Date: May 2003
Posts: 2,539
Also, from php.net:

return.php
PHP Code:
<?php

$var 
'PHP';

return 
$var;

?>
noreturn.php
PHP Code:
<?php

$var 
'PHP';

?>
testreturns.php
PHP Code:
<?php

$foo 
= include 'return.php';

echo 
$foo// prints 'PHP'

$bar = include 'noreturn.php';

echo 
$bar// prints 1

?>
So if you structure the included PHP files using return then you should be without order problems and without seeing ones on your pages.
__________________
Responses are offered on a voluntary if/as time is available basis, no guarantees. Double posting or bumping threads will not get your question answered any faster. No support via PM or email, responses not guaranteed. Thank you for your comprehension.
Charter is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
php includes template info 5wayshost Coding & Tutorials 0 12-31-2006 10:59 PM
Themes and PHP Includes Arctic Troubleshooting 3 05-07-2005 09:21 PM
Using PHP include in templates sid How-to Forum 3 11-23-2003 07:06 AM
PHP includes in Template? jirving How-to Forum 1 09-29-2003 03:37 PM
Templates - cannot get index.php to display different one jirving How-to Forum 9 09-29-2003 06:57 AM


All times are GMT -8. The time now is 07:37 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.