<?php

/*
# --------------------------------------------------------
# HAVE E-MAIL DESTINATARIES FROM email_queue IN A SEPARATE TABLE
# --------------------------------------------------------
*/

$addressees = array();
$query = "SELECT id, userid FROM email_queue";
$result = mysql_query($query) or die("ERROR in query: $query");
while(list($id, $userid) = mysql_fetch_row($result)) {
    $addressees[$id] = unserialize($userid);
}

$query = $this->db->filterEngineStatement("CREATE TABLE `email_queue_addressees` (`email_queue_id` INT NOT NULL ,`userid` INT NOT NULL ,PRIMARY KEY ( `email_queue_id` , `userid` )) ENGINE = MYISAM");
mysql_query($query) or die("ERROR in query: $query");

$query = "ALTER TABLE `email_queue` DROP `userid`";
mysql_query($query) or die("ERROR in query: $query");

foreach ($addressees as $id => $useridArr) {
    foreach ($useridArr as $userid) {
        $query = "INSERT INTO email_queue_addressees (email_queue_id, userid) VALUES ($id, $userid)";
        mysql_query($query) or die("ERROR in query: $query");
    }
}

?>
