2014-06-10 196 views
3

我有以下功能,假設發送電子郵件給每個用戶。我相信在我的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) { ["[email protected]"]=> string(3) "118" } array(1) { ["[email protected]"]=> string(3) "119" } 
+0

看起來你並不需要'$西班牙郵政=陣列($ correoUser => $ ID用戶);'可言的,你應該改變'$ mailer-> addRecipient($西班牙郵政);''來$ mailer-> addRecipient($ correoUser);' – James

回答

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(); 
} 
+0

它似乎工作得很好,直到你調用'sendMailToUser'函數。它由於某種原因不能正確調用該功能 – MONZTAAA

+0

你看到了什麼樣的錯誤? – stefan

+0

空白頁面,我在函數被調用的上方做了一個var_dump,並且它一直進入該調用,之後它不會。 – MONZTAAA