0

I am trying to call a custom made function (not mysql_* as in other questions suggested) from the index page. The index page is calling three custom functions. The first two are working correctly but the third function is throwing and error:

Fatal error: Uncaught Error: Call to undefined function display_index1() in F:\Apache24\htdocs\Site1\index.php:45 Stack trace: #0 {main} thrown in F:\Apache24\htdocs\Site1\index.php on line 45

This is my index page which is calling the function:

 <!DOCTYPE html>
    <?php 
    include("includes/connect.php");
    include("includes/functions.php"); 
    ?>
    <html>
    <head>
        <title>Ecommerce Website</title>
        <link rel="stylesheet" type="text/css" href="styles/style.css">
        <script src="js/style.js"></script>
    <body>
        <div class="container-fluid">
            <header>
                <div class="form">
                    <form method="get" target="" name="searchbar_form">
                        <input type="text" name="searchbar" id="searchbar">
                        <input type="submit" id="search_button" value="Search">
                    </form>
                </div>
            </header>
            <div class="menubar">

                    <div class="dropdown">
                        <button onclick="dropdownToggle()" class="dropdown-button">Shop By Category</button>

                            <ul class="dropdown-content" id="dropdownContent">
                                Categories
                                <?php getcats(); ?>
                                Brands
                                <?php getbrands(); ?>
                            </ul>

                    </div>
                <div class="menubar-div">
                    <ul class="menu-items">
                        <?php getcats(); ?>
                    </ul>
                </div>
                <div class="cart">
                    <a href="#">Cart</a>
                </div>
            </div>
            <div class="content">
                <div class="index_product_row">
                    <?php display_index1(); ?>
                </div>
            </div>
        </div>
        <div class="footer">
                This is footer
        </div>
    </body>
    </html>

This is the functions.php file which has the three called functions:

<?php 

    function getcats(){
        global $con;

        $get_cats="select * from categories";
        $run_cats=mysqli_query($con, $get_cats);

        while($row_cats=mysqli_fetch_array($run_cats)){
            $cat_id = $row_cats['cat_id'];
            $cat_title = $row_cats['cat_title'];

            echo "<li><a href='#'>$cat_title</a></li>";
        }
    }



    function getbrands(){
        global $con;

        $get_brands="select * from brands";
        $run_brands=mysqli_query($con, $get_brands);

        while($row_brands=mysqli_fetch_array($run_brands)){
            $brand_id = $row_brands['brand_id'];
            $brand_title = $row_brands['brand_title'];

            echo "<li><a href='#'>$brand_title</a></li>";
        }
    }


    function display_index1(){
        global $con;

        $display_query = "select(product_image, product_title) from products where product_cat='1'";
        $display_result = mysqli_query($con, $display_query);

        while($result_rows = mysqli_fetch_array($display_result)){
            $product_image = $result_rows['product_image'];
            $product_title = $result_rows['product_title'];

            echo "<img src='/admin/product_images/".$product_image."' width='200' height='200' alt='".$product_title."'/>";
            echo "<span id='index_product_title'>".$product_title."</span>";
        }
    }

?>

The first two functions are returning data successfully. Calling the third is returning the error.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
vilom
  • 51
  • 3
  • 9
  • 5
    Are you sure you're including the correct file? Put an `echo 'FUNCTIONS DEFINED';` after `function display_index1() { ... }`; does that message appear? – deceze Oct 14 '16 at 07:34
  • Or use a debugger with a break point if you want to code clean. – Xenos Oct 14 '16 at 07:41
  • 1
    Since `functions.php` seems to be an important file, use `require()` so that if the file doesn't exist, your application will send a `FATAL`, instead of a notice and continue without the file. (`include` is less strict) http://stackoverflow.com/questions/3633900/difference-between-include-and-require-in-php – ʰᵈˑ Oct 14 '16 at 07:42
  • Thanks @hd. The require() solved the problem. There was an old duplicate file that was causing the first two functions to work but third was in the latest file that's why it was not working. – vilom Oct 14 '16 at 08:44
  • 1
    https://img.buzzfeed.com/buzzfeed-static/static/enhanced/terminal01/2011/6/15/14/enhanced-buzz-5551-1308162992-8.jpg – deceze Oct 14 '16 at 10:03

1 Answers1

0

There was an old duplicate file that was causing the first two functions to work but third was in the latest file that's why it was not working.

vilom
  • 51
  • 3
  • 9