I'm working on a really simple email notification system for newly added comments. It works like this:
- loop through project users
- for each user, loop through all comments written in the last hour
- if there are any comments that are not written by the user, push them to an array
- if comment array is not empty, send an email to the user
Here's some of the code:
<?php
$users_query = mysql_query("SELECT * FROM users");
$comments_query = mysql_query("SELECT * FROM comments c, users u WHERE c.date > DATE_SUB(NOW(), INTERVAL 1 HOUR) and c.user_id = u.id");
while ($user = mysql_fetch_assoc($users_query)) {
$commentsArr = array();
while ($comment = mysql_fetch_assoc($comments_query)) {
if ($comment['email'] != $user['email']) {
array_push($commentsArr, $comment['comment']);
}
}
// send $commentsArr by email to $user['email']
}
?>
The problem is that the same mysql_fetch_assoc
cannot be used multiple times on a single page. I tried reseting the pointer using mysql_data_seek($comments_query, 0);
but that just threw the following error:
Warning: mysql_data_seek() [function.mysql-data-seek]: Offset 0 is invalid for MySQL result index 4 (or the query data is unbuffered)
Are there any other ways of handling this? All other people with similar questions were advised to join their tables instead of splitting them up. I don't think I can do that since I have to go through all users first, so if I join them, I'll have more rows than users.
Anyway, is there a way to loop through the comments while inside a loop?