Reputation: 5
So in this code all I am trying to do is create a view chores page. I have created a chore class and a choreDAO class, which my view_chores page is calling from.
I have used the exact same code with other pages such as view_members and they also have the other two classe and they work fine!
The error occurs in the view_chores.php file below; this line of code:
echo "<b>Title:</b> " . $chore->getChoreName() . "<br />";
any help would be great. thank you!
Here is my view_chores.php page.
<?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(); ?>
Here is my 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; }
}
?>
and here is my 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;
}
}
?>
Upvotes: 0
Views: 8634
Reputation: 983
Also, look at shouldn't Chore::setChoreName()
Shouldn't $this->ChoreName
be $this->chore_name
?
Upvotes: 0
Reputation: 24549
In your ChoreDAO class getChores() method, you are using this:
$chore= new ChoreDAO($i, $chore_name);
Where it should be:
$chore= new Chore($i, $chore_name);
Upvotes: 3
Reputation: 983
getChoreName() exists in Chore. So, Chore::getChoreName() exists but you aren't using that class. Perhaps you meant for ChoreDAO to extend the Chore class?
Upvotes: 0