piotrek
piotrek

Reputation: 1363

Why site looks differ if printed with php?

I have strange problem, I make for example 3 files main.html style.css index.php, then when I open index.php which read main.html and print to output I get different appearance than when I opening main.html, do you know what I making wrong ?

HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>aa</title>
<link rel="stylesheet" type="text/css" href="style2.css"/>
</head>
<body>
<div id="container">
a
</div>
</body></html>

CSS:

body
{
    width: 100%;
    padding:0;
    margin: 0;
    background: #f6f6f6;
}

#container
{
    margin: 0 ;
    padding: 0;
    background: #f6f6f6;

}

PHP:

<?php
 $output = file_get_contents("main2.html");
 echo $output;
?>

This main2.html enter image description here This is index.php enter image description here

Upvotes: 1

Views: 99

Answers (5)

j_s_stack
j_s_stack

Reputation: 665

In my last template system I used this function:

    private function read_file($filename){
        $code = '';
        if(file_exists($filename)){
            $templatefile = fopen($filename, 'r');
            while(!feof($templatefile)){
                $code = $code.fgets($templatefile, 1024);
            }   
            fclose($templatefile);
        }
        return $code;
    }

That works perfectly for me. Insted of file_get_content im using fopen to open the File and then with fgets im reading the Content and finally it has to be closed with fclose. See the PHP.net for further Information about the function.

You can also add some Code for a 404 Message if your Template File doesn't exist.

Upvotes: 0

piotrek
piotrek

Reputation: 1363

I find solution, i must set php file encoding to ANSI or I set all files encoding UTF-8 without BOM + header('Content-Type: text/html; charset=utf-8'); to php file.

Upvotes: 0

MihirUj
MihirUj

Reputation: 43

http://php.net/manual/en/function.file-get-contents.php

file_get_contents — Reads entire file into a string

http://php.net/manual/en/function.include.php

The include (or require) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement.

So better use include or require. Note : I am writing from a mobile that's why it is not so formatted.

I agree with @jordan davis "Also you should set the width: 100% on the header not on the body."

Upvotes: 0

Andr&#233; Ferreira
Andr&#233; Ferreira

Reputation: 185

Where you have this:

<?php
 $output = file_get_contents("main2.html");
 echo $output;
?>

Just simply change to

<?php include ('main2.html');

Upvotes: 1

Jordan Davis
Jordan Davis

Reputation: 1520

Use the PHP Require Function.

//PHP

<?php 
require 'main.html'; 
?>

Also you should set the width: 100% on the header not on the body.

Upvotes: 0

Related Questions