Advertisement
Guest User

Untitled

a guest
Mar 17th, 2017
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. TEHTAVAT 5-6
  2.  
  3. lueviesti.php:
  4.  
  5. <?php
  6.  
  7. $conn = new mysqli('localhost', 'ROOT', '', 'chat')
  8. or die ('Cannot connect to db');
  9.  
  10. $result = $conn->query("select userID, userName from user");
  11.  
  12. echo "<html>";
  13. echo "<body>";
  14. echo "<form action='lueviesti2.php' method='get'>";
  15. echo "<select name='userID'>";
  16.  
  17. while ($row = $result->fetch_assoc()) {
  18.  
  19. unset($id, $name);
  20. $id = $row['userID'];
  21. $name = $row['userName'];
  22. echo "<option value=" . $id . ">" . $name . "</option>";
  23. }
  24.  
  25. echo "</select>";
  26. echo "<input type='submit'/>";
  27. echo "</form>";
  28.  
  29. echo "</body>";
  30. echo "</html>";
  31.  
  32. ?>
  33. --------------
  34.  
  35. lueviesti2.php:
  36.  
  37. <?php
  38. $USER = $_GET["userID"];
  39.  
  40. echo "<table style='border: solid 1px black;'>";
  41. echo "<tr><th>Message</th><th>Sender</th><th>TO</th></tr>";
  42.  
  43. class TableRows extends RecursiveIteratorIterator {
  44. function __construct($it) {
  45. parent::__construct($it, self::LEAVES_ONLY);
  46. }
  47.  
  48. function current() {
  49. return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>";
  50. }
  51.  
  52. function beginChildren() {
  53. echo "<tr>";
  54. }
  55.  
  56. function endChildren() {
  57. echo "</tr>" . "\n";
  58. }
  59. }
  60.  
  61. $servername = "localhost";
  62. $username = "ROOT";
  63. $password = "";
  64. $dbname = "chat";
  65.  
  66. try {
  67. $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  68. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  69. $stmt = $conn->prepare("SELECT messageData, messageSender, messageUser FROM message, user WHERE message.messageSender=user.userID AND userID='$USER'");
  70. $stmt->execute();
  71.  
  72. // set the resulting array to associative
  73. $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
  74. foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
  75. echo $v;
  76. }
  77. }
  78. catch(PDOException $e) {
  79. echo "Error: " . $e->getMessage();
  80. }
  81. $conn = null;
  82. echo "</table>";
  83. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement