Template page help

JeremeR

New member
I am trying to create a template page (masterPage.php) where a header.php and footer.php files are included on the page, and and body (index.php, about.php, etc) are used for the body content of each page. I have this somewhat working but for some reason the content portion is showing up before the header and footer. Is there a better way to do this? -- Thanks

masterPage.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Site</title>
<!-- You can include any additional CSS or meta tags here
<link rel="stylesheet" href="styles.css"> -->
</head>
<body>


<table border="1px" width="100%">
<tr>
<td>

<?php
require_once('header.php');
?>

</td>
</tr>
<tr>
<td>

<?php
$content
?>

</td>
</tr>
<tr>
<td>

<?php
require_once('footer.php');
?>

</td>
</tr>
</table>



</body>
</html>

header.php
<table border="1px" width="100%">
<tr>
<td>Header</td>
</tr>
</table>

footer.php
<table border="1px" width="100%">
<tr>
<td>Footer</td>
</tr>
</table>

index.php
<?php

function pageContent()
{
print("<table border='0px' width='100%'>");
print("<tr>");
print("<td>Index</td>");
print("</tr>");
print("</table>");
}




$content = pageContent();

require_once('masterPage.php');

?>
 
Your approach is generally correct, but the issue you're encountering is likely due to the order in which you're including the content within masterPage.php. Right now, you're including the content before both the header and footer.

To fix this, you should include the content between the header and footer sections.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Site</title>
<!-- You can include any additional CSS or meta tags here
<link rel="stylesheet" href="styles.css"> -->
</head>
<body>

<table border="1px" width="100%">
<tr>
<td>

<?php
require_once('header.php');
?>

</td>
</tr>
<tr>
<td>

<?php
// Content included here
echo $content;
?>

</td>
</tr>
<tr>
<td>

<?php
require_once('footer.php');
?>

</td>
</tr>
</table>

</body>
</html>

And also modify your index.php to assign the content to $content instead of printing it directly:

<?php

function pageContent()
{
return "<table border='0px' width='100%'>
<tr>
<td>Index</td>
</tr>
</table>";
}

$content = pageContent();

require_once('masterPage.php');

?>

This way, the content will be included between the header and footer sections in your masterPage.php, ensuring that they display in the correct order.


Best Regard
Danish hafeez | QA Assistant
ICTInnovations
 
Back
Top