0

我正在制作一个通知脚本,目前有它来显示关于同一图像的多个通知,例如 john 喜欢 image_name,sarah 喜欢 image_name -> 2 个人喜欢 image_name。

这就是我到目前为止所拥有的。

$query = mysql_query("SELECT type, extra_id, COUNT(*), id AS count FROM `notifications` WHERE `receiver_id` = '14' AND `read` = '0' GROUP BY type, extra_id");
while($row = mysql_fetch_array($query)){

    if($row['count'] == 1){

        switch ($row['type']) {
            case "Following":
                echo "John Doe is now following you <br />";
            break;
            case "Liked":
                $image_id = $row['extra_id'];
                $image_q = mysql_query("SELECT * FROM `images` WHERE `id` = '$image_id' LIMIT 1");
                $image = mysql_fetch_array($image_q);
                echo "John Doe likes ".$image['heading']."<br />";
            break;
        }

    }   else   {

        switch ($row['type']) {
            case "Following":
                echo $row['count']." New Users are following you <br />";
            break;
            case "Liked":
                $image_id = $row['extra_id'];
                $image_q = mysql_query("SELECT * FROM `images` WHERE `id` = '$image_id' LIMIT 1");
                $image = mysql_fetch_array($image_q);
                echo $row['count']." Users Like ".$image['heading']."<br />";
            break;
        }

    }

}

我的数据库对于通知表看起来像这样。 http://i.minus.com/iZtrR0MNZ95Qn.png

代码的输出是,

2 New Users are following you 
5 Users Like IMAGE_ID_29
John Doe likes IMAGE_ID_50

它应该在哪里

2 New Users are following you
John Doe likes IMAGE_ID_29
2 Users like IMAGE_ID_50
4

1 回答 1

1

您当前的查询是:

SELECT type, extra_id, COUNT(*), id AS count FROM `notifications` WHERE `receiver_id` = '14' AND `read` = '0' GROUP BY type, extra_id

也就是说,您正在使用该id字段作为您的count.

您可能希望将其重写为:

SELECT type, extra_id, COUNT(*) AS count, id FROM `notifications` WHERE `receiver_id` = '14' AND `read` = '0' GROUP BY type, extra_id
于 2012-08-22T16:22:01.933 回答