Name: Alres C. Arena Prenciss Ann P. Juni 1) Website: Index - PHP

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

NAME: ALRES C.

ARENA
PRENCISS ANN P. JUNI

1) WEBSITE
index.php

<?php
require_once 'app/init.php';

$itemsQuery = $db->prepare("SELECT id, name, done FROM items WHERE user =:user");

$itemsQuery->execute([ 'user' => $_SESSION['user_id']

]);

$items = $itemsQuery->rowCount() ? $itemsQuery : [];

foreach($items as $item) {
print_r($item);

?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>To do</title>

<link href="http://fonts.googleapis.com/css?family=Open+Sans"
rel="stylesheet">
<link href="http://fonts.googleapis.com/css?
family=Shadows+Into+Light+Two" rel="stylesheet">

<link rel="stylesheet" href="css/main.css">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

</head>
<body>
<div class="list">
<h1 class="header">To do.</h1>

<?php if(!empty($items)): ?>

<ul>
<?php foreach($items as $item): ?>
<li><span class="item<?php echo $item['done'] ? ' done' : '' ?
>"><?php echo $item['name']; ?></span>
<?php if($item['done']): ?>
<a href="mark.php?as=done&item=<?php echo
$item['id']; ?>" class="done-button">Mark as done</a>
<?php endif; ?>
</li>
<?php endforeach; ?>

</ul>

<?php else: ?>


<p>You haven't added any items yet.</p>
<?php endif; ?>

<form class="item-add" action="add.php" method="post">


<input type="text" name="name" placeholder="Type a new
item here." class="input" autocomplete="off" required />
<input type="submit" value="Add" class="submit" />
</form>
</div>

</body>
</html>

mark.php
<?php

require_once 'app/init.php'

if(isset($_GET['as'], $_GET['item'])) {
$as = $_GET['as'];
$item = $_GET['item'];

switch($as) {
case 'done':
$doneQuery = $db->prepare("
UPDATE items
SET done = 1
WHERE id = :item
AND user = :user
");

$doneQuery->execute([
'item' => $item,
'user' => $_SESSION['user_id']
]);

break;

}
}

header('Location: index.php');
add.php
<?php

require_once 'app/init.php';

if(isset($_POST['name'])) {
$name = trim($_POST['name']);

if(!empty($name)) {
$addedQuery = $db->prepare("
INSERT INTO items (name, user, done, created)
VALUES (:name, :user, 0, NOW())
");

$addedQuery->execute([
'name' => $name,
'user' => $_SESSION['user_id']
]);
}
}

header('Location: index.php');

app/init.php
<?php

session_start();
$_SESSION['user_id'] = 1;

$db = new PDO('mysql:dbname=todo;host=localhost', 'root', 'root');

//Handle this some other way

if (!isset($_SESSION['user_id'])) {
die('You are not signed in.');
}

css/main.css
body {
background-color:#eceff0;

body, input {
font: 1em "Open Sans", sans-serif;
color: #363639;

a{
text-decoration: none;
border-bottom: 1px dashed #363639;
}

/* List */
.list {
background-color: #fff;
margin: 20px auto;
width: 100%;
max-width:500px;
padding: 20px;
border-radius: 2px;
box-shadow: 3px 3px 0 rgba(0, 0, 0, .1);
box-sizing: border-box;
}

.list .header {
font-family: "Shadows Into Light Two", cursive;
margin: 0 0 10px 0;

/* Items */
.items {
margin: 0;
padding: 0;
list-style-type: none;

.items li:hover .done-button {


opacity: 1;
}

.items .item.done {
text-decoration: line-through;
}

.items li,
.item-add .input {
border: 0;
border-botom: 1px dashed #ccc;
padding: 15px 0;
}

.input {
width: 100%;
}

.input:focus {
outline: none;
}

/* Done button */
.done-button {
display: inline-block;
font-size: 0.8em;
background-color: #d9dfe1;
color: #363639;
padding: 3px 6px;
border: 0;
opacity: 0.4;

/* Submit button */
.submit {
background-color: #fff;
padding: 5px 10px;
border: 1px solid #ddd;
width: 100%
margin-top: 10px;
box-shadow: 3px 3px 0 #ddd;
}

QUESTIONS :

1. WHAT IS THE PURPOSE OF ISSET FUNCTION?


2. IS IT POSSIBLE TO LINK EXTERNAL FONTS?
3. IS INLINE PHP CODE VALID FOR EXECUTION?

You might also like