Skip to content

Instantly share code, notes, and snippets.

@vinzenz
Last active August 27, 2023 03:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save vinzenz/2872dfb74573fcbf56663a91f7182406 to your computer and use it in GitHub Desktop.
Save vinzenz/2872dfb74573fcbf56663a91f7182406 to your computer and use it in GitHub Desktop.
Extremely Simple Sample TODO List App in PHP
<!--
Copyright 2017 Vinzenz Feenstra, Red Hat, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<?php
define('DB_USER', 'todo-app');
define('DB_PASS', 'todo-app-pass');
define('DB_NAME', 'todo');
$db = new PDO('mysql:dbname='.DB_NAME, DB_USER, DB_PASS);
$ITEMS = array();
function get(&$var, $default=null) {
return isset($var) ? $var : $default;
}
switch(get($_GET['action'])) {
case 'new':
$title = get($_GET['title']);
$stmt = $db->prepare('INSERT INTO todo VALUES(NULL, ?, FALSE)');
if(!$stmt->execute(array($title))) {
die(print_r($stmt->errorInfo(), true));
}
header("Location: ".$_SERVER['DOCUMENT_URI']);
die();
case 'toggle':
$id = get($_GET['id']);
if(is_numeric($id)) {
$stmt = $db->prepare('UPDATE todo SET done = !done WHERE id = ?');
if(!$stmt->execute(array($id))) {
die(print_r($stmt->errorInfo(), true));
}
}
header("Location: ".$_SERVER['DOCUMENT_URI']);
die();
default:
break;
}
$stmt = $db->prepare('SELECT * from todo');
if ($stmt->execute()) {
$ITEMS = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
?>
<html>
<head>
<title> Sample TODO App </title>
<style>
div, body, html {
margin: 0px;
background-color: #eee;
}
h1 {
padding: 30px;
}
div {
margin-left: 30px;
margin-top: 15px;
}
div input {
height: 28px;
font-size: 1.2em;
}
div button {
height: 28px;
font-size: 1.2em;
}
div ul {
margin: 0px;
padding: 0px;
border: 1px solid #333;
max-width: 500px;
background-color: #ffe;
-webkit-box-shadow: 10px 10px 18px 1px rgba(0,0,0,0.18);
-moz-box-shadow: 10px 10px 18px 1px rgba(0,0,0,0.18);
box-shadow: 10px 10px 18px 1px rgba(0,0,0,0.18);
border-radius: 5px 5px;
}
li a {
font-size: 1.25em;
display: block;
}
li:hover {
background-color: #fff;
-webkit-box-shadow: 10px 10px 18px 1px rgba(0,0,0,0.18);
-moz-box-shadow: 10px 10px 18px 1px rgba(0,0,0,0.18);
box-shadow: 10px 10px 18px 1px rgba(0,0,0,0.18);
}
li {
display: block;
}
li.checked span {
text-decoration: line-through;
}
li.checked i:before {
color:green;
content: '\2713';
padding:0 6px 0 0;
}
li.unchecked i:before {
content: '\2713';
color:transparent;
padding:0 6px 0 0;
}
li a {
text-decoration: none;
color:inherit;
}
ul li{list-style-type:none;font-size:1em;}
</style>
</head>
<body>
<h1>Sample TODO</h1>
<div id="new-task">
<input id="task-title" name="title" type="text" placeholder="Task Title"><button id='new-task-button'>Add</button>
</div>
<div id="task-list">
<ul>
<?php foreach($ITEMS as $ITEM): ?>
<li class=<?php if($ITEM['done']): ?>"checked"<?php else: ?>"unchecked"<?php endif;?>>
<a href="?action=toggle&id=<?=$ITEM['id']?>">
<i></i><span>
<?=htmlspecialchars($ITEM['title'])?></span>
</a>
</li>
<?php endforeach; ?>
</ul>
</div>
<script>
document.getElementById('new-task-button').onclick = function(){
window.location.href = '?action=new&title=' + encodeURI(document.getElementById('task-title').value);
};
</script>
</body>
</html>
CREATE DATABASE todo;
CREATE USER 'todo-app'@'localhost' IDENTIFIED BY 'todo-app-pass';
GRANT ALL PRIVILEGES ON todo.* TO 'todo-app'@'localhost' ;
CONNECT todo;
CREATE TABLE `todo` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`title` varchar(2048) NOT NULL,
`done` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
);
INSERT INTO todo VALUES(NULL, 'Sample TODO entry #1', FALSE);
INSERT INTO todo VALUES(NULL, 'Sample TODO entry #2', TRUE);
INSERT INTO todo VALUES(NULL, 'Sample TODO entry #3', FALSE);
INSERT INTO todo VALUES(NULL, 'Too many things todo', FALSE);
@MuhaddiMu
Copy link

Notice: Undefined index: DOCUMENT_URI in G:\XAMPP\htdocs\CODE\My-Blog\Admin\todo.php on line 35

I don't know why I am getting this error. Can you please help me resolve this?

@stoneC0der
Copy link

Hi! If your access the app using "localhost/simple/todo/index.php" you need to change/comment these lines
header("Location: ".$_SERVER['DOCUMENT_URI']);
and use something like this
header("Location: index.php");

@kevenli
Copy link

kevenli commented Mar 6, 2019

$_SERVER['DOCUMENT_URI'] caused an error in newer PHP.
So I modify it into

header("Location: ".$_SERVER['SCRIPT_NAME']);

it works.

@antham1616
Copy link

Hi , im getting this error, i copied the code directly from above, i have noticed there isnt a host specified
Fatal error: Uncaught PDOException: could not find driver in /homepages

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment