PHP: How can I assign unique rows to array?

I have a query which gets sent to an array through a while loop.  What I would like to do though is, take that while loop and have it only loop through rows where the userid is unique.  Here's the code:

 
$todaygreenleadersquery = SecureQuery("
	SELECT distinct a.user AS userid, DATE_FORMAT( FROM_UNIXTIME( date ) , '%H' ) AS HOUR , COUNT( a.id ) AS total
	FROM posts a
	LEFT JOIN users b ON a.user = b.id
	WHERE a.date > $datetimestamptoday
	GROUP BY userid, DATE_FORMAT( FROM_UNIXTIME( date ) , '%H' )
	ORDER BY total DESC , b.posts DESC
");
$todaygreencount=0;
while($row = mysql_fetch_array($todaygreenleadersquery)){		
	$todaygreencount++;
	echo "
		<td>$todaygreencount</td>
		<td>$row[userid])</td>
		<td'>$row[total]</td>
	";
}

Open in new window


So if $row[userid] was 5, for example, it wouldn't execute all that code within that while loop again if it comes to another $row[userid] that was 5.  (5 is just an example).
davideo7Asked:
Who is Participating?
 
Ray PaseurCommented:
Something along these lines might work.  There are lots of ways to handle this.
// AN ARRAY OF USERIDS WE HAVE ALREADY PROCESSED
$elements = array();

// ITERATE OVER THE RESULTS SET
while($row = mysql_fetch_array($todaygreenleadersquery))
{
    // SKIP USERIDS WE HAVE ALREADY PROCESSED
    if (in_array($row[userid], $elements)) continue;
    
    // STORE THIS USERID
    $elements[] = $row[userid];
    
    // PROCESS THE DATA FOR THE FIRST INSTANCE OF $row[userid]
    $todaygreencount++;
    echo "
        <td>$todaygreencount</td>
        <td>$row[userid])</td>
        <td'>$row[total]</td>
    ";
}

Open in new window

0
 
Ray PaseurCommented:
If you assign array keys and values to the same data elements, the PHP associative array handler will overwrite the array value fields when the key matches a previous key.  It's a very easy way to use an iterator to, in effect, create an array_unique() condition.
0
 
MarsMCommented:
$todaygreenleadersquery = SecureQuery("
        SELECT distinct a.user AS userid, DATE_FORMAT( FROM_UNIXTIME( date ) , '%H' ) AS HOUR , COUNT( a.id ) AS total
        FROM posts a
        LEFT JOIN users b ON a.user = b.id
        WHERE a.date > $datetimestamptoday
        GROUP BY userid, DATE_FORMAT( FROM_UNIXTIME( date ) , '%H' )
        ORDER BY total DESC , b.posts DESC
");
$todaygreencount=0;
$users_done=array();
while($row = mysql_fetch_array($todaygreenleadersquery)){               
        if (!empty($row['userid'])) {
           continue;
        }
        $users_done[$row['userid']]=1;
        $todaygreencount++;
        echo "
                <td>$todaygreencount</td>
                <td>$row[userid])</td>
                <td'>$row[total]</td>
        ";
}

Open in new window


However, I would have a look at the sql-query to accomplish this.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
davideo7Author Commented:
Ray_Paseur: How would I go about coding that?
0
 
davideo7Author Commented:
That worked very well, thank you so much!
0
 
Ray PaseurCommented:
Glad to help -- thanks for the points, ~Ray
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.