Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- TEHTAVAT 5-6
- lueviesti.php:
- <?php
- $conn = new mysqli('localhost', 'ROOT', '', 'chat')
- or die ('Cannot connect to db');
- $result = $conn->query("select userID, userName from user");
- echo "<html>";
- echo "<body>";
- echo "<form action='lueviesti2.php' method='get'>";
- echo "<select name='userID'>";
- while ($row = $result->fetch_assoc()) {
- unset($id, $name);
- $id = $row['userID'];
- $name = $row['userName'];
- echo "<option value=" . $id . ">" . $name . "</option>";
- }
- echo "</select>";
- echo "<input type='submit'/>";
- echo "</form>";
- echo "</body>";
- echo "</html>";
- ?>
- --------------
- lueviesti2.php:
- <?php
- $USER = $_GET["userID"];
- echo "<table style='border: solid 1px black;'>";
- echo "<tr><th>Message</th><th>Sender</th><th>TO</th></tr>";
- class TableRows extends RecursiveIteratorIterator {
- function __construct($it) {
- parent::__construct($it, self::LEAVES_ONLY);
- }
- function current() {
- return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>";
- }
- function beginChildren() {
- echo "<tr>";
- }
- function endChildren() {
- echo "</tr>" . "\n";
- }
- }
- $servername = "localhost";
- $username = "ROOT";
- $password = "";
- $dbname = "chat";
- try {
- $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
- $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $stmt = $conn->prepare("SELECT messageData, messageSender, messageUser FROM message, user WHERE message.messageSender=user.userID AND userID='$USER'");
- $stmt->execute();
- // set the resulting array to associative
- $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
- foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
- echo $v;
- }
- }
- catch(PDOException $e) {
- echo "Error: " . $e->getMessage();
- }
- $conn = null;
- echo "</table>";
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement