Jump to content

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