Unable to include php file in my html code

I am trying to access my function.php file so that I can use the functions inside it. But I can’t connect it. I tried to run only PHP, and there were no problems. But when I include it in my HTML, nothing happens.

HTML code:

 <!DOCTYPE html>
<?php 
include("functions/function.php");
?>
<html>
    <head>
    <title>Online Shop</title>
    <link rel='stylesheet' href='../CSS/style.css' media="all"/>
    </head>
<body>  
            <!--Main Wrapper starts here!-->
    <div class="main_wrapper"> 

            <!--Header Wrapper starts here!-->
        <div class="header_wrapper" >
        <img id="logo" src="../Pictures/logo.png"/>
        <img id="banner" src="../Pictures/green-banner.jpg"/>
        </div>
            <!--Header ends here!-->

            <!--Menu Bar starts here!-->
        <div class="menubar">
        <ul id="menu">
            <li><a href="#"> Home </a></li> 
            <li><a href="#"> All Products </a></li>
            <li><a href="#"> My Account </a></li>
            <li><a href="#"> Sign Up </a></li>
            <li><a href="#"> Shopping Cart </a></li>
            <li><a href="#"> Contact Us  </a></li>
        </ul>
        <div id="form">
            <form method="get" action="results.php" enctype="multipart/form-data">
                <input type="text" name="user_query"  placeholder="Search Product"/>
                <input type="submit" name="search" value="search"/>
            </form> 
        </div>
        </div><!--Menubar ends here!-->

            <!--Content Wrapper here!-->
        <div class="content_wrapper">
            <div id="sidebar"> 
                <div id="sidebar_title"> Categories</div>
                <ul id="cats">
                    <?php getCats(); ?>             
                </ul>   
            </div>
            <div id="content_area">THis is asa content</div>
        </div><!--Content Wrapper ends here!-->
        <div id="footer">  </div>
    </div><!--Wrapper-->
</body>
</html>

PHP CODE:

<?php

$con = mysqli_connect("localhost","root","","ecommerce");
//Getting Categories
function getCats(){

global $con;
$get_cats = "Select * from categories";

$run_cat = mysqli_query($con, $get_cats);

while ($row_cats=mysqli_fetch_array($run_cat)){

    $cat_id = $row_cats['cat_id'];
    $cat_title = $row_cats['cat_title'];
echo"<li><a href='#'>$cat_title</a></li>";
}
} //getCats() ENDS HERE
?>

Additional Information:

My HTML path: C: /xampp/htdocs/ProjectWebDev/HTML/index.html

My PHP path: C: /xampp/htdocs/ProjectWebDev/functions/function.php

+4
source share
3 answers

"But when I include it in my HTML, nothing happens."

You need to tell Apache to process files .htmllike PHP.

, . .html PHP .

: .htaccess, .html .php ?

.htaccess :

AddHandler application/x-httpd-php .html

.htaccess, //Apache/PHP. , .

, , -/PHP .

-, :

c://file.html c://file.php, .

, :

  • http://localhost|example.com/file.html

  • http://localhost|example.com/file.php

, .

, .

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: .

+5

, HTML .php. , .

<?php 
include("../functions/function.php");
?>
+2

It seems like the path is wrong, you should use include("../functions/function.php");.

In addition, you need to rename the file .htmlto .php(if not configured on your server)

+1
source

Source: https://habr.com/ru/post/1621802/


All Articles