PC / tl / sc / pm电子邮件的Codeigniter逻辑

时间:2016-02-06 11:37:16

标签: php codeigniter if-statement logic

我有一个像这样的数组

Array
(
   [0] => Array
       (
        [PM] => 14
        [LD] => 578
        [TL] => 56
        [SC] => 67
        [PC] => 
       )
)

以上只是PC / PM / TL / LD的ID。然后,我将在数据库查询中使用它们来获取其电子邮件ID。

我想使用以下逻辑,我的电子邮件模板应该像这样工作 PC中的“To”和所有其余的CC'ed。

  • 如果没有分配PC,则TL in in email all in cc
  • 如果没有TL,那么LD in in email rest all in cc
  • 如果没有LD,那么PM in in email rest all in cc
  • 如果没有PM,那么SC in in email rest all in cc

另外,我想将电子邮件ID添加到数组中。所以如果我开始:

Array ( 
    [PC] => 
    [TL] => 1109 
    [LD] => 838 
    [PM] => 715,824,694 
    [SC] => 727
) 

我将执行此SQL($ user_sql):

SELECT id,email
FROM   new_login 
WHERE  id IN (1109,838,715,824,694,727) 
AND    status = 1

输出:

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 
)

1 个答案:

答案 0 :(得分:0)

以下代码将:

  • 根据双字母代码的优先顺序排列给定的ID;
  • 创建一个ID列表,将多ID字符串拆分为单独的数组元素;
  • 查询数据库以接收相应的电子邮件地址
  • 将这些电子邮件地址存储在正确数组变量( $ to $ cc
  • 中的键/值对(ID /电子邮件)中

您没有指定您使用的数据库引擎。此代码假定您使用mysqli,并且您有一个名为 $ con 的连接对象。代码的数据库部分未经测试。但它至少应该让你知道如何继续。

代码中的注释应该澄清发生了什么:

// Example data:
$data = Array (
   Array ("PM" => "715,824,694", "LD" => "838", "TL" => "1109", 
          "SC" => "727", "PC" => null)
);

// Define the priority order of the different two letter codes:
$order = array_flip(array("PC", "TL", "LD", "PM", "SC"));

// Iterate over the data
foreach ($data as $row) {
    // Order $row elements by priority and filter out empty values:
    $row = array_filter(array_merge($order, $row));
    // if nothing is left over, then skip this row
    if (count($row) == 0) continue;
    // Count the number of IDs in "TO" addressee:
    $to_count = count(explode(',', array_values($row)[0]));
    // Create one list of IDs and turn comma-separated items 
    // into separate array elements:
    $row = explode(',', implode(',', $row));
    // Initialise arrays that will receive the email addresses for TO / CC
    $to = array();
    $cc = array();
    // Retrieve email addresses from database.
    // You need a valid mysqli connection object: $con
    // Prepare SQL statement to avoid SQL injection, and produce
    // the right number of question marks in it:
    $query = "SELECT id,email FROM new_login WHERE status = 1
              AND id IN (" . implode(',', array_fill(0, count($row), '?')) . ") ";
    $stmt = $con->prepare($query) or die($con->error());
    // Bind the ID values to prepared SQL statement
    call_user_func_array(array($stmt, 'bind_param'), $row);
    // Execute SQL query
    $stmt->execute();
    // Fetch result into array
    $res = $stmt->get_result();
    while($email = $res->fetch_array(MYSQLI_ASSOC)) {
        // Check whether it concerns a TO 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 and $cc to send email
    // ...
}