简体   繁体   English

PHP OOP仅访问文本文件的第一行

[英]PHP OOP only accessing first line of text file

I am doing an assignment for school, and I am having an odd problem. 我正在做学校作业,但遇到了一个奇怪的问题。 The object that I have created only accesses the first line of a text file we are using. 我创建的对象仅访问我们正在使用的文本文件的第一行。 This is highly problematic for me because if I build a database and can only use the first line, well, so much for usefulness. 这对我来说是个很大的问题,因为如果我建立一个数据库并且只能使用第一行,那么这样做很有用。

My instructor and I both can't figure out why this problem is here, and we have worked on this for over a week. 我和我的教练都无法弄清楚为什么会出现此问题,并且我们已经为此工作了一个多星期。

Here is the text file: 这是文本文件:

003345, Pauline, Sampson, Admin, 14.96
012345, Mike, King, Manager, 20.47
123456, Pete, Smith, Accountant, 25.53
345678, Mary, Jones, Accountant, 32.53
456789, Mary, King, Manager, 18.35
777999, Caroline, Baxter, Nurse, 27.45

Here is my start HTML: 这是我的起始HTML:

 <!DOCTYPE html> <html> <head> <title>Modify1.html</title> <link rel ="stylesheet" type="text/css" href="sample.css" /> </head> <body> <h1>Weekly Pay report</h1> <form action="modify1.php" method="post"> <table> <tr><td>Employee ID 1</td><td><input type="text" name="id1"></td></tr> <tr><td>Employee ID 2</td><td><input type="text" name="id2"></td></tr> <tr><td>Employee ID 3</td><td><input type="text" name="id3"></td></tr> </table> <p><input type = "submit" value = "Submit"> </form> </body> </html> 

Here is my PHP output: 这是我的PHP输出:

 <!DOCTYPE html> <html> <head> <title>Modify 1</title> <link rel ="stylesheet" type="text/css" href="sample.css" /> </head> <body> <?php include("inc-employee-object.php"); $id1 = $_POST["id1"]; $id2 = $_POST["id2"]; $id3 = $_POST["id3"]; $emp1 = new Employee(); $emp2 = new Employee(); $emp3 = new Employee(); $emp1->findEmployee($id1); $emp2->findEmployee($id2); $emp3->findEmployee($id3); print ("<p>Weekly Pay for ".$emp1->getFirstName()." ". $emp1->getLastName().": $".$emp1->getWeeklyPay()."</p>"); print ("<p>Weekly Pay for ".$emp2->getFirstName()." ". $emp2->getLastName().": $".$emp2->getWeeklyPay()."</p>"); print ("<p>Weekly Pay for ".$emp3->getFirstName()." ". $emp3->getLastName().": $".$emp3->getWeeklyPay()."</p>"); ?> </body> </html> 

And here is my object: 这是我的对象:

 <?php class Employee { private $empID; private $firstName; private $lastName; private $jobTitle; private $hourlyWage; public function addEmployee() { $empRecord = $this->empID.", ".$this->firstName.", ".$this->lastName.", ".$this->jobTitle.", ".$this->hourlyWage."\\n"; $empFile = fopen("employees.txt", "a"); fputs($empFile, $empRecord); fclose($empFile); } public function findEmployee($id) { $empFile = fopen("employees.txt", "r"); $empRecord = fgets($empFile); $notFound = true; while (!feof($empFile) and $notFound) { list ($empID, $fName, $lName, $title, $wage) = explode(",", $empRecord); if ($id == $empID) { $this->empID = $empID; $this->firstName = $fName; $this->lastName = $lName; $this->jobTitle = $title; $this->hourlyWage = $wage; $notFound = false; } if ($notFound == false) { return 1; } else { return 0; } $empRecord = fgets($empFile); } fclose($empFile); } public function getID() { return $this->empID; } public function setID($empID) { $this->empID = $empID; } public function getFirstName() { return $this->firstName; } public function setFirstName($fName) { $this->firstName = $fName; } public function getLastName() { return $this->lastName; } public function setLastName($lName) { $this->lastName = $lName; } public function getJobTitle() { return $this->jobTitle; } public function setJobTitle($title) { $this->jobTitle = $title; } public function getHourlyWage() { return $this->hourlyWage; } public function setHourlyWage($hourlyWage) { $this->hourlyWage = $hourlyWage; } public function getWeeklyPay() { $this->hourlyWage = trim($this->hourlyWage); return number_format ($this->hourlyWage * 40, 2); } public function getAnnualPay() { $this->hourlyWage = trim($this->hourlyWage); return number_format($this->hourlyWage * 40 *52,2); } } // end of class definition ?> 

Can anyone tell me what I'm doing wrong? 谁能告诉我我在做什么错?

Thanks 谢谢

In your while loop, you will always exit on the first item due to this code... 在while循环中,由于此代码,您将始终在第一项上退出...

    if ($notFound == false)
    {
        return 1;
    }
    else
    {
        return 0;
    }

Under any condition, this will exit out of the loop and function. 在任何情况下,这都会退出循环并起作用。 This should be outside the loop and probably at the end of the function. 这应该在循环之外,并且可能在函数末尾。

If you want to stop the loop once the record is found, then use break; 如果要在找到记录后停止循环,请使用break; something like... 就像是...

    if ($id == $empID)
    {
        $this->empID = $empID;
        $this->firstName = $fName;
        $this->lastName = $lName;
        $this->jobTitle = $title;
        $this->hourlyWage = $wage;
        $notFound = false;
        break;   // Exit loop
    }

@NigelRen is right about the loop exiting once a condition is found. @NigelRen关于在发现条件后退出循环是正确的。 But that may be by your design and should not matter in this case as you are calling the class separately for each id. 但这可能是由您设计的,在这种情况下应该无关紧要,因为您将分别为每个id调用类。

If you look at the your text file you are missing commas in between the wage and the employee id's. 如果查看您的文本文件,则工资和员工ID之间缺少逗号。 So it is only seeing the first row and not the others. 因此,它仅看到第一行,而不看到其他行。

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM