Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3281870
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:51:51+00:00 2026-05-17T19:51:51+00:00

i create a template file for my site… it’s like: <!– template.php –> <?php

  • 0

i create a template file for my site… it’s like:

<!-- template.php -->
<?php function showheader() { ?>
<head><body>
<!-- some of style files and menus -->
<div class="content">
<?php } ?>

<?php function showfooter() { ?>
</div></body></html>
<?php } ?>

i use this file as a template like this:

<?php include_once("template.php"); showheader(); ?>
content text or photo or ... etc.
<?php showfooter(); ?>

that’s all… but if i try to use a connection on template file, it screw up!
i used an external file like:

<?php
//
// include_once connection file
// query strings goes here
//

do {
echo $row_table['id']; //example
} while ($row_table = mysql_fetch_assoc($table));

?>

and i use this file as include_once(“filename.php”); on template file… at this point it gives errors… like what is this connection variable, what is this connection string… etc. it cannot reach connection strings…

by the way, i use another external connection like:

<?php
global $hostname_conn,$database_conn,$username_conn,$password_conn,$conn;
$hostname_conn = "localhost";
$database_conn = "test";
$username_conn = "****";
$password_conn = "****";
$conn = mysql_pconnect($hostname_conn, $username_conn, $password_conn) or trigger_error(mysql_error(),E_USER_ERROR); 
mysql_query("SET NAMES 'utf8'");
?>

i’m gonna cry! what’s the problem… and do you know another way to use template…
thanks much…

PS: i change variables on conn.php as global (and it didnt work) and i change include, include_once, require, require_once where i include files but it didnt give anything.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T19:51:51+00:00Added an answer on May 17, 2026 at 7:51 pm

    This separates the page into two PHP files: (1) the first obtains the data, and (2) the second displays the data.

    While getting data, not a single character should be printed out.
    If some errors occurred, display an error page.

    Once you get all your data with no errors – it’s time to include a template. The template has two PHP files as well: the template for the page itself and the template that is shared in common by all the pages in the site.

    By sorting things this way you’ll solve all your present and future templating problems.

    A typical script may look like

    <?
    //include our settings, connect to database etc.
    include dirname($_SERVER['DOCUMENT_ROOT']).'/cfg/settings.php';
    //getting required data
    $DATA=dbgetarr("SELECT * FROM links");
    $pagetitle = "Links to friend sites";
    //etc
    //and then call a template:
    $tpl = "links.php";
    include "template.php";
    ?>
    

    where template.php is your main site template, including common parts, like header, footer, menu etc:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>My site. <?=$pagetitle?></title>
    </head>
    <body>
    <div id="page">
    <? include $tpl ?>
    </div>
    </body>
    </html>
    

    and links.php is the actual page template:

    <h2><?=$pagetitle?></h2>
    <ul>
    <? foreach($DATA as $row): ?>
    <li><a href="<?=$row['link']?>" target="_blank"><?=$row['name']?></a></li>
    <? endforeach ?>
    <ul>
    

    easy, clean and maintainable.

    settings.php contains all common settings:

    <?php
    $hostname_conn,$database_conn,$username_conn,$password_conn,$conn;
    $hostname_conn = "localhost";
    $database_conn = "test";
    $username_conn = "****";
    $password_conn = "****";
    $conn = mysql_connect($hostname_conn, $username_conn, $password_conn) 
             or trigger_error(mysql_error(),E_USER_ERROR); 
    mysql_query("SET NAMES 'utf8'") or trigger_error(mysql_error(),E_USER_ERROR);
    
    $tpl = "default.php";
    $pagetitle = "";
    
    function dbgetarr(){
      $a     = array();
      $args  = func_get_args();
      $query = array_shift($args); 
      $query = str_replace("%s","'%s'",$query); 
      foreach ($args as $key => $val) { 
        $args[$key] = mysql_real_escape_string($val); 
      } 
      $query = vsprintf($query, $args);
    
      $res = mysql_query($query);
      if (!$res) {
        trigger_error("dbget: ".mysql_error()." in ".$query);
      } else {
        while($row = mysql_fetch_assoc($res)) $a[]=$row;
      }
      return $a;
    }
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can use VS08's MFC/ActiveX template to create a C++ ActiveX object that I
VS.net creates a template when you create a WCF project. It adds a class
I'm trying to create a feature that both creates a list template and an
My goal here is to create a very simple template language. At the moment,
I'm trying to create a perfectly circular button, So I created a template from
I want to create templates for base new reports on to have common designs.
I have created a template for Visual Studio 2008 and it currently shows up
I have a gridview to which I have created an Insert Template in the
I am trying to learn & create Visual Studio templates and as per this
I use Codesmith to create our code generation templates and have had success in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.