DiscreteTomatoes DiscreteTomatoes - 1 month ago 6
HTML Question

PHP echoing in wrong location of html

Hello i'm attempting to make a template based system to deliver content but i've run into a problem that i just cant seem to solve. When i try to echo out variables that have data from includes it gets outputted in the wrong section of my html.

Below is 'newstuff.php' which is my page to be executed on the browser, the offending variables are

$php $head $content
.

<?php

$php = include "templates/content/newstuff/phpCode.php";
$head = include "templates/content/newstuff/head.html";
$content = include "templates/content/newstuff/content.php";

include realpath(dirname(__FILE__)).'/templates/templateMain.php';
?>


Below is 'tempalteMain.php' this is my tempalte. Note the location of the echoing of
$php $head $content
.

<?php
echo $php;
?>


<!DOCTYPE html>
<html>

<head>
<?php echo $head; ?>
</head>

<body class="body1" onload="inputBlur2()">
<div class="borderLine"></div>
<div class="banner"></div>

<div class="mainContent1" >

<div class="header1" >

<div class="headerContainer" >

<ul class="navList1">
<li><a id = "B0" href="index.php">New Stuff</a></li>
<li><a id = "B1" href="MainPage.php">Products</a></li>
<li><a id = "B2" href="ProjectsPage.php">Projects</a></li>
<li><a id = "B3" href="AOrdering.php">About Ordering</a></li>
<li><a id = "B4" href="ContactMe.php">Contact Us</a></li>
<li><a id = "B5" href="FAQPage.php">FAQ</a></li>
<li><a id = "B6" href="SCart.php">My Cart</a></li>
</ul>
</div>
</div>

<div class="content1">
<?php echo $content; ?>
</div>

</div>
</body>
</html>


Below is 'head.html' this provides the code to be delivered by the
$head
PHP variable.

<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="/../StylePR.css">
<title>KickUp Electronics</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>


Below is 'content.php' this provides the code to be delivered by the
$content
PHP variable.

<p>Welcome to the new stuff page!!!</p>


Finally, this is the page source that gets outputted taken from chrome DOM editor. Note the locations of the information from content.php is wrong and there are strange '1's that are echoed out (also, when viewing page source, the information from head.html is placed outside the html tags).

<!DOCTYPE html>
<html>
<head><meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="/../StylePR.css">
<title>KickUp Electronics</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
</head>
<body class="body1" onload="inputBlur2()">
<p>Welcome to the new stuff page!!!</p> <!--Wrong Location!-->
1





1



<div class="borderLine"></div>
<div class="banner"></div>

<div class="mainContent1">

<div class="header1">

<div class="headerContainer">

<ul class="navList1">
<li><a id="B0" href="index.php">New Stuff</a></li>
<li><a id="B1" href="MainPage.php">Products</a></li>
<li><a id="B2" href="ProjectsPage.php">Projects</a></li>
<li><a id="B3" href="AOrdering.php">About Ordering</a></li>
<li><a id="B4" href="ContactMe.php">Contact Us</a></li>
<li><a id="B5" href="FAQPage.php">FAQ</a></li>
<li><a id="B6" href="SCart.php">My Cart</a></li>
</ul>
</div>
</div>

<div class="content1">
1 </div>


</div>

</body></html>


I tried searching many times for a solution to no avail. Is this a problem with the echo being executed before the html fully loads? Any help would be highly appreciated!

Answer

You're using includes wrong.

$php = include "templates/content/newstuff/phpCode.php";

is immediately outputting the output of that file, and setting $php to 1 (i.e. "it worked!").

Handling Returns: include returns FALSE on failure and raises a warning. Successful includes, unless overridden by the included file, return 1. - http://php.net/manual/en/function.include.php

You can use output buffering to capture the output, but a better solution is probably moving the include calls directly into templateMain.php.