示例#1
0
	public function save() {
		$name = $this -> input -> post("name");
		$province = $this -> input -> post("province");
		$latitude = $this -> input -> post("latitude");
		$longitude = $this -> input -> post("longitude");
		$years = $this -> input -> post("years");
		$populations = $this -> input -> post("populations");
		$district_id = $this -> input -> post("district_id");

		//Check if we are in editing mode first; if so, retrieve the edited record. if not, create a new one!
		if (strlen($district_id) > 0) {
			$district = Districts::getDistrict($district_id);
			//also, retrieve the district-population combinations for future comparisons. Delete the old ones
			$district_populations = District_Populations::getAllForDistrict($district_id);
			//Delete all these existing combinations
			foreach ($district_populations as $district_population) {
				$district_population -> delete();
			}
		} else {
			$district = new Districts();
		}

		$district -> name = $name;
		$district -> province = $province;
		$district -> latitude = $latitude;
		$district -> longitude = $longitude;
		$district -> save();
		$district_id = $district -> id;
		$counter = 0;
		//Loop to get all the year-population combinations. Only add the ones that have actual values
		foreach ($years as $year) {
			if (strlen($year) > 1) {
				$district_population = new District_Populations();
				$district_population -> name = $name;
				$district_population -> population = $populations[$counter];
				$district_population -> year = $years[$counter];
				$district_population -> district_id = $district_id;
				$district_population -> save();
				$counter++;
			} else {
				$counter++;
				continue;
			}

		}
		redirect("district_management");
	}