Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random Records Problem

Status
Not open for further replies.

jgd12345

Technical User
Joined
Apr 22, 2003
Messages
124
Location
GB
Hi, I was wondering how togo about the following problem. Alright I have a table called "members" and it has the following fields: id, username, password, email. What I need todo is do a script which randomly selects 3 members from the table for each member and emails them saying their 3 members. Now this is the hard bit. I also need it to say the email address of the users who had their email address sent. Get me.

Hope you can help. Thanx
 
Your question is unclear. What do you mean by
I also need it to say the email address of the users who had their email address sent?

 
ah sorry, here's an example. The following loops through every member in the members table and then selects 3 random members for each member and then stores their email addresses in the variable $emailaddresses. and emails the randomly selected member and the original member telling them.

The bit I'm having trouble with is that the member who has been randomly selected could be sent lots of emails if he's really unlucky. So instead of sending him a seperate email each time I need it to email him once saying the $member variable.:

$query = mysql_query("SELECT * FROM members);

while($member = mysql_fetch_array($query)) {
$emailaddresses = "";

$query2 = mysql_query("SELECT * FROM members WHERE name=RAND() LIMIT 0, 3");

while($memberemail = mysql_fetch_array($query2)) {
$emailaddresses .= $memberemail[email];

mail($memberemail[email], "You have been selected", $member[email], $adminemail);
}

mail($member[email], "Main Email", $emailaddresses, $adminemail);
}
 
You're going to have to write code that keeps track of to whom you've sent email.

I don't know how many records you have in your table, but you might try adding a new column to keep track of whether they've been emailed in this run of the program.

Add a column "email_sent" to your table. At the beginning of your script, set that column to 0 for all records. Then when selecting members to email, use the clause "WHERE email_sent = 0". As you send each member email, set that member's "email_sent" column to 1. Keep in mind, however, that this solution is not multiuser friendly.

Another solution that is multiuser friendly is to keep track of all emails you've already sent to in arrays in your script and only send emails to people who haven't received one yet. If the number of members in this table is large, however, you can run into resource problems with this solution.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Multi-user resource friendly solution...

create a table on the fly...
Code:
Table Sentemails-<Random #>
  member_id
  emails_sent

Then follow through with Sleipnir's first solution.

-Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top