Jump to content

Counting from two fields


ViperSBT

Recommended Posts

I have two fields in a table that both may contain a userid.  I would like to find the number of rows for each userid occurance, no matter which field it is in....  Something like:

So if the two fields were user1 and user2 and the data looked like this:

user1  user2
  2        4
  1        2
  3        5
  1        2

I would want my result to be:

1 = 2 Counts
2 = 3 Counts
3 = 1 Counts
4 = 1 Counts
5 = 1 Counts
Link to comment
https://forums.phpfreaks.com/topic/14517-counting-from-two-fields/
Share on other sites

That would require that I provide the UserID to count for, I want it to return all UserIDs in the table.  Also this provides two separate columns for each UserID, I need one column that is the UserID and another column that is the counts for the number of times that UserID exists in either of the fields of that table.
Try this:
[code]<?php
$temp = array();
$q = "select user1, user2 from tablename"
$rs = mysql_query($q) or die("Problem with query: $q<br>" . mysql_error());
while($rw = mysql_fetch_assoc($rs)) {
    $temp[] = $rw['user1'];
    $temp[] = $rw['user2'];
}
$nw = array_count_values($temp);
echo '<pre>' . print_r($nw,true) . '</pre>';
?>[/code]

Ken

Archived

This topic is now archived and is closed to further replies.

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