Andrew777 Posted September 26, 2011 Share Posted September 26, 2011 Hi Guys, This is a new post based on my last post for the same but I've revamped the code a little from the last post since I couldn't get it to work even with the suggestions, so my new code is below... Basically I have an updates table (updates_all) and a subscriptions table (subscriptions) and in the updates table items are entered for multiple users with querydate which increases based on the unix timestamp of the update. The Subscriptions table holds the data for users who are subscribed to other user's profiles. My issue is to show a logged in user his subscriptions and when a user he is subscribed to has a new update that user shows up on top of the list of his users. With the code below, i've been able to list the contents of the updates table, filter out the profiles which he is not subscribed to, and order the results by the most recent querydate. My question now is how do I run the while loop so it filters out all but one result per/member name?? Results output below the code... $sql_findsubs = "SELECT * FROM updates_all ORDER BY auto_id DESC"; $rs_findsubs = mysql_query($sql_findsubs); $subscripPID = array(); $sql="SELECT * FROM subscriptions WHERE memberid='$id' "; $rs=mysql_query($sql); while($row=mysql_fetch_array($rs)) { $subscripPID[] =$row['profileid']; } while($rowfs = mysql_fetch_assoc($rs_findsubs)) { $id = $rowfs['id']; if (in_array($id, $subscripPID)) { echo $rowfs['auto_id'].' - '.$rowfs['querydate'].'.......'.$rowfs['member']; echo '<br>'; } } RESULTS: So if John is subscribed to Bob, Mary, Jim and Andy, I want to only show the four rows below, not all the other entries because those have a smaller querydate for those members. AutoID - Querydate - Name 130 - 1109092040.......Bob <-------- I want to show this one only for Bob 129 - 1109092039.......Bob 128 - 1109091935.......Bob 98 - 1106162306.......Mary <-------I want to show this one only for Mary 97 - 1106162254.......Mary 96 - 1106162215.......Jim <-------I want to show this one only for Jim 90 - 1105062043.......Bob 89 - 1105052200.......Andy <------I want to show this one only for Andy 88 - 1105052154.......Bob 87 - 1105052154.......Bob 86 - 1105052038.......Bob 80 - 1105052034.......Andy 79 - 1105052032.......Andy 73 - 1105052023.......Bob 72 - 1105052018.......Andy 60 - 1103192354.......Bob 4 - 1103172045.......Bob Any help is greatly appreciated... Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/247909-filtering-out-duplicate-entries-from-a-while-loop/ Share on other sites More sharing options...
Buddski Posted September 27, 2011 Share Posted September 27, 2011 Try this.. <?php // empty array to store the shown users // $members = array(); while($rowfs = mysql_fetch_assoc($rs_findsubs)) { $id = $rowfs['id']; // Make sure the ID is in subscripPID and that this user hasnt been shown before // if (in_array($id, $subscripPID) && !in_array($rowfs['id'],$members)) { // Add them into the displayed list // array_push($members,$rowfs['id']); // Do the output // echo $rowfs['auto_id'].' - '.$rowfs['querydate'].'.......'.$rowfs['member']; echo '<br>'; } } ?> This will have an array of the people you have already shown. Each loop will check against that array, if the users ID is not found it adds it to the array and outputs the desired code.. Quote Link to comment https://forums.phpfreaks.com/topic/247909-filtering-out-duplicate-entries-from-a-while-loop/#findComment-1273117 Share on other sites More sharing options...
MasterACE14 Posted September 27, 2011 Share Posted September 27, 2011 MySQL: DISTINCT Quote Link to comment https://forums.phpfreaks.com/topic/247909-filtering-out-duplicate-entries-from-a-while-loop/#findComment-1273118 Share on other sites More sharing options...
Andrew777 Posted September 27, 2011 Author Share Posted September 27, 2011 Buddski, YOU ROCK!!! I think it's working now... I really appreciate it. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/247909-filtering-out-duplicate-entries-from-a-while-loop/#findComment-1273334 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.