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

时间:2018-03-08 07:19:30

标签: php oop

我正在为学校做作业,我遇到了一个奇怪的问题。我创建的对象只访问我们正在使用的文本文件的第一行。这对我来说是非常有问题的,因为如果我构建一个数据库并且只能使用第一行,那么就是有用了。

我的导师和我都无法弄清楚为什么会出现这个问题,我们已经为此工作了一个多星期。

这是文本文件:

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

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

由于

2 个答案:

答案 0 :(得分:1)

在您的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
    }

答案 1 :(得分:-1)

@NigelRen在找到条件后退出循环是正确的。但这可能是你的设计,在这种情况下无关紧要,因为你是为每个id单独调用类。

如果查看文本文件,则工资和员工ID之间缺少逗号。所以它只看到第一行而不是其他行。

干杯!