Aramil
Aramil

Reputation: 421

Mysql PHP recomendation

Im buidling a reminder based on a mysql table data, my table looks like

Task Desc  Name Email
1    Task1 John [email protected]
2    Task2 Eve  [email protected]
3    task3 John [email protected]
4    Task4 Mark [email protected]
5    task5 John [email protected]

in my php

$query = "SELECT desc, name, email FROM table";
$result_query = mysql_query($query) or die("Error in query: $query " . mysql_error());
$num_results_query = mysql_num_rows($result_query);

if ($num_results_trad > 0) {
ob_start();
HERE I WRITE MY EMAIL

$Buffer = ob_get_contents();
    // get the output
    ob_end_clean();

After that i send the email. Now my problem is that i need to send a email to each user email once with all the Task assigned to him. If i do a loop i would be sending one email for each task or row, what im trying to do is to send one email, by example to John with Task 1 3 and 5, then to the second email found and so on with each user. Some one can point me in some direction?

Upvotes: 0

Views: 62

Answers (2)

cmorrissey
cmorrissey

Reputation: 8583

You could simplify this by using GROUP_CONCAT

ie

SELECT GROUP_CONCAT(DISTINCT desc SEPARATOR ', '), name, email FROM table GROUP BY email

Upvotes: 0

PressingOnAlways
PressingOnAlways

Reputation: 12356

You have to loop through this twice, one to aggregate all the tasks and then once to send out the email. In non-working php pseudocode:

$taskForUser = array();

// your query
$query = "SELECT desc, name, email FROM table";
$result_query = mysql_query($query) or die("Error in query: $query " . mysql_error());
$num_results_query = mysql_num_rows($result_query);


foreach ($query as $result) {
    $taskForUser[$result['Email']][] = $result;
}

foreach ($taskForUser as $userTasks) {
    // email $userTasks at once.
}

Upvotes: 3

Related Questions