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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
davideo7Author Commented:
Ray_Paseur: How would I go about coding that?
0
Acronis Data Cloud 7.8 Enhances Cyber Protection

A closer look at five essential enhancements that benefit end-users and help MSPs take their cloud data protection business further.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
davideo7Author Commented:
That worked very well, thank you so much!
0
Ray PaseurCommented:
Glad to help -- thanks for the points, ~Ray
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.