Hello, Viv here!
I’m having an issue with some PHP code I made. Basically, you can add your classes and once there’s at least one class you can add your assignments. It should prioritize all your assignments based on…
- Percent in class (lower = higher priority)
- Due date minus current date (lower = higher priority)
- Point value (higher = higher priority)
The issue on the table…I can’t seem to create any assignments–it just resets the form! I don’t get what I did wrong, can anyone help?
<?php
$cookies = $_COOKIE;
$classes = array();
foreach ($cookies as $key => $value) {
if (substr($key, 0, 2) === "c-") {
$classes[$key] = $value;
}
}
echo '<h3>Add a Class:</h3>';
echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">
Class Name: <input type="text" name="className"><br>
Percentage: <input type="number" name="classPercentage" min="0" max="100"><br>
<input type="submit" value="Add Class">
</form><br>';
if (isset($_POST['className']) && isset($_POST['classPercentage'])) {
setcookie("c-" . urlencode(strtoupper($_POST['className'])), $_POST['classPercentage'], time() + 60 * 60 * 24 * 30);
header("Location: " . $_SERVER['PHP_SELF']);
}
if (!empty($classes)) {
$assignments = array();
foreach ($cookies as $key => $value) {
if (substr($key, 0, 2) === "a-") {
$assignments[$key] = json_decode($value, true);
}
}
usort($assignments, function($a, $b) use ($classes) {
$diff = strtotime($a["dueDate"]) - strtotime($b["dueDate"]);
if ($diff !== 0) {
return $diff;
}
$diff = $b["points"] - $a["points"];
if ($diff !== 0) {
return $diff;
}
return $classes[$a["className"]] - $classes[$b["className"]];
});
echo '<h3>Add an Assignment:</h3>';
echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">
Assignment Name: <input type="text" name="assignmentName"><br>
Points: <input type="number" name="assignmentPoints"><br>
Due Date: <input type="date" name="assignmentDueDate"><br>
Class: <select name="assignmentClass">';
foreach ($classes as $key => $value) {
echo '<option value="' . $key . '">' . substr($key, 2) . ' (' . $value . '%)</option>';
}
echo '</select><br>
<input type="submit" value="Add Assignment">
</form><br>';
if (isset($_POST['assignment-name']) && isset($_POST['assignment-points']) && isset($_POST['assignment-due-date']) && isset($_POST['class-select'])) {
$assignmentName = $_POST['assignment-name'];
$assignmentPoints = $_POST['assignment-points'];
$assignmentDueDate = $_POST['assignment-due-date'];
$assignmentClass = $_POST['class-select'];
setcookie("a-$assignmentName", "$assignmentPoints,$assignmentDueDate,$assignmentClass", time() + (86400 * 30), "/");
}
$assignments = array();
foreach ($_COOKIE as $key => $value) {
if (substr($key, 0, 2) == "a-") {
$assignments[$key] = explode(",", $value);
}
}
usort($assignments, function($a, $b) use ($classes) {
$aDueDate = strtotime($a[1]);
$bDueDate = strtotime($b[1]);
$aPoints = $a[0];
$bPoints = $b[0];
$aClassPercent = $classes[$a[2]];
$bClassPercent = $classes[$b[2]];
if ($aDueDate - time() == $bDueDate - time()) {
if ($aClassPercent == $bClassPercent) {
return $aPoints > $bPoints ? -1 : 1;
}
return $aClassPercent < $bClassPercent ? -1 : 1;
}
return ($aDueDate - time()) < ($bDueDate - time()) ? -1 : 1;
});
echo "<table>";
echo "<tr><th>Assignment</th><th>Due Date</th><th>Points</th><th>Class</th><th>Action</th></tr>";
foreach ($assignments as $key => $assignment) {
$assignmentName = substr($key, 2);
$assignmentDueDate = $assignment[1];
$assignmentPoints = $assignment[0];
$assignmentClass = $assignment[2];
echo "<tr>";
echo "<td>$assignmentName</td>";
echo "<td>$assignmentDueDate</td>";
echo "<td>$assignmentPoints</td>";
echo "<td>$assignmentClass</td>";
echo "<td><button onclick='deleteAssignment(\"$key\")'>I'm Done!</button></td>";
echo "</tr>";
}
echo "</table>";
}
?>
All help is appreciated!