繁体   English   中英

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

[英]PHP OOP only accessing first line of 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

这是我的起始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> 

这是我的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> 

这是我的对象:

 <?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 ?> 

谁能告诉我我在做什么错?

谢谢

在while循环中,由于此代码,您将始终在第一项上退出...

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

在任何情况下,这都会退出循环并起作用。 这应该在循环之外,并且可能在函数末尾。

如果要在找到记录后停止循环,请使用break; 就像是...

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

@NigelRen关于在发现条件后退出循环是正确的。 但这可能是由您设计的,在这种情况下应该无关紧要,因为您将分别为每个id调用类。

如果查看您的文本文件,则工资和员工ID之间缺少逗号。 因此,它仅看到第一行,而不看到其他行。

干杯!

暂无
暂无

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

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