php - Codeigniter logic of PC/tl/sc/pm email -
i have array
array ( [0] => array ( [pm] => 14 [ld] => 578 [tl] => 56 [sc] => 67 [pc] => ) )
the above ids of pc/pm/tl/ld. use them in database query email ids.
i want use following logic, email template should work pc in “to” , rest cc’ed.
- if no pc assigned, tl in email rest in cc
- if no tl, ld in email rest in cc
- if no ld, pm in email rest in cc
- if no pm, sc in email rest in cc
also want add email ids array. if start with:
array ( [pc] => [tl] => 1109 [ld] => 838 [pm] => 715,824,694 [sc] => 727 )
i perform sql ($user_sql):
select id,email new_login id in (1109,838,715,824,694,727) , status = 1
to output:
array ( [694] => a@exateam.com [715] => b@gmail.com [727] => c@gmail.com [824] => d@gmail.com [838] => e@gmail.com [1109] => m@gmail.com )
this code below will:
- order given ids according priority of two-letter codes;
- create 1 list of ids splitting multi-id strings separate array elements;
- query database receive corresponding emails addresses
- store these email addresses in key/value pairs (id/email) in right array variable ($to or $cc)
you did not specify database engine use. code assumes use mysqli , have conection object named $con. database part of code untested. should @ least give idea how proceed.
comments in code should clarify happening:
// example data: $data = array ( array ("pm" => "715,824,694", "ld" => "838", "tl" => "1109", "sc" => "727", "pc" => null) ); // define priority order of different 2 letter codes: $order = array_flip(array("pc", "tl", "ld", "pm", "sc")); // iterate on data foreach ($data $row) { // order $row elements priority , filter out empty values: $row = array_filter(array_merge($order, $row)); // if nothing left over, skip row if (count($row) == 0) continue; // count number of ids in "to" addressee: $to_count = count(explode(',', array_values($row)[0])); // create 1 list of ids , turn comma-separated items // separate array elements: $row = explode(',', implode(',', $row)); // initialise arrays receive email addresses / cc $to = array(); $cc = array(); // retrieve email addresses database. // need valid mysqli connection object: $con // prepare sql statement avoid sql injection, , produce // right number of question marks in it: $query = "select id,email new_login status = 1 , id in (" . implode(',', array_fill(0, count($row), '?')) . ") "; $stmt = $con->prepare($query) or die($con->error()); // bind id values prepared sql statement call_user_func_array(array($stmt, 'bind_param'), $row); // execute sql query $stmt->execute(); // fetch result array $res = $stmt->get_result(); while($email = $res->fetch_array(mysqli_assoc)) { // check whether concerns or cc address: if (array_search($email["id"], $row) < $to_count) { $to[$email["id"]] = $email["email"]; } else { $cc[$email["id"]] = $email["email"]; } } $stmt->close(); // use $to , $cc send email // ... }
Comments
Post a Comment