<?php include("includes/db.php"); include("includes/auth.php");

$users=$conn->query("SELECT DISTINCT user_id FROM doctor_messages");
?>
<h2>Doctor Chat</h2>

<?php while($u=$users->fetch_assoc()): 
$uid=$u['user_id'];
$msgs=$conn->query("SELECT * FROM doctor_messages WHERE user_id=$uid");
?>
<h3>User <?= $uid ?></h3>
<?php while($m=$msgs->fetch_assoc()): ?>
<p><b><?= $m['sender'] ?>:</b> <?= $m['message'] ?></p>
<?php endwhile; ?>

<form method="post">
<input type="hidden" name="user_id" value="<?= $uid ?>">
<input name="reply">
<button>Send</button>
</form>
<hr>
<?php endwhile; ?>
