使用foreach循环遍历数组

时间:2014-06-10 16:51:07

标签: php arrays email foreach

我有以下功能,可以向每个用户发送电子邮件。我相信我的foreach循环我需要创建一个新的foreach循环来浏览每封电子邮件,然后向每个用户发送一次电子邮件,我该怎么做?

function getCronCuponDescargado(){

    $app = JFactory::getApplication();      
    $db = JFactory::getDBO();

    $query = 'SELECT k2i.title, count(cs.item_id) as contador, cs.user_id, u.email
                FROM #__k2_items as k2i 
                LEFT JOIN #__cuphoneo_subscripcion as cs ON cs.item_id = k2i.id
                LEFT JOIN #__users as u ON u.id = cs.user_id
                WHERE cs.estado = 1 GROUP BY cs.item_id ORDER BY cs.user_id';

    $db->setQuery($query);  
    $result = $db->loadObjectList();

    $query = 'SELECT count(item_id) 
                FROM #__cuphoneo_subscripcion
                WHERE estado = 0';
    $db->setQuery($query);
    $resultado = $db->loadObjectList();

    $mailer =& JFactory::getMailer();
    $mailer->IsHTML(true);  
    $emailSubject = '¡Tus Cuphones!';
    $sender = $mailer->From;
    $fcontent = '<h2>Listado de cupones a los que estas suscrito:</h2>
                    <table>
                        <tr>
                            <th>Nombre del cupón</th>
                            <th>Tiempo restante de cupón</th>
                        </tr>';

    foreach($result as $value){   

        $correoUser = $value->email;
        $idUser = $value->user_id;

        //Loop through array using foreach to send one email to each different user
        $correos = array($correoUser => $idUser);

        var_dump($correos);

        $mailer->setSender($sender);
        $mailer->addRecipient($correos);
        $mailer->setSubject($emailSubject);

        $fcontent .= '<tr>
                        <td>'.$value->title.'</td>                              
                        <td>'.$value->id.'</td>
                        <td>'.$value->contador.'</td>
                      </tr>';

    }

    $fcontent .= '</table>';
    $mailer->setBody($fcontent);
    $send =& $mailer->Send();
}

$correos具有以下值:

array(1) { ["davidfuertesrojas@gmail.com"]=> string(3) "118" } array(1) { ["victoryork87@gmail.com"]=> string(3) "119" }

1 个答案:

答案 0 :(得分:0)

你可以试试这段代码。我没有重构你的,也没有测试过,但你应该明白这个想法。希望它有所帮助。

function getCronCuponDescargado(){

$app = JFactory::getApplication();      
$db = JFactory::getDBO();

$query = 'SELECT k2i.title, count(cs.item_id) as contador, cs.user_id, u.email
            FROM #__k2_items as k2i 
            LEFT JOIN #__cuphoneo_subscripcion as cs ON cs.item_id = k2i.id
            LEFT JOIN #__users as u ON u.id = cs.user_id
            WHERE cs.estado = 1 GROUP BY cs.item_id ORDER BY cs.user_id';

$db->setQuery($query);  
$result = $db->loadObjectList();

$query = 'SELECT count(item_id) 
            FROM #__cuphoneo_subscripcion
            WHERE estado = 0';
$db->setQuery($query);
$resultado = $db->loadObjectList();


$fcontent = '<h2>Listado de cupones a los que estas suscrito:</h2>
                <table>
                    <tr>
                        <th>Nombre del cupón</th>
                        <th>Tiempo restante de cupón</th>
                    </tr>
                    <tr>
                      <td>[title]</td>                              
                      <td>[id]</td>
                      <td>[contador]</td>
                   </tr>
                  </table>
               ';

foreach($result as $value){  
    // set personalized content
    $content = str_replace(array('[title]', '[id]', '[contador]'), array($value->title, $value->id, $value->contador), $fcontent);

    // send message to the current user
    sendMailToUser ($value->email, $content);
}



}

function sendMailToUser ($recipient, $content) {
    $mailer =& JFactory::getMailer();
    $mailer->IsHTML(true);  
    $emailSubject = '¡Tus Cuphones!';
    $sender = $mailer->From;     
    $mailer->setSender($sender);
    $mailer->addRecipient($recipient);
    $mailer->setSubject($emailSubject);
    $mailer->setBody($content);
    $send =& $mailer->Send();
}