所以在这段代码中,我要做的就是创建一个视图杂项页面。 我创建了一个杂项类和一个choreDAO类,我的view_chores页面正在调用它。
我使用了与其他页面完全相同的代码,例如view_members,它们还有其他两个classe,它们工作正常!
错误发生在下面的view_chores.php文件中;这行代码:
echo "<b>Title:</b> " . $chore->getChoreName() . "<br />";
任何帮助都会很棒。谢谢!
这是我的view_chores.php页面。
<?php
ob_start();
require_once 'includes/Member.php';
require_once 'includes/MemberDAO.php';
require_once 'includes/Chore.php';
require_once 'includes/ChoreDAO.php';
require_once 'includes/session.php';
confirm_logged_in(); // needs to come before any html because it does a redirect
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
require 'toolbar.php';
$member = ($_SESSION['member']);
$choreDAO = new ChoreDAO();
$chores = $choreDAO->getChores();
echo "<p>Hello " . $member->getFN() . "</p>";
echo "<p>These are the current chores: </p>";
foreach ($chores as $chore) {
echo "<b>Title:</b> " . $chore->getChoreName() . "<br />";
echo "</p>";
}
echo $display; ?>
<a href="add_chore_form.php">Add Chore?</a>
</body>
</html>
<?php ob_flush(); ?>
这是我的Chore.php
<?php
class Chore {
private $id;
private $chore_name;
public function __construct($i, $chore_name) {
$this->id = $i;
$this->chore_name = $chore_name;
}
public function getId() { return $this->id; }
public function getChoreName() { return $this->chore_name; }
public function setId($i) { $this->id = $i; }
public function setChoreName($cn) { $this->ChoreName = $cn; }
}
?>
这是我的ChoreDAO.php
<?php
require_once 'DAO.php';
class ChoreDAO extends DAO {
public function __construct() {
parent::__construct();
}
public function insert($chore) {
if (!isset($chore)) {
throw new Exception("Chore required");
}
$sql = "INSERT INTO Chore(chore_name) VALUES (?)";
$params = array($chore->getChoreName());
$stmt = $this->link->prepare($sql);
$status = $stmt->execute($params);
if ($status != true) {
$errorInfo = $stmt->errorInfo();
throw new Exception("Could not save Chore: " . $errorInfo[2]);
}
$sql = "SELECT LAST_INSERT_ID()";
$stmt = $this->link->prepare($sql);
$status = $stmt->execute();
if ($status != true) {
$errorInfo = $stmt->errorInfo();
throw new Exception("Could not retrieve new chore's id: " . $errorInfo[2]);
}
$row = $stmt->fetch();
$id = $row[0];
$chore->setId($id);
}
public function delete($chore) {
if (!isset($chore)) {
throw new Exception("Chore required");
}
$id = $chore->getId();
if ($id == null) {
throw new Exception("Chore id required");
}
$sql = "DELETE FROM Chore WHERE id = ?";
$params = array($chore->getId());
$stmt = $this->link->prepare($sql);
$status = $stmt->execute($params);
if ($status != true) {
$errorInfo = $stmt->errorInfo();
throw new Exception("Could not delete Chore: " . $errorInfo[2]);
}
}
public function update($chore) {
if (!isset($chore)) {
throw new Exception("Chore required");
}
$id = $chore->getId();
if ($id == null) {
throw new Exception("Chore id required");
}
$sql = "UPDATE Chore SET chore_name = ? WHERE id = ?";
$params = array($chore->getChoreName());
$stmt = $this->link->prepare($sql);
$status = $stmt->execute($params);
if ($status != true) {
$errorInfo = $stmt->errorInfo();
throw new Exception("Could not update Chore: " . $errorInfo[2]);
}
}
public function getChore($id) {
$sql = "SELECT * FROM Chore WHERE id = ?";
$params = array($id);
$stmt = $this->link->prepare($sql);
$status = $stmt->execute($params);
if ($status != true) {
$errorInfo = $stmt->errorInfo();
throw new Exception("Could not retrieve Chore: " . $errorInfo[2]);
}
$chore = null;
if ($stmt->rowCount == 1) {
$row = $stmt->fetch();
$id = $row['id'];
$chore_name = $row['house_name'];
$chore = new ChoreDAO($id, $chore_name);
}
return $chore;
}
public function getChores() {
$sql = "SELECT * FROM Chore";
$stmt = $this->link->prepare($sql);
$status = $stmt->execute();
if ($status != true) {
$errorInfo = $stmt->errorInfo();
throw new Exception("Could not retrieve chores: " . $errorInfo[2]);
}
$chores = array();
$row = $stmt->fetch();
while ($row != null) {
$id = $row['id'];
$chore_name = $row['chore_name'];
$chore= new ChoreDAO($i, $chore_name);
$chores[$id] = $chore;
$row = $stmt->fetch();
}
return $chores;
}
}
?>
答案 0 :(得分:3)
在你的ChoreDAO类getChores()方法中,你正在使用它:
$chore= new ChoreDAO($i, $chore_name);
它应该在哪里:
$chore= new Chore($i, $chore_name);
答案 1 :(得分:0)
在Chore中存在getChoreName()。 所以,Chore :: getChoreName()存在但你没有使用那个类。也许你认为ChoreDAO可以扩展Chore类?
答案 2 :(得分:0)
另外,看看应该不是Chore :: setChoreName()
不应$this->ChoreName
为$this->chore_name
?