Jump to content

Filtering out duplicate entries from a while loop


Andrew777

Recommended Posts

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.

 

 

 

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.