Insert php code into html document using include

Dear participants. I turn to you with a banal question – you need div-blocks like a header, menus, etc. for convenience. insert as php inserts, since the site is made manually without tsms and there are many pages with content that needs to be edited on all pages from time to time. Can you please tell me the best way to do this? I understand that you need to create separate php files with the code to be inserted. I’m interested in the format in which the code should be entered and the format of its implementation in the html document. Thank you.

Here is the header code, etc.

   <div id="hmenu" style="height:80px;">
    <ul>
        <li><a class="current first" href="index.html"> </a></li>  
        <li><a href="#"></a></li>
        <li><a href="#"></a></li>
        <li><a href="#"> </a></li>
        <li><a href="#"></a></li>
    </ul>
</div>
              <h2> </h2>
                    <div class="news">
                        <div id="news" class="scroll-pane">
                    <ul>
                        <li>- <a href="mmebel.html"> 1</a></li>
                        <li>- <a href="#"> 2</a></li>  
                        <li>- <a href="#"> 3</a></li>
                        <li>- <a href="#"> 4</a></li>
                        <li>- <a href="#"> 4</a></li>
                        <li>- <a href="#"> 4</a></li>
                        <li>- <a href="#"> 4</a></li>
                        <li>- <a href="#"> 4</a></li>
                        <li>- <a href="#"> 4</a></li>
                        <li>- <a href="#"> 4</a></li>
                        <li>- <a href="#"> 4</a></li>
                        <li>- <a href="#"> 4</a></li>
                        <li>- <a href="#"> 4</a></li>
                        <li>- <a href="#"> 4</a></li>
                        <li>- <a href="#"> 4</a></li>
                        <li>- <a href="#"> 4</a></li>
                        <li>- <a href="#"> 4</a></li>
                    </ul>
                            </div>
                        </div>
                </div>

Answer 1, authority 100%

<div id="hmenu" style="height:80px;">
   <?php include_once 'hmenu.php' ?>
</div>
<h2> </h2>
<div class="news">
    <div id="news" class="scroll-pane">
        <?php include_once 'news.php' ?>
    </div>
</div>

Document must be HTML.


Answer 2, authority 50%

well, in its simplest form, you can do something like

function viewInit($view) {
    $viewPath = $_SERVER['DOCUMENT_ROOT'] . '/view/' . $view . '.html';
    if(file_exists($viewPath)) {
        //   $view.html      view 
        include_once $viewPath;
    }
}

Now for a small example, let’s say we have a template top.htmland a template foo.html

And we write a news output script, then we do something like this

Script news.php

//   
$dbResult = 'SELECT * FROM news';
$newsAll = array();
while( $row = mysql_fetch_assoc($dbResult) ) { 
    $newsAll[] = $row; //     
}
//   
viewInit('newsAll'); //   newsAll.

Template itself:

<!--   html' -->
<?viewInit('top')?>
<!--       -->
<div id="newsAll">
    <?foreach($newsAll as $news):?>
       <div class="newsTitlte">
         <?=$news['title']?>
       </div>
       <div class="newsContent">
         <?=$news['content']?>
       </div>
    <?endforeach?>
</div>
<!--   -->
<?viewInit('foo')?>

Actually, the simplest thing I could think of, if you are interested in an OOP implementation – ask.

It was on the question of php code in “pure html” – in my opinion I found such a module for Apache.
PS: a web server is definitely needed…

PPS: as you might guess, nesting of templates can be any, i.e. for example, other templates can be included in the same top.html, such as preheader, header, topnav, etc.


Answer 3, authority 50%

Well, you can also add to .htaccess:

AddType x-httpd-php .html .htm

And then .html will be interpreted like .php.


Answer 4, authority 50%

If you can’t change the main file extension from .html to .php:

include virtual=”file.php”

A line is written in the right place in the html file IN COMMENTS. Instead of “file.php” there can be any file (including one more html).


Answer 5

at least the main file that will include the blocks must be saved in php format, otherwise it won’t understand the <?php include ‘header.php’ ?> construct. And since the main one is in php, then you already need a web server.