get-comments.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. require_once("../../php/db_utils.php");
  3. try {
  4. // Connect to the database
  5. $database = db_connect();
  6. // Incoming data
  7. $canvasId = $_POST["canvas_id"];
  8. $ideaId = $_POST["idea_id"];
  9. // Declare query
  10. $query = 'SELECT comments.id, comments.comment, comments.collaborator, user.name, comments.date
  11. FROM comments
  12. INNER JOIN user ON comments.collaborator = user.username
  13. WHERE comments.canvas_id = "' . $canvasId . '"
  14. AND comments.idea_id = "' . $ideaId . '"
  15. ORDER BY comments.date;';
  16. // Run query
  17. $recordSet = db_query_return($database, $query);
  18. // Get comments by active user
  19. $comments = Array();
  20. // Declare index
  21. $index = 0;
  22. // For every comment
  23. while($record = $recordSet->fetch_assoc()) {
  24. $comments[$index]["id"] = $record["id"];
  25. $comments[$index]["comment"] = $record["comment"];
  26. $comments[$index]["collaborator"] = $record["collaborator"];
  27. $comments[$index]["name"] = $record["name"];
  28. $comments[$index]["date"] = $record["date"];
  29. // Increment index
  30. $index++;
  31. }
  32. mysqli_free_result($recordSet);
  33. echo(json_encode($comments));
  34. // Close the connection to the database
  35. db_close($database);
  36. }
  37. catch(Exception $e) {
  38. $errorMsg = $e->getMessage();
  39. print_r($errorMsg);
  40. }
  41. ?>