I was wondering if anyone could help me with this problem i’m having.
I have a ReminderDAO class with methods to delete, edit, insert etc and a Reminder class with a constructor and get and sets.
I then have a a view reminders where it just lists all the reminders out.
I want to be able to add an edit and delete to this view page.
To use the delete and edit functions in my ReminderDAO class, i need to pass a reminder object through the function and i’m not quite sure how to do this.
If anyone could help me that would be of great help, i’m new to this language so i apologise if it’s not great code.
Thank you in advance!
Reminder DAO
class ReminderDAO extends DAO {
public function __construct() {
parent::__construct();
}
public function insert($reminder) {
if (!isset($reminder)) {
throw new Exception("Reminder required");
}
$sql = "INSERT INTO Reminders(member_id, title, details, reminder_type) VALUES (?, ?, ?, ?)";
$params = array($reminder->getMember_id(), $reminder->getTitle(), $reminder->getDetails(), $reminder->getType());
$stmt = $this->link->prepare($sql);
$status = $stmt->execute($params);
if ($status != true) {
$errorInfo = $stmt->errorInfo();
throw new Exception("Could not save Reminder: " . $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 reminder's id: " . $errorInfo[2]);
}
$row = $stmt->fetch();
$id = $row[0];
$reminder->setId($id);
}
public function delete($reminder) {
if (!isset($reminder)) {
throw new Exception("Reminder required");
}
$id = $reminder->getId();
if ($id == null) {
throw new Exception("Reminder id required");
}
$sql = "DELETE FROM Reminders WHERE id = ?";
$params = array($reminder->getId());
$stmt = $this->link->prepare($sql);
$status = $stmt->execute($params);
if ($status != true) {
$errorInfo = $stmt->errorInfo();
throw new Exception("Could not delete reminder: " . $errorInfo[2]);
}
}
public function update($reminder) {
if (!isset($reminder)) {
throw new Exception("Reminder required");
}
$id = $reminder->getId();
if ($id == null) {
throw new Exception("Reminder id required");
}
$sql = "UPDATE Reminders SET member_id = ?, title = ?, details = ?, reminder_type = ? WHERE id = ?";
$params = array($reminder->getMember_id(), $reminder->getTitle(), $reminder->getDetails(), $reminder->getType());
$stmt = $this->link->prepare($sql);
$status = $stmt->execute($params);
if ($status != true) {
$errorInfo = $stmt->errorInfo();
throw new Exception("Could not update Reminder: " . $errorInfo[2]);
}
}
public function getReminder($id) {
$sql = "SELECT * FROM Reminders 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 Reminder: " . $errorInfo[2]);
}
$reminder = null;
if ($stmt->rowCount == 1) {
$row = $stmt->fetch();
$id = $row['id'];
$member_id = $row['member_id'];
$title = $row['title'];
$details = $row['details'];
$type = $row['reminder_type'];
$reminder = new ReminderDAO($id, $member_id, $title, $details, $type);
}
return $reminder;
}
public function getReminders() {
$sql = "SELECT * FROM Reminders";
$stmt = $this->link->prepare($sql);
$status = $stmt->execute();
if ($status != true) {
$errorInfo = $stmt->errorInfo();
throw new Exception("Could not retrieve reminders: " . $errorInfo[2]);
}
$reminders = array();
$row = $stmt->fetch();
while ($row != null) {
$id = $row['id'];
$member_id = $row['member_id'];
$title = $row['title'];
$details = $row['details'];
$type = $row['reminder_type'];
$reminder = new Reminder($id, $member_id, $title, $details, $type);
$reminders[$id] = $reminder;
$row = $stmt->fetch();
}
return $reminders;
}
}
?>
Reminder Class
<?php
class Reminder {
private $id;
private $member_id;
private $title;
private $details;
private $reminder_type;
public function __construct($i, $m_id, $title, $det, $type) {
$this->id = $i;
$this->member_id = $m_id;
$this->title = $title;
$this->details = $det;
$this->reminder_type = $type;
}
public function getId() { return $this->id; }
public function getMember_id() { return $this->member_id; }
public function getTitle() { return $this->title; }
public function getDetails() { return $this->details; }
public function getType() { return $this->reminder_type; }
public function setId($i) { $this->id = $i; }
public function setMember_id($mID) { $this->member_id = $mID; }
public function setTitle($t) { $this->title = $t; }
public function setDetails($d) { $this->details = $d; }
public function setType($type) { $this->reminder_type = $type; }
}
?>
View Reminders
<?php
ob_start();
require_once 'includes/Member.php';
require_once 'includes/MemberDAO.php';
require_once 'includes/Reminder.php';
require_once 'includes/ReminderDAO.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']);
$reminderDAO = new ReminderDAO();
$reminders = $reminderDAO->getReminders();
echo "<p>Hello " . $member->getFN() . "</p>";
echo "<p>These are the current reminders: </p>";
foreach ($reminders as $rem) {
echo "<b>Title:</b> " . $rem->getTitle() . "<br />";
echo "<b>Details:</b> " . $rem->getDetails() . "<br />";
echo "<b>Type: </b>" . $rem->getType() . "<br />";
echo "</p>";
}
echo $display; ?>
<a href="add_reminder_form.php">Add Reminder?</a>
</body>
</html>
<?php ob_flush(); ?>
edit_reminder_form.php class
<?php
ob_start();
require_once 'includes/session.php';
require_once 'includes/Member.php';
require_once 'includes/MemberDAO.php';
require_once 'includes/Reminder.php';
require_once 'includes/ReminderDAO.php';
require_once 'includes/session.php';
confirm_logged_in(); // needs to come before any html because it does a redirect
?>
<?php
$reminderDAO = new ReminderDAO();
$reminder = $reminderDAO->getReminder($_GET['id']);
?>
<html>
<head>
<title>Edit Reminder</title>
</head>
<body>
<table>
<tr>
<td>
<h2>Edit Reminder</h2>
<?php if (isset($_GET['errorMessage'])) echo "<p>".$_GET['errorMessage']."</p>"; ?>
<form action="edit_reminder.php" method="POST">
Title: <input type="text" name="title" value="<?php $reminder->getTitle(); ?>" /><br/>
Details: <input type="text" name="details" value="<?php $reminder->getDetails()?> " /><br/>
<select name="reminder_type" value="<?php $reminder->getType();?>">
<option value="Choose">Please choose a reminder type!</option>
<option value="Bill">Bill</option>
<option value="Shopping">Shopping</option>
<option value="Event">Event</option>
<option value="Birthday">Birthday</option>
<option value="Other">Other</option>
</select>
<br />
<input type="submit" name="reminder" value="Edit Reminder" />
</form>
<br />
<a href ="view_reminders.php"> Cancel </a>
</td>
</tr>
</table>
</body>
<?php
//5.Close connection
if(isset($connection)) {
mysql_close($connection);
}
?>
</html>
<?php ob_flush(); ?>
You could send the
IDof thereminderto the next page where you edit/delete a reminder.In
edit.phpyou get the reminder object using the ID (e.g.$_GET['id']), load the data from the database using ReminderDAO and create a form populated with the reminder values. In that form, you should also put the reminder id, so when he submit the form toSave changes, you can identify the reminder that was edited.After saving the changes, you can redirect him back to the list of reminders using
headerfunction.Similar, in
delete.phpyou can delete the reminder using the ID (e.g.$_GET['id']) and then redirect the user to the list of reminders.