Search code examples
phpfilerequirerequire-once

Can anyone help me to make require working?


So I have 2 php files, one is calling function other is implementing the functions.

In the one that is implementing the functions I call require("shares.php") - shares.php is file that calls the functions

The problem is that, it's not seeing the functions, so I made a check to see if the functions exists and No, they do not exists ((.

Please see here.

shares.php

    <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'> 
<html xmlns='http://www.w3.org/1999/xhtml'> 
<head> 
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' /> 
<title>Shares</title> 
</head> 

<body> 
<?php

if (function_exists('get_contents')) 
{ 
    if(function_exists('print_contents'))
    {
        $data = get_contents();
        print_contents($data);
    }
}

else
{
    print("Not Found");
}
?>
</body> 
</html> 

And here are the implementation of functions.

       if(isset($_GET['share']))
        {
            $conn = db_connect();
            if($_GET["newSorting"] == 1)
            {

                 $shares = get_shareSearch($conn, "company");

            }

            if($_GET["newSorting"] == 2)
            {

                $shares = get_shareSearch($conn, "rate");

            }
            if($_GET["newSorting"] == 3)
            {

                 $shares = get_shareSearch($conn, "issue_date");

            }

            db_disconnect($conn);
            $contents = array('shares' => $shares);
            return $contents;
        }

        else
        {
            $conn = db_connect();
            $shares = get_share($conn);
            db_disconnect($conn);
            $contents = array('shares' => $shares);
            return $contents;
        }

    }

    function print_contents($contents) 
    {

        if(count($contents['shares']) == 0)
        {
            echo "<script type = 'text/javascript'>alert('Sorry but share is not found! Q_Q');</script>";

        }
        else
        {
        ?>    
            <table>
                <tr>
                    <th><a href="share-company-links.php?companySort=true">Company Name</a></th>
                    <th><a href="share-company-links.php?rateSort=true">Rate</a></th>
                    <th><a href="share-company-links.php?issueSort=true">Issue date</a></th>
                </tr>
        <?php

        foreach ($contents['shares'] as $share) 
        {
            print "<tr>";
            $identifier = urlencode($share['COMPANY']);
            print "<td><a href='share-details.php?company={$identifier}'>{$share['COMPANY']}</a></td>";
            print "<td>{$share['RATE']}</td>";

            $issue_date = $share['ISSUE_DATE'];
            $issue_date = $issue_date === NULL ? "&lt; not available &gt;" : $issue_date;
            print "<td>{$issue_date}</td>";
            print "</tr>";
        }
        ?>
            </table>
        <?php
        }
    }

    require_once("shares.php");

    ?>

The result will be Not Found.


Solution

  • The file that uses these functions (share.php) should require the other file, not the other way around. So, in shares

    <body> 
    <?php
    
    require_once('thatotherfile.php');
    
    if (function_exists('get_contents')) 
    { 
        if(function_exists('print_contents'))
        {
            $data = get_contents();
            print_contents($data);
        }
    }
    

    will work.