Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d336315

Browse files
Added function to camelCase action, Implemented UPDATE func
1 parent f145377 commit d336315

File tree

7 files changed

+81
-25
lines changed

7 files changed

+81
-25
lines changed

‎config/utilities.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* Get URI path.
4-
* Returns a string.
4+
* @return string $uri Sanitized URI
55
*/
66
function getUri() : string
77
{

‎src/app/Router.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function setParams(string $uri) : void
9494
public function redirectTo() : void
9595
{
9696
$controller = $this->getNamespace() . $this->params['controller'];
97-
$action = $this->params['action'];
97+
$action = $this->capitalizeAction($this->params['action']);
9898

9999
if (class_exists($controller))
100100
{
@@ -134,4 +134,22 @@ private function getNamespace() : string
134134

135135
return $namespace;
136136
}
137+
138+
/**
139+
* Camel case the 'action' string
140+
* If in the URL the action is something like '{controller}/add-new-task'...
141+
* ... change it to 'addNewTask' in order to make it match with a class method
142+
* @param string $action Get the 'action' from the URL
143+
* @return string Camel Cased 'action' string
144+
*/
145+
private function capitalizeAction(string $action) : string
146+
{
147+
$action = explode('-', $action);
148+
149+
for($i=1; $i < count($action); $i++){
150+
$action[$i] = ucwords($action[$i]);
151+
}
152+
153+
return implode($action);
154+
}
137155
}

‎src/controllers/Tasks.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

‎src/controllers/Test.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
namespace Controllers;
3+
4+
use \Models\Task;
5+
6+
class Test
7+
{
8+
public function __construct()
9+
{
10+
$this->taskModel = new Task;
11+
}
12+
13+
public function tasks()
14+
{
15+
$tasks = $this->taskModel->selectAll();
16+
17+
view('TestDb/tasks', $tasks);
18+
}
19+
20+
public function addTask()
21+
{
22+
echo __METHOD__;
23+
}
24+
25+
public function markRead($data)
26+
{
27+
if ($this->taskModel->changeTaskStatus($data['id']))
28+
{
29+
$this->tasks();
30+
}
31+
}
32+
}

‎src/models/Task.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,14 @@ public function selectAll() : array
1717
$this->db->query("SELECT * FROM task");
1818
return $this->db->resultSet();
1919
}
20+
21+
public function changeTaskStatus($id) : bool
22+
{
23+
$this->db->query("UPDATE task SET completed = 1 WHERE id = :id");
24+
$this->db->bind(':id', $id);
25+
if ($this->db->execute())
26+
return true;
27+
return false;
28+
}
2029

2130
}

‎src/views/TestDb/tasks.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
<?php require_once APPROOT . '/src/views/include/header.php'; ?>
2-
<h1>Tasks loaded from database:</h1>
2+
<h1>Test Database</h1>
3+
<h2>This page tests the connection to a database, using a typical "to-do list app" to perform CRUD operations</h2>
4+
<hr>
5+
<h3>[CREATE] Add new task</h3>
6+
<form action="<?= URLROOT; ?>/test/add-task" method="post">
7+
<input type="text" name="new_task" placeholder="Description...">
8+
<input type="submit" value="Add task">
9+
</form>
10+
<h3>[READ] Get all tasks:</h3>
311
<ul>
412
<? foreach ($data as $task) : ?>
513
<li>
6-
<?= $task->completed ? "<strike> $task->description </strike>" : $task->description; ?>
14+
<? if ($task->completed) : ?>
15+
<strike> <?= $task->description; ?> </strike> &emsp;
16+
<? else : ?>
17+
<?= $task->description; ?> &emsp;
18+
<a href="<?= URLROOT; ?>/test/<?= $task->id; ?>/mark-read">Mark as done</a> |
19+
<? endif; ?>
20+
21+
<a href="<?= URLROOT; ?>/test/<?= $task->id; ?>/delete">Delete</a>
722
</li>
823
<? endforeach; ?>
924
</ul>

‎src/views/include/header.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
<head>
44
<meta charset="UTF-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6-
<meta http-equiv="X-UA-Compatible" content="ie=edge">
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7+
<link rel="stylesheet" href="<?= URLROOT;?>/public/css/style.css">
78
<title> <?= SITENAME; ?> </title>
89
</head>
910
<body>
1011
<nav>
1112
<ul>
1213
<li><a href="<?= URLROOT; ?>"> Home </a></li>
1314
<li><a href="<?= URLROOT; ?>/about"> About </a></li>
14-
<li><a href="<?= URLROOT; ?>/tasks/list">Test database</a></li>
15+
<li><a href="<?= URLROOT; ?>/test/tasks">Test database</a></li>
1516
</ul>
1617
</nav>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /