我正在製作一個通知腳本,目前有這個功能可以顯示多個關於同一圖像的通知,例如john likes image_name,sarah likes 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
它可能無法幫助回答你的問題,但你應該停止使用'mysql_ *'功能。他們正在被棄用。請使用[PDO](http://php.net/manual/en/book.pdo.php)(自PHP 5.1起支持)或[mysqli](http://php.net/manual/en/book)。 mysqli.php)(自PHP 4.1起支持)。如果你不確定使用哪一個,[閱讀本文](http://net.tutsplus.com/tutorials/php/pdo-vs-mysqli-which-should-you-use/)。 – Matt
沒有答案,但我看到基本上相同的代碼重複在您的代碼段中:http://en.wikipedia.org/wiki/Don't_repeat_yourself – PeeHaa
我會盡量減少它,並刪除幹區後,我得到它的工作。感謝您的輸入:) – user1527354