3

I'm trying to access my function.php file so I'll be able to use the functions inside it. But I can't connect it. I tried running the PHP alone and there's no problem encountered. But when I include it on my HTML, nothings happening.

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

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
juju17
  • 271
  • 4
  • 15

3 Answers3

5

"But when I include it on my HTML, nothings happening."

You need to instruct Apache to treat .html files as PHP.

If you haven't, then do. .html files do not parse PHP directives by default.

Consult: Using .htaccess to make all .html pages to run as .php files?

and create a file called .htaccess and placed inside the root of your server with the following content:

AddHandler application/x-httpd-php .html

If and when you do decide to use the .htaccess method, remember to restart your server/services/Apache/PHP. Those changes won't take effect until you do.

Plus, make sure that you do have a webserver/PHP installed and is properly configured.

If you're trying to access your files in a web browser such as:

c://file.html or c://file.php, then that won't work.

It must be used like this, e.g.:

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

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

Also make sure the path to your include is correct.

Add error reporting to the top of your file(s) which will help find errors.

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

// rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • "Create a .htaccess file at the root of your website and add this line: down vote accepted Create a .htaccess file at the root of your website and add this line: [Apache2 @ Ubuntu/Debian: use this directive] AddType application/x-httpd-php .html .htm If your are running PHP as CGI (probably not the case), you should write instead: AddHandler application/x-httpd-php .html .htm " Sir, I quite don't get this. Does creating a .htaccess file is the same as creating html and php? And also, What does he meant when he say : "Create a .htaccess file at the root of your website and add this line" – juju17 Dec 26 '15 at 18:45
  • @user4932301 It's included in the link I added in my answer http://stackoverflow.com/questions/4687208/using-htaccess-to-make-all-html-pages-to-run-as-php-files – Funk Forty Niner Dec 26 '15 at 18:46
  • you create a text-based file `.htaccess` (with the dot like that) and place it in the root of your server. If you don't want to go through that trouble, then just rename your `.html` file extension to `.php`. Like I mentioned in my answer, a web browser won't parse PHP directives by default, a server does that. – Funk Forty Niner Dec 26 '15 at 18:49
  • @user4932301 If and when you do decide to use the `.htaccess` method, remember to restart your server/services/Apache/PHP. Those changes won't take effect until you do. – Funk Forty Niner Dec 26 '15 at 18:53
  • One last question, where is the "root" of a server? It may sounds stupid sorry :P. Still learning :) – juju17 Dec 26 '15 at 19:01
  • @user4932301 that will depend on what your public folder is named as. Some are called htdocs, some are called public, or public_html. Yours seems to be called `htdocs`. – Funk Forty Niner Dec 26 '15 at 19:02
  • 1
    It's working now! Thanks Fred -ii- ! You're a big help in stackoverflow :D – juju17 Dec 26 '15 at 19:03
  • 1
    @user4932301 You're most welcome (and thanks), *cheers* glad I was of help. Happy coding/happy holidays/New Year ;-) – Funk Forty Niner Dec 26 '15 at 19:04
  • Dears I have found this question as the closer to my question on serverfault, if you kindly can reply https://serverfault.com/questions/1065400/apache-2-4-php-7-4-include-and-require-dont-include-php-files-but-others-files – Robert Jun 02 '21 at 09:13
3

Try this, And also change your HTML file format to .php. I hope it will work.

<?php 
include("../functions/function.php");
?>
halfer
  • 19,824
  • 17
  • 99
  • 186
Muhammad Haseeb
  • 1,269
  • 12
  • 22
1

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

Also you need to rename the .html file to .php (if not diffrently configured in your server)

CoderPi
  • 12,985
  • 4
  • 34
  • 62