donglu5000 2014-03-05 16:14
浏览 323
已采纳

PHP中的Foreach循环只获取第一个实例

I have the following code that was written to send an email reminder to users, but it only sends an email to the first user in the list and the remaining users are not emailed. The query does work in SQL to pull all of the users

$users = array(mysql_fetch_array(mysql_query("SELECT DISTINCT wp_users.* FROM wp_users LEFT JOIN (SELECT lead_id, MAX(case when field_number = 7 then value end) AS email, MAX(case when field_number = 8 then value end) AS tournament_id FROM wp_rg_lead_detail GROUP BY lead_id) AS t1 ON t1.email = wp_users.user_email AND t1.tournament_id = '$tournament_id' WHERE t1.lead_id IS NULL AND id !=1")));


// Create the replacements array
$replacements = array();
foreach ($users as $user) {
$replacements[$user['user_email']] = array (
'{user_login}' => $user['user_login']
);
echo $user['user_email'] . "<br />" . $user['user_login']; 
}

// Create the mail transport configuration
$transport = Swift_MailTransport::newInstance();

// Create an instance of the plugin and register it
$plugin = new Swift_Plugins_DecoratorPlugin($replacements);
$mailer = Swift_Mailer::newInstance($transport);
$mailer->registerPlugin($plugin);

// Create the message
$message = Swift_Message::newInstance();
$message->setSubject("Pure Fantasy Golf Pick Reminder for {user_login}");
$message->setBody("{user_login}, this is a reminder that you need to submit a pick for this weeks tournament " . $tournament_name . ". Please go to http://dev.purefantasygolf.com to make your pick now.");
$message->setFrom("purefantasygolf@gmail.com", "Pure Fantasy Golf");

// Send the email
foreach($users as $user) {
$message->setTo($user["user_email"], $user["user_login"]);
$mailer->send($message);
}
  • 写回答

3条回答 默认 最新

  • dpa0760 2014-03-05 16:18
    关注

    Have a look at the documentation of mysql_fetch_array.

    You need to loop over your results like this:

    $result = mysql_query("SELECT DISTINCT wp_users.* FROM wp_users LEFT JOIN (SELECT lead_id, MAX(case when field_number = 7 then value end) AS email, MAX(case when field_number = 8 then value end) AS tournament_id FROM wp_rg_lead_detail GROUP BY lead_id) AS t1 ON t1.email = wp_users.user_email AND t1.tournament_id = '$tournament_id' WHERE t1.lead_id IS NULL AND id !=1");
    
    $replacements = array();
    while($user = mysql_fetch_array($result)) {
        $replacements[$user['user_email']] = array (
            '{user_login}' => $user['user_login']
        );
        echo $user['user_email'] . "<br />" . $user['user_login'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集