在回覆中$numOfEmployees
每次提交新表單時都是一樣的。我在這裏做錯了什麼?我試圖每次將它保存到會話中,但仍然無法正常工作。我是新來的PHP,到目前爲止還不是最大的粉絲爲什麼這個PHP變量不會按每個請求遞增?
<?php
class Employee{
private $fn;
private $ln;
private $dpt;
private $ID;
public function setVars($fna, $lna, $dpta, $numOfEmployeesa){
$this-> fn = $fna;
$this -> ln = $lna;
$this -> dpt = $dpta;
$this -> ID = $numOfEmployeesa;
}
}
if(isset($_SESSION['numOfEmployees'])){
//get it
$numOfEmployees = $_SESSION['numOfEmployees'];
} else {
//set a default value if not isset
$numOfEmployees = 0;
}
if(isset($_SESSION['employeeArray'])){
//get it
$employeeArray = $_SESSION['employeeArray'];
} else {
//set a default value if not isset
$employeeArray = array();
}
$employee = new Employee();
$fn = $_POST['firstname'];
$ln = $_POST['lastname'];
$dpt = $_POST['department'];
$employee -> setVars($fn, $ln, $dpt, $numOfEmployees);
$numOfEmployees++;
echo "First Name: " . $fn . "\nLast Name: " . $ln . "\nDepartment: " . $dpt . "\nID: " . sprintf('%08d', $numOfEmployees) . "\nNumber of employees: " . $numOfEmployees;
$employeeArray[] = $employee;
$_SESSION['employeeArray'] = $employeeArray;
$_SESSION['numOfEmployees'] = $numOfEmployees;
?>
需要更換'$ someValue中='和'$ numOfEmployees ='和'$ employeeArray ='在你的兩個if語句 – cmorrissey
要使用會話,你需要調用'session_start' –
謝謝我修復了變量仍然不能正常工作 – Zachscs