Advertisement
oshkoshbagoshh

To Do list app.php

Jul 18th, 2019
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.34 KB | None | 0 0
  1. <?php
  2. require 'vendor/autoload.php';
  3. # This logic handles connecting to the database, where we store our todo status
  4. $pdo = new \PDO("sqlite:" . "db/sqlite.db");
  5.  
  6. # This PHP logic handles user actions
  7. # New TODO
  8. if (isset($_POST['submit']))
  9. {
  10.   $description = $_POST['description'];
  11.   $sth = $pdo->prepare("INSERT INTO todos (description) VALUES (:description)");
  12.   $sth->bindValue(':description', $description, PDO::PARAM_STR);
  13.   $sth->execute();
  14. }
  15. # Delete TODO
  16. elseif (isset($_POST['delete']))
  17. {
  18.   $id = $_POST['id'];
  19.   $sth = $pdo->prepare("delete from todos where id = :id");
  20.   $sth->bindValue(':id', $id, PDO::PARAM_INT);
  21.   $sth->execute();
  22. }
  23. # Update completion status
  24. elseif (isset($_POST['complete']))
  25. {
  26.     $id = $_POST['id'];
  27.     $sth = $pdo->prepare("UPDATE todos SET complete = 1 where id = :id");
  28.     $sth->bindValue(':id', $id, PDO::PARAM_INT);
  29.     $sth->execute();
  30. }
  31. # Here is the HTML:
  32. ?>
  33. <!DOCTYPE HTML>
  34. <html lang="en">
  35. <head>
  36.   <title>Todo List</title>
  37. </head>
  38.  
  39. <body class="container">
  40.   <h1>Todo List</h1>
  41.   <form method="post" action="">
  42.     <input type="text" name="description" value="">
  43.     <input type="submit" name="submit" value="Add">
  44.   </form>
  45.   <h2>Current Todos</h2>
  46.   <table class="table table-striped">
  47.     <thead><th>Task</th><th></th><th></th></thead>
  48.     <tbody>
  49.  
  50. <?php
  51.   # Entering PHP mode,
  52. $sth = $pdo->prepare("SELECT * FROM todos ORDER BY id DESC");
  53. $sth->execute();
  54.  
  55. foreach ($sth as $row) {
  56.   # Exiting PHP Mode
  57.    ?>
  58. <tr>
  59.   <td>
  60.       <!-- This is PHP shorthand for inserting dynamic text into HTML -->
  61.       <?=htmlspecialchars($row['description'])?></td>
  62.   <td>
  63.     <?php # Here we are mixing HTML and PHP to get the desired document
  64. if (!$row['complete']) {
  65.         ?>
  66.     <form method="POST">
  67.       <button type="submit" name="complete">Complete</button>
  68.       <input type="hidden" name="id" value="<?=$row['id']?>">
  69.       <input type="hidden" name="complete" value="true">
  70.     </form>
  71.     <?php
  72. } else {
  73.         ?>
  74.     Task Complete!
  75.     <?php
  76. }
  77.     ?>
  78.   </td>
  79.   <td>
  80.     <form method="POST">
  81.       <button type="submit" name="delete">Delete</button>
  82.       <input type="hidden" name="id" value="<?=$row['id']?>">
  83.       <input type="hidden" name="delete" value="true">
  84.     </form>
  85.   </td>
  86. </tr>
  87. <?php
  88. }
  89. ?>
  90.     </tbody>
  91.   </table>
  92. </body>
  93. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement