【问题标题】:Codeigniter logic of PC/tl/sc/pm emailPC/tl/sc/pm email 的 Codeigniter 逻辑
【发布时间】:2016-02-06 11:37:15
【问题描述】:

我有一个这样的数组

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

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

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

  • 如果没有分配 PC,则 TL 进入电子邮件,其余全部在抄送中
  • 如果没有 TL,则 LD 输入到电子邮件中,其余全部在 cc 中
  • 如果没有 LD,则 PM 到电子邮件中,其余全部在 cc 中
  • 如果没有 PM,则 SC 输入电子邮件,其余全部在 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 
)

【问题讨论】:

  • 你试过什么?您的代码示例是什么样的?哪一部分失败了?尽量为读者提供更多信息
  • 您使用哪个数据库?以及您使用哪些函数来连接和检索数据(mysqli_?PDO?)。

标签: php codeigniter if-statement logic


【解决方案1】:

下面的代码将:

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

您没有指定使用哪个数据库引擎。此代码假定您使用 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
    // ...
}

【讨论】:

  • 如果我得到多个值,如 Array ([PC] => [TL] => 645 [LD] => 838 [PM] => 715,824,694 [SC] => 727)
  • [PM] =&gt; 715,824,694 到底是什么意思?是字符串"715,824,694",还是像[PM] =&gt; array(715, 824, 694)这样的数组?请注意,您当前的所有值都是数字,看到字符串或数组出现在预期数字的位置会很奇怪。你能澄清一下吗?
  • 另外,请指定您期望的结果类型。您是否希望将这些组合 ID(告诉我它们是字符串还是数组)分成数字?那么您是否还需要这些两个字母的单词(PM、TL、...),还是只提供一个用于 TO 的 ID 数组和一个用于 CC 的数组,例如您的示例中的 $cc = array(838, 715, 824, 694, 727)? /跨度>
  • 数组的键 PM 处的字符串
  • 你的意思是OR数组?预期的结果是什么?请参阅我的最后评论。那是你想要的输出吗?
猜你喜欢
  • 2014-08-21
  • 1970-01-01
  • 1970-01-01
  • 2012-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-12
相关资源
最近更新 更多