我已经为我网站的新闻提供了一段时间了。我有网站设置,成员可以评论评论。目前,新闻摘要显示所有评论成员发表在“主要”评论中。
我想只显示最新的子评论。我目前正在尝试使用此代码的代码区域是“location_comments”和“location_comments_b”。
这是我最近发布的帖子即新闻源的完整代码。它目前有效,但显示所有“location_comments_b”。
<?
unset($timestamp);
if(isset($_GET['rc_view'])){ $rc_view = $_GET['rc_view']; }else{ $rc_view = ""; }
//Check to see if user is logged in to display stats updates
if(($userId = is_logged())!= 0)
{
//Check to see if user wants to see more posts
if(isset($_GET['offset'])){ $offset = $_GET['offset']; }
//echo "($rc_view)";
// no of elements per page
if(isset($offset)){
$limitfriendstatus = "$offset";
}else{
$limitfriendstatus = "10";
}
require "pages/my/status/commentnew.inc";
echo "<br>";
$q_status = "
(SELECT status.timestamp, status.id, status.com_uid, status.com_name, status.com_content, status.com_id, friend.userId1, friend.userId2, friend.status1, friend.status2,
'status' AS post_type FROM status
LEFT JOIN friend
ON (status.com_id = friend.userId1 AND friend.userId2 = $userIdme)
OR (status.com_id = friend.userId2 AND friend.userId1 = $userIdme)
WHERE friend.status1 = '1' AND friend.status2 = '1'
OR status.com_id = '$userIdme'
GROUP BY status.id)
";
$q_status_com = "
(SELECT statcom.timestamp, statcom.id, statcom.statcom_uid, statcom.statcom_name, statcom.statcom_content, statcom.statcom_id, friend.userId1, friend.userId2, friend.status1, friend.status2,
'statcom' AS post_type FROM statcom
LEFT JOIN friend
ON (statcom.statcom_uid = friend.userId1 AND friend.userId2 = $userIdme)
OR (statcom.statcom_uid = friend.userId2 AND friend.userId1 = $userIdme)
WHERE friend.status1 = '1' AND friend.status2 = '1'
OR statcom.statcom_uid = '$userIdme'
GROUP BY statcom.id)
";
$s_status = "
(SELECT sweet.timestamp, sweet.sid, sweet.sweet_location, sweet.sweet_sec_id, sweet.sweet_owner_userid, sweet.sweet_userid, friend.userId1, friend.userId2, friend.status1, friend.status2,
'sweet' AS post_type FROM sweet
LEFT JOIN friend
ON (sweet.sweet_userid = friend.userId1 AND friend.userId2 = $userIdme)
OR (sweet.sweet_userid = friend.userId2 AND friend.userId1 = $userIdme)
WHERE ( friend.status1 = '1' AND friend.status2 = '1'
OR sweet.sweet_userid = '$userIdme' )
AND NOT sweet.sweet_location = 'status' AND NOT sweet.sweet_location = 'statuscom'
AND NOT sweet.sweet_location = 'profilecomments_b' AND NOT sweet.sweet_location = 'writingcomments_b'
AND NOT sweet.sweet_location = 'artcomments_b' AND NOT sweet.sweet_location = 'club_events_com'
AND NOT sweet.sweet_location = 'clubevent' AND NOT sweet.sweet_location = 'club_events_com_b'
AND NOT sweet.sweet_location = 'profilecomments' AND NOT sweet.sweet_location = 'clubcomments'
AND NOT sweet.sweet_location = 'clubcomments_b' AND NOT sweet.sweet_location = 'artcomments'
AND NOT sweet.sweet_location = 'writingcomments' AND NOT sweet.sweet_location = 'locations'
AND NOT sweet.sweet_location = 'location_comments' AND NOT sweet.sweet_location = 'location_comments_b'
GROUP BY sweet.sid)
";
$pc_status = "
(SELECT profilecomments.timestamp, profilecomments.id, profilecomments.com_uid, profilecomments.com_name, profilecomments.com_content, profilecomments.com_id, friend.userId1, friend.userId2, friend.status1, friend.status2,
'profilecomment' AS post_type FROM profilecomments
LEFT JOIN friend
ON (profilecomments.com_uid = friend.userId1 AND friend.userId2 = $userIdme)
OR (profilecomments.com_uid = friend.userId2 AND friend.userId1 = $userIdme)
WHERE friend.status1 = '1' AND friend.status2 = '1'
OR profilecomments.com_uid = '$userIdme'
GROUP BY profilecomments.id)
";
$pc_statcom = "
(SELECT profilecomments_b.timestamp, profilecomments_b.id, profilecomments_b.statcom_uid, profilecomments_b.statcom_name, profilecomments_b.statcom_content, profilecomments_b.statcom_id, friend.userId1, friend.userId2, friend.status1, friend.status2,
'profilestatcom' AS post_type FROM profilecomments_b
LEFT JOIN friend
ON (profilecomments_b.statcom_uid = friend.userId1 AND friend.userId2 = $userIdme)
OR (profilecomments_b.statcom_uid = friend.userId2 AND friend.userId1 = $userIdme)
WHERE friend.status1 = '1' AND friend.status2 = '1'
OR profilecomments_b.statcom_uid = '$userIdme'
GROUP BY profilecomments_b.id)
";
$vpc_status = "
(SELECT writingcomments.timestamp, writingcomments.id, writingcomments.com_uid, writingcomments.com_name, writingcomments.com_content, writingcomments.com_id, friend.userId1, friend.userId2, friend.status1, friend.status2,
'vpcom' AS post_type FROM writingcomments
LEFT JOIN friend
ON (writingcomments.com_uid = friend.userId1 AND friend.userId2 = $userIdme)
OR (writingcomments.com_uid = friend.userId2 AND friend.userId1 = $userIdme)
WHERE friend.status1 = '1' AND friend.status2 = '1'
OR writingcomments.com_uid = '$userIdme'
GROUP BY writingcomments.id)
";
$vpc_statcom = "
(SELECT writingcomments_b.timestamp, writingcomments_b.id, writingcomments_b.statcom_uid, writingcomments_b.statcom_name, writingcomments_b.statcom_content, writingcomments_b.statcom_id, friend.userId1, friend.userId2, friend.status1, friend.status2,
'vpstatcom' AS post_type FROM writingcomments_b
LEFT JOIN friend
ON (writingcomments_b.statcom_uid = friend.userId1 AND friend.userId2 = $userIdme)
OR (writingcomments_b.statcom_uid = friend.userId2 AND friend.userId1 = $userIdme)
WHERE friend.status1 = '1' AND friend.status2 = '1'
OR writingcomments_b.statcom_uid = '$userIdme'
GROUP BY writingcomments_b.id)
";
$art_status = "
(SELECT artcomments.timestamp, artcomments.id, artcomments.com_uid, artcomments.com_name, artcomments.com_content, artcomments.com_id, friend.userId1, friend.userId2, friend.status1, friend.status2,
'artcom' AS post_type FROM artcomments
LEFT JOIN friend
ON (artcomments.com_uid = friend.userId1 AND friend.userId2 = $userIdme)
OR (artcomments.com_uid = friend.userId2 AND friend.userId1 = $userIdme)
WHERE friend.status1 = '1' AND friend.status2 = '1'
OR artcomments.com_uid = '$userIdme'
GROUP BY artcomments.id)
";
$art_statcom = "
(SELECT artcomments_b.timestamp, artcomments_b.id, artcomments_b.statcom_uid, artcomments_b.statcom_name, artcomments_b.statcom_content, artcomments_b.statcom_id, friend.userId1, friend.userId2, friend.status1, friend.status2,
'artstatcom' AS post_type FROM artcomments_b
LEFT JOIN friend
ON (artcomments_b.statcom_uid = friend.userId1 AND friend.userId2 = $userIdme)
OR (artcomments_b.statcom_uid = friend.userId2 AND friend.userId1 = $userIdme)
WHERE friend.status1 = '1' AND friend.status2 = '1'
OR artcomments_b.statcom_uid = '$userIdme'
GROUP BY artcomments_b.id)
";
//Checks for location comments
$location_status = "
(SELECT location_comments.timestamp, location_comments.id, location_comments.com_uid, location_comments.com_name, location_comments.com_content, location_comments.com_id, friend.userId1, friend.userId2, friend.status1, friend.status2,
'loc_com' AS post_type FROM location_comments
LEFT JOIN friend
ON (location_comments.com_uid = friend.userId1 AND friend.userId2 = $userIdme)
OR (location_comments.com_uid = friend.userId2 AND friend.userId1 = $userIdme)
WHERE friend.status1 = '1' AND friend.status2 = '1'
OR location_comments.com_uid = '$userIdme'
GROUP BY location_comments.id)
";
//Checks for location comments comments aka b
$location_status_b = "
(SELECT location_comments_b.timestamp, location_comments_b.id, location_comments_b.statcom_uid, location_comments_b.statcom_name, location_comments_b.statcom_content, location_comments_b.statcom_id, friend.userId1, friend.userId2, friend.status1, friend.status2,
'loc_com_b' AS post_type FROM location_comments_b
LEFT JOIN friend
ON (location_comments_b.statcom_uid = friend.userId1 AND friend.userId2 = $userIdme)
OR (location_comments_b.statcom_uid = friend.userId2 AND friend.userId1 = $userIdme)
WHERE friend.status1 = '1' AND friend.status2 = '1'
OR location_comments_b.statcom_uid = '$userIdme'
GROUP BY location_comments_b.id)
";
$club_status = "
(SELECT clubcomments.timestamp, clubcomments.id, clubcomments.com_uid, clubcomments.com_name, clubcomments.com_content, clubcomments.com_id, friend.userId1, friend.userId2, friend.status1, friend.status2,
'clubcom' AS post_type FROM clubcomments
LEFT JOIN friend
ON (clubcomments.com_uid = friend.userId1 AND friend.userId2 = $userIdme)
OR (clubcomments.com_uid = friend.userId2 AND friend.userId1 = $userIdme)
WHERE friend.status1 = '1' AND friend.status2 = '1'
OR clubcomments.com_uid = '$userIdme'
GROUP BY clubcomments.id)
";
$club_statcom = "
(SELECT clubcomments_b.timestamp, clubcomments_b.id, clubcomments_b.statcom_uid, clubcomments_b.statcom_name, clubcomments_b.statcom_content, clubcomments_b.statcom_id, friend.userId1, friend.userId2, friend.status1, friend.status2,
'clubstatcom' AS post_type FROM clubcomments_b
LEFT JOIN friend
ON (clubcomments_b.statcom_uid = friend.userId1 AND friend.userId2 = $userIdme)
OR (clubcomments_b.statcom_uid = friend.userId2 AND friend.userId1 = $userIdme)
WHERE friend.status1 = '1' AND friend.status2 = '1'
OR clubcomments_b.statcom_uid = '$userIdme'
GROUP BY clubcomments_b.id)
";
$club_event = "
(SELECT club_events.timestamp, club_events.fsid, club_events.usrida, club_events.author, club_events.content, club_events.subject, clubmem.userId1, clubmem.userId2, clubmem.status1, clubmem.status3,
'clubevent' AS post_type FROM club_events
LEFT JOIN clubmem
ON (club_events.author = clubmem.userId1 AND clubmem.userId2 = $userIdme)
WHERE clubmem.status2 = '1' AND clubmem.status3 = '1'
OR club_events.usrida = '$userIdme'
GROUP BY club_events.fsid)
";
}else{
$limitfriendstatus = "10";
}
$art_update = "
(SELECT art.timestamp, art.id, art.usrida, art.name, art.type, art.title,
art.imgname, art.content1, art.vehicle_id, null,
'art' AS post_type FROM art)";
$club_update = "
(SELECT club.timestamp, club.club_id, club.club_admin1, null, club.club_name, club.club_city,
club.club_state, club.club_about, club.club_privacy, null,
'club' AS post_type FROM club)";
$vp_update = "
(SELECT submit.timestamp, submit.int, submit.usrida, submit.name, submit.color, submit.year,
submit.make, submit.model, submit.type, null,
'submit' AS post_type FROM submit)";
$vp_updateS = "
(SELECT submit.timestamp, submit.int, submit.usrida, submit.name, submit.color, submit.year,
submit.make, submit.model, submit.type, null,
'submit2' AS post_type FROM submit LIMIT 0)";
if($rc_view){
if($rc_view == "status"){ $query = "$vp_updateS UNION ALL $q_status UNION ALL $q_status_com ORDER BY timestamp DESC"; }
if($rc_view == "sweets"){ $query = "$vp_updateS UNION $s_status ORDER BY timestamp DESC"; }
if($rc_view == "comments"){ $query = "$vp_updateS UNION ALL $vpc_statcom UNION ALL $pc_statcom UNION ALL $pc_status UNION ALL $vpc_status UNION ALL $art_status UNION ALL $club_status UNION ALL $art_statcom UNION ALL $club_statcom UNION ALL $location_status UNION ALL $location_status_b ORDER BY timestamp DESC"; }
if($rc_view == "clubs"){ $query = "$vp_updateS UNION ALL $club_update UNION ALL $club_event ORDER BY timestamp DESC"; }
if($rc_view == "vehpro"){ $query = "$vp_updateS UNION $vp_update ORDER BY timestamp DESC"; }
if($rc_view == "vehpic"){ $query = "$vp_updateS UNION $art_update ORDER BY timestamp DESC"; }
}else{
if(($userId = is_logged())!= 0)
{
$query = "
$vp_update UNION ALL
$club_update UNION ALL
$club_event UNION ALL
$art_update UNION ALL
$q_status UNION ALL
$q_status_com UNION ALL
$s_status UNION ALL
$pc_status UNION ALL
$vpc_status UNION ALL
$art_status UNION ALL
$club_statcom UNION ALL
$art_statcom UNION ALL
$pc_statcom UNION ALL
$vpc_statcom UNION ALL
$location_status UNION ALL
$location_status_b UNION ALL
$club_status
ORDER BY timestamp DESC
";
}else{
$query = "
$vp_update UNION ALL
$club_update UNION ALL
$art_update
ORDER BY timestamp DESC
";
}
}
$queryA = "$query LIMIT $limitfriendstatus";
$queryB = "$query";
$result = mysql_query($queryA)
or die ("Couldn't ececute query. A");
$result2 = mysql_query($queryB)
or die ("Couldn't ececute query. B");
$num_rows_posts = mysql_num_rows($result2);
//Clean imgname string just in case its blank on a post
unset($imgname);
while ($row = mysql_fetch_array($result))
{
$bgcolor = "epboxa";
extract($row);
//echo "$usrida";
//Testing timestamp
$timestamp_p = $timestamp;
//echo " ( timestamp = $timestamp_p ) ";
if(isset($vm_id_a)){ $vm_id_a++; }else{ $vm_id_a = '1'; };
echo "<a class='anchor' name='viewmore$vm_id_a'></a>";
$color = stripslashes($color);
$model = stripslashes($model);
////Vehicle Profile Start
if($post_type == "submit"){
require "pages/recentposts/rec_veh_pro.inc";
}
////Vehicle Profile End
////Club Start
if($post_type == "club"){
require "pages/recentposts/rec_club.inc";
}
////Club End
////Vehicle Pic Start
if($post_type == "art"){
require "pages/recentposts/rec_veh_pic.inc";
}
////Vehicle Pic End
////Status Start
if($post_type == "status"){
require "pages/recentposts/rec_status.inc";
}
////Status End
////Status Start
if($post_type == "statcom"){
require "pages/recentposts/rec_status_com.inc";
}
////Status End
////Sweet Start
if($post_type == "sweet"){
require "pages/recentposts/rec_sweet.inc";
}
////Sweet End
////Profile Comment Start
if($post_type == "profilecomment"){
require "pages/recentposts/rec_com_profile.inc";
}
////Profile Comment End
////Profile Comment comments Start
if($post_type == "profilestatcom"){
require "pages/recentposts/rec_com_profile_com.inc";
}
////Profile Comment comments End
////Vehicle Profile Comment Start
if($post_type == "vpcom"){
require "pages/recentposts/rec_com_vp.inc";
}
////Vehicle Profile Comment End
////Vehicle Profile Comment comment Start
if($post_type == "vpstatcom"){
require "pages/recentposts/rec_com_vp_com.inc";
}
////Vehicle Profile Comment comment End
////Vehicle Pic Comment Start
if($post_type == "artcom"){
require "pages/recentposts/rec_com_art.inc";
}
////Vehicle Pic Comment End
////Vehicle Pic Comment comments Start
if($post_type == "artstatcom"){
require "pages/recentposts/rec_com_art_com.inc";
}
////Vehicle Pic Comment comments End
////Club Comment Start
if($post_type == "clubcom"){
require "pages/recentposts/rec_com_club.inc";
}
////Club Comment End
////Club Comment comments Start
if($post_type == "clubstatcom"){
require "pages/recentposts/rec_com_club_com.inc";
}
////Club Comment comments End
////Club Event Start
if($post_type == "clubevent"){
require "pages/recentposts/rec_club_event.inc";
}
////Club Event End
////Location Comment Start
if($post_type == "loc_com"){
require "pages/recentposts/rec_com_locations.inc";
}
////Location Comment End
////Location Comment B Start
if($post_type == "loc_com_b"){
require "pages/recentposts/rec_com_locations_com.inc";
}
////Location Comment B End
//Testing timestamp
//echo " ( timestamp = $timestart ) <br>";
//Clean up vars
unset($timestart, $timestamp_p, $timestamp, $id, $int, $com_uid, $usrida, $com_name, $name, $com_content, $color, $com_id, $year);
unset($statfriend, $userId1, $userId2, $userId1a, $userId2a);
unset($sweet_location, $sweet_id, $sweet_userid, $sweet_url, $sweet_owner_userid);
unset($sw_usernameB, $sw_username, $sw_user_sweeted, $sw_username888, $sw_username88, $ID02, $timestart);
unset($com_uid, $com_id, $com_name, $com_content, $com_uid, $com_id, $com_name, $com_content );
//Clean imgname string just in case its blank on a post
unset($imgname);
} //End of main sql
//Check to see if user is logged in and if there are later post they can view... then show link
if(($userId = is_logged())!= 0)
{
$numofposts = $num_rows_posts;
if(isset($vm_id_a)){}else{$vm_id_a = "0";}
echo "Currently showing $vm_id_a of $numofposts posts.";//testing
if( !isset($numofposts) ){$numofposts = "0";}else{
//$plussome = ($num_rows - $numofposts);
//echo "<Br>$limitfriendstatus - $num_rows - $numofposts - $plussome <br>";
if(isset($num_rows)){}else{ $num_rows = ""; }
if($limitfriendstatus < $num_rows || $limitfriendstatus < $numofposts){
$vm_id = $limitfriendstatus + 1;
echo "<table width=100%><tr><td>";
echo " <center><a href=\"?rc_view=$rc_view&offset=" . ($limitfriendstatus + 10) . "#viewmore$vm_id\">Show More Posts...</a> </center> ";
echo "</td></tr></table><br><br>";
}
}
}
?>
答案 0 :(得分:0)
要获取每个location_comments_b.id的最新评论,JOIN
一个MAX(TimeStamp)
子查询:
SELECT
b.timestamp, b.id, b.statcom_uid, b.statcom_name, b.statcom_content,
b.statcom_id, f.userId1, f.userId2, f.status1, f.status2,
'loc_com_b' AS post_type
FROM location_comments_b b
JOIN (SELECT id,MAX(timestamp) timestamp
FROM location_comments_b GROUP BY id) t
ON t.id = b.id
AND t.timestamp = b.timestamp
LEFT JOIN friend f
ON (b.statcom_uid = f.userId1 AND f.userId2 = $userIdme)
OR (b.statcom_uid = f.userId2 AND f.userId1 = $userIdme)
WHERE f.status1 = '1' AND f.status2 = '1'
OR b.statcom_uid = '$userIdme'