Jump to content

Foreach Loop


MrDNA

Recommended Posts

Hello, I'm stuck at a piece of code, I'm hoping any one could help me further.Here is the idea and the problem: I want to send a mail with a personal touch. The receivers name etc. Thus I had this idea: Get all the ID's out of the database (mysql).With a foreach loop walk trough it. Foreach ID get the remainder of the needed info from the DB,Put them in place.And mail. Thus every one gets his own name in his inbox. I'm hopelessly stuck at the part where I ONLY get the ID's out of the DB and let the foreach run trough them.The sending works and I DO get the ID's out of the DB and to display them with a While loop. Script:

$QueryDB = mysql_query("SELECT * FROM Registration");$ResultDB = mysql_fetch_array($QueryDB);foreach($ResultDB as $PersID){	 //1) Get email.	 //2) Get name.	 //3) send mail.}

(Its a quick and smaller rewrite of what I had before. Before the rage quit that is.I'm not that great of a programmer, but trying to get around and learn more by doing things like this!) Any one got a hint, snippet of code or another way to do it?Thank you in advance.

Link to comment
Share on other sites

instead of fetching all the IDs and then sending a query for every ID, it would be better to just have 1 query,eg. if your name and email fields are Name and Email:

$query = 'SELECT Name,Email FROM Persoons_registratie';$result = mysql_query($query); while (($row = mysql_fetch_assoc($result)) !== false) {// name is $row['Name']// email is $row['Email']}

edit: that's assuming you want to send an email for every row in the Persoons_registratie table.

Link to comment
Share on other sites

Thank you for your answer. I changed the dutch words to english. Just to be clear, with that snipet, I mail just 1 persons name to his OWN email? I thought that with a while loop, I'd mail every ones names to every ones email.I had it working with a while, but with the idea a while would mess up everything, I pushed it aside. Thank you again.

Link to comment
Share on other sites

a loop works based on iterations. That means consecutive executions, one after the other for each index. In this case, the script would get all the names and emails for each record in the table, and then with mysql_fetch_assoc (within the context of the while loop), assign them each to ONE index in the $row array, for each iteration. So in each iteration, you have that current records name and email available, to which you can send out an email, or to do whatever you want. Basically, it will only do what you tell it do, but the current example is setup up to do one at a time.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...