0

jQuery code:

function sorting(){
  $(".search_result_employee").load("include/searchedResult.php", {sort: "SORT_DESC"});
}

php code:

$array = file("data/employees.txt");



$row = intdiv(count($array), 4);
if (count($array)%4 != 0){
    $row++;
}

//$row = round(count($array)/4);
$x = $z = 0;
for ($x; $x < $row; $x++){
    echo '<div class="row my-4">';
    for ($y=0; $y <= 3; $y++){
        if (isset($array[$z])) {
            $name = explode("| ", $array[$z]);
            $eName = $name[0];
            $eDepartment = $name[7];
            $eProfile = end($name);

            echo '
            <div class="col-md">
                <a href="displayInfo.php?name='."$eName".'" class="grey_bg p-2 m-1 employeeCard">
                    <img src="employeesProfile/'."$eProfile".'" alt="profile" class="profile"/>
                    '."$eName".'<br/>
                        <span>'."$eDepartment".'</span>
                </a>
            
            </div>
            ';
        }
        $z++;
        
    }
    echo '</div>';
}

First go in the page using

<?php include("include/searchedResult.php"); ?>

The page can found the file. Howver, after clicked the button, the jQuery load() the include/searchResu;t.php page. Then, the file doesn't exist. First time is working properly thensecond time can't. Anyone know how to solve this?

this is the error:

Warning: file(data/employees.txt): failed to open stream: No such file or directory in C:\xampp\htdocs\COS30020\include\searchedResult.php on line 14

4
  • the include directory is likely not availabele to the browser - look in the network tab Commented Oct 4, 2021 at 8:48
  • 1
    Please share more details. Are you facing any error message? Also, the given code does not echo the loaded file, so adding it to the markup will obviously not work Commented Oct 4, 2021 at 8:50
  • the text file. Second time can't find the text file Commented Oct 4, 2021 at 8:57
  • My guess is that you have the php in two places and when you load from /include, the data is not in the same directory Commented Oct 4, 2021 at 8:58

1 Answer 1

2

When including files in PHP, all relative paths will be relative from the first entry PHP-file.

If index.php includes searchResult.php, then file('data/employees.txt') will be relative from the index.php-file, not the searchResult.php-file.

If you call the searchResult.php-file directly, then it will be relative from searchResult.php (which then is the entry file).

Solve this by using the magic constant __DIR__, which contains the absolute path to the file it's written in.

So it should be:

file(__DIR__ . '/relative/path/from/this/file/data/employees.txt')
Sign up to request clarification or add additional context in comments.

2 Comments

this is what I want! Really thank you sir. Never think about this before
@VoonTaoTan - If the answer solved your issue, please accept it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.