-1

我使用 PHP 和 MySQL 创建了一个个人消息传递系统。系统的一部分允许向适合特定组的人发送组电子邮件。

发送电子邮件的代码是:

session_start();
include_once('../../dbconnect.php');

$email=$_SESSION['email'];
$to = $_POST ['touser'];
$toemail = $_POST['touseremail'];
$from = $_SESSION['name'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$time = time();

if ($to == 'Level 4'){
 $usersquery = "SELECT email FROM users WHERE account = 'Level4'";
 $getusers = $conn -> query($usersquery);
 while ($row = $getusers->fetch_assoc()){

  foreach ($row as $value){

 $query ="INSERT INTO messages (to_user, to_email, subject, message, from_user, from_email, daterecord) VALUES ('" . $to . "', '" . $value . "', '" . mysqli_real_escape_string($conn, $subject) . "', '" . mysqli_real_escape_string($conn, $message) . "', '$from', '$email', '$time')";
   $send = $conn -> query($query);

  }

 }

}

这很好用,但问题是当我打开发件箱时,会显示每条单独的消息 - 当一大群人收到消息时,这会堵塞发件箱并且非常烦人!发件箱的代码是:

$emailquery = "SELECT * FROM messages WHERE from_email = '$email' AND sent_deleted = 'no' ORDER BY daterecord DESC";
$sentemails = $conn->query($emailquery);
$emailcount = $sentemails ->num_rows;

if ($emailcount == 0){
 echo '<div>No messages sent</div>';
}

else{
?>
<table class="udtable">
    <tr>
        <th class="reqhead">To</th>
        <th class="reqhead">Subject</th>
        <th class="reqhead"></th>
    </tr>
<?
$i=1;
while ($sent = $sentemails->fetch_assoc()){
if ($i%2 != 0){
        $rowclass = 'reqodd';
        }
        else {
        $rowclass = 'reqeven';
        }

 echo ' <tr class = "' . $rowclass . '">
         <td class="reqfrom">' . $sent['to_user'] . '</td>
         <td class="reqsubj">' . $sent['subject'] . '</td>
         <td class="req"><a id="link' . $sent['id'] . '" href="#" class="sentopen">Open</a></td>
         <td class="reqmessage"><pre class=sentboxmessage>' . $sent['message'] . '</pre></td>
         <td class="reqid">' . $sent['id'] . '</td>
        </tr>';
        $i++;
}

?>
</table>
<?
}

?>

发件箱如下所示:

在此处输入图像描述

我想将所有这些消息合并到一行中。

有任何想法吗?

谢谢!

4

1 回答 1

0

您需要在您的发件箱代码片段中的 mysql select 语句中使用分组

SELECT * FROM messages WHERE from_email = '$email' AND sent_deleted = 'no' GROUP BY subject, daterecord ORDER BY daterecord DESC

这将起作用,并将按主题和日期记录对您的消息进行分组。因此,任何同时发送的具有相同主题的消息都将被组合在一起。如果多个人可以同时发送具有相同主题的消息,您可能需要添加更多分组选项。

于 2013-04-23T18:47:19.940 回答