You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.php 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. session_start();
  3. // Database class for DB operations. These work, they are NOT been tampered with.
  4. // Do not change this class.
  5. class MyDB extends SQLite3
  6. {
  7. function __construct()
  8. {
  9. $this->open('./dbase/comments.db');
  10. }
  11. function get_comments()
  12. {
  13. $result = $this->query('SELECT * FROM comments where published = 1 order by hierarchy');
  14. $comments = array();
  15. $i = 0;
  16. while ($res = $result->fetchArray(SQLITE3_ASSOC)) {
  17. $comments[$i]['id'] = $res['id'];
  18. $comments[$i]['parentid'] = $res['parent_id'];
  19. $comments[$i]['author'] = $res['author'];
  20. $comments[$i]['email'] = $res['email'];
  21. $comments[$i]['comment'] = urldecode($res['comment']);
  22. $comments[$i]['offset'] = sizeof(explode('-', $res['hierarchy'])) - 1;
  23. $i += 1;
  24. }
  25. return $comments;
  26. }
  27. }
  28. // database object
  29. $db = new MyDB();
  30. // Vraag alle comments op.
  31. $db->get_comments();
  32. // Set some variables for easier use.
  33. $name = $_COOKIE['commentname'];
  34. $email = $_COOKIE['commentemail'];
  35. $comment = $_SESSION['comment'];
  36. $_SESSION['parent_id'] = $_GET['commentid'];
  37. // reset the name
  38. $name = null;
  39. // We might have a stale error message. Clear it
  40. if (true) {
  41. unset($_SESSION['errormsg']);
  42. }
  43. ?>
  44. <html>
  45. <head>
  46. <!-- Some bootstrap, just for fun -->
  47. <link rel="stylesheet" href="./css/bootstrap.min.css">
  48. <link rel="stylesheet" href="./style/style.css">
  49. <script src="./js/bootstrap.min.js"></script>
  50. </head>
  51. <body>
  52. <div id="wrapwrap">
  53. <div class="container">
  54. <div id="header" class="row">
  55. <!-- Vergeet je naam niet! -->
  56. <h1>Examen PHP: &lt; Vul hier je naam in &gt; </h1>
  57. </div>
  58. <!-- Het artikel -->
  59. <div id="main" class="row">
  60. <div class="col-md-8 offset-md-2">
  61. <h2>Lorem ipsum dolor sit amet.</h2>
  62. <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Laboriosam optio consequatur impedit, voluptatem exercitationem, nihil sit adipisci sunt maxime delectus sapiente reiciendis? Repudiandae tempora ratione quia laborum, sunt iusto suscipit.</p>
  63. <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Corrupti temporibus eos, perspiciatis, voluptatibus praesentium minus fugiat corporis omnis explicabo, cupiditate consequuntur! Ratione voluptates nulla rerum qui atque exercitationem saepe provident molestiae sint! Ad voluptate reprehenderit nostrum laborum quam, rem perspiciatis.</p>
  64. </div>
  65. </div>
  66. <!-- De comments-sectie, hier begint de actie! -->
  67. <div id="comments" class="row">
  68. <?php
  69. if ($_SESSION['errormsg'] != '') {
  70. ?>
  71. <!-- if error, show the error message -->
  72. <div class="alert alert-danger" role="alert">
  73. <?php $_SESSION['errormsg']; ?>
  74. </div>
  75. <?php
  76. }
  77. ?>
  78. <div class="col-md-8 offset-md-2">
  79. <hr/>
  80. <!-- If we want to add a comment, call ourselves with the right commentid
  81. commentid 0 = a comment to the article itself-->
  82. <a href="index.php?commentid=0" class="link">Leave a comment</a>
  83. <?php if (isset($_GET['commentid']) && $_GET['commentid'] == '0') { ?>
  84. <form action="savecomment.php" method="POST">
  85. <div class="form-group">
  86. <label for="name">Author</label>
  87. <input id="name" name="name" type="text" class="form-control" value="<?php echo $name; ?>"></input>
  88. <label for="email">Email</label>
  89. <input id="email" name="email" type="email" class="form-control" value="<?php echo $email; ?>"></input>
  90. <label for="comment">Comment</label>
  91. <textarea id="comment" name="comment" type="textarea" class="form-control" value="<?php echo $comment; ?>"></textarea>
  92. </div>
  93. <button type="submit" class="btn btn-success mt-16">Submit</button>
  94. </form>
  95. <?php
  96. } ?>
  97. <h4>Comments</h4>
  98. <?php
  99. // Voor elke comment, toon deze
  100. foreach ($comments as $row) {
  101. ?>
  102. <!-- Offset the card with each sublevel of cmments, use $row['offset'] -->
  103. <div class="card offset-md-" >
  104. <div class="card-body">
  105. <h5 class="card-title">Comment <?php echo $row['id']; ?></h5>
  106. <blockquote class="blockquote-footer mb-0"><?php echo $row['author']; ?></blockquote>
  107. <p class="card-text"><?php echo $row['comment']; ?></p>
  108. </div>
  109. <div class="card-footer text-muted">
  110. <!-- If we want to add a comment, call ourselves with the right commentid of the comment
  111. we are commenting -->
  112. <a href="index.php?commentid=$row['id']" class="card-link">Reply to this comment</a>
  113. <?php
  114. // als commentid == deze rij, toon het formulier
  115. if (isset($_POST['commentid']) && $_POST['parent_id'] == $row['id']) ?>
  116. <!-- het formulier, vul waarden in waar mogelijk -->
  117. <form action="savecomment.php" method="POST">
  118. <div class="form-group">
  119. <label for="name">Author</label>
  120. <input id="name" name="name" type="text" class="form-control" value="<?php echo $name; ?>"></input>
  121. <label for="email">Email</label>
  122. <input id="email" name="email" type="email" class="form-control" value="<?php echo $email; ?>"></input>
  123. <label for="comment">Comment</label>
  124. <textarea id="comment" name="comment" type="textarea" class="form-control" value="<?php echo $comment; ?>"></textarea>
  125. </div>
  126. <button type="submit" class="btn btn-success mt-16 ">Submit</button>
  127. </form>
  128. </div>
  129. </div>
  130. <?php
  131. } ?>
  132. </div>
  133. </div>
  134. <div id=" footer " class=" row "></div>
  135. </div>
  136. </div>
  137. </body>
  138. </html>