-2

I am pulling out som data from a table called taskmanager. One of the columns contains 'assigned_user_id' and can occur multiple times.

I am trying to figure out, how do I add sequential numbers where assigned_user_id is the same (duplicate).

But I dont really know where to start.

Inside my while loop, I have a counter that counts the duplicates, and I tried playing with that, but couldnt get it to work.

And I dont know if this is the correct path to be on.

The counter inside loop:

$CountOccurence = $conn->prepare("SELECT COUNT(task_assigned_user) FROM taskmanager WHERE task_assigned_user = ?");
$CountOccurence->bind_param("i", $gettaskassigneduser);
$CountOccurence->execute();
$CountOccurence->bind_result($total);
    while ($CountOccurence->fetch()) {
        echo "(".$total.")";
    }                                   
$CountOccurence->close();

This gives:

Normal sequential counter   Assigneduserid     **sequential by assigned_user_id**
  1                              1                          2 ($total)
  2                              2                          2 ($total)
  3                              2                          2 ($total)
  4                              4                          1 ($total)
  5                              1                          2 ($total)

What I would like to have is:

Normal sequential counter   Assigneduserid     **sequential by assigned_user_id**
  1                              1                          1
  2                              2                          1
  3                              2                          2
  4                              4                          1
  5                              1                          2

Can someone pls help me or point me to a direction I can look at. Thank you.

MauiRiis
  • 21
  • 5
  • Just add the user ids into an array, then you can use `array_count_values` to easily determine how many times each one occurred so far. – CBroe May 05 '22 at 09:10
  • Could you pls elaborate. I have looked at array_count_values before my question, but couldnt figure out how to count 1, 2 unique for each Assigneduserid. I ended up with same result, just displaying the total besides each Assigneduserid. – MauiRiis May 05 '22 at 09:13
  • You aren't executing the counting query inside of another loop, are you??? If so, I would recommend a JOIN or something to reduce the total trips to the database. – mickmackusa May 05 '22 at 09:25
  • I am. Because I was trying to get above result. But no luck so far. Not even with array_count_values,. – MauiRiis May 05 '22 at 09:31

1 Answers1

0

Just add the user ids into an array while you are looping over the data, then you can use array_count_values to easily determine how many times each one occurred so far.

In the first loop iteration, you add the AUid 1 to your array, then the array_count_values will return you the array [1 => 1]. So you use your AUid 1 as key to access the value 1, and output that into your 3rd column.

In the second loop iteration, you add the AUid 2 to your array. Then array_count_values will return [1 => 1, 2 => 1]. Now you use the AUid 2 to access that value 1, and output it into your 3rd column.

In the third loop iteration, you add another AUid 2 to your array. Then array_count_values will return [1 => 1, 2 => 2]. Now you use the AUid 2 to access that value 2, and output it into your 3rd column.

And so on ...

CBroe
  • 91,630
  • 14
  • 92
  • 150
  • I am really sorry CBroe, but I don't know how to do this. As soon as I use array_count_values it display error. Can only count integer or string. Any chance you can glance my code here?: (Line 181 is were I woult like sequential the result - its ok if u dont want too) https://jsfiddle.net/MauiRiis/5fv1mkxt/ – MauiRiis May 05 '22 at 09:53
  • _"As soon as I use array_count_values it display error. Can only count integer or string."_ - well then you must have put something else than an actual integer or string value into the array, I guess. But your code in the fiddle doesn't even appears to contain any of this yet? – CBroe May 05 '22 at 10:04
  • I updated the code. Line 192 got the array_count_values (the array value is on line 113 . Which displays: Warning: array_count_values(): Can only count STRING and INTEGER values! in /volume1/WEB/test/taskmanager.php on line 220 Array ( [34] => 1 [30] => 1 [2] => 2 [35] => 1 ) – MauiRiis May 05 '22 at 10:08
  • Please share the URL of the current version of your fiddle then, because https://jsfiddle.net/MauiRiis/5fv1mkxt/ still does not contain `array_count_values` anywhere. – CBroe May 05 '22 at 10:15
  • Trying again. array_count is at line 122. The array value ($gettaskassigneduser) is at line 43 - https://jsfiddle.net/MauiRiis/5fv1mkxt/4/ – MauiRiis May 05 '22 at 10:50
  • You are trying to use `$gettaskassigneduser` as an array and a scalar value there, both at the same time makes no sense. If it is an array, then stuff like `$showresponsible->bind_param("i", $gettaskassigneduser);` and `if($gettaskassigneduser != $Userid)` doesn't make much sense. Use a different variable name for the array. – CBroe May 05 '22 at 11:02
  • I tried changing $gettaskassigneduser = $row["task_assigned_user"]; to $countassigneduser[] = $row["task_assigned_user"]; didnt help. Same error. Sorry. I am abit lost here. It's out of my league. – MauiRiis May 05 '22 at 11:22
  • $countassigneduser is only 2,4,56,22,45,24 etc. So isnt array_count_arrays missing a component? – MauiRiis May 05 '22 at 11:24
  • What do you mean, _"missing a component"_? – CBroe May 05 '22 at 11:27
  • It also looks like (jsfiddle is _not_ a good place to present PHP code, no syntax highlighting, hard to read) you are only calling the function _after_ the loop - that makes no sense of course, you need to count the number of _current_ occurrences, _during_ the loop. – CBroe May 05 '22 at 11:29
  • Think you should strike that. Sorry. print_r($countassigneduser); gives = Array ( [0] => 34 [1] => 30 [2] => 2 [3] => [4] => 2 [5] => 35 ) 6 But $arr = array_count_values($countassigneduser); print_r($arr); gives error. – MauiRiis May 05 '22 at 11:29
  • I dont know where to share. Pastebin?: https://pastebin.com/Aan02h9t – MauiRiis May 05 '22 at 11:30
  • Result inside loop. https://ibb.co/4RhHz1d – MauiRiis May 05 '22 at 11:34
  • Proper code indentation would makes this a lot easier to read ... – CBroe May 05 '22 at 11:38
  • Replace the part where you are currently calling array_count_values and print_r-ing the result, with a `var_dump($countassigneduser);` - so that we can see what values are actually in there at this point. – CBroe May 05 '22 at 11:39
  • But the only line where you are adding items to this, appears to be `$countassigneduser[] = $row["task_assigned_user"];` - so that would have to mean that `$row["task_assigned_user"]` does not contain an actual ID value then. Do not all of your tasks _have_ a user assigned? If so, that is something you should have mentioned! – CBroe May 05 '22 at 11:41
  • Only add an element for the current record then, if this assigned user actually exists - `if($row["task_assigned_user"]) { $countassigneduser[] = $row["task_assigned_user"]; }`. And in the place where you want to output this sequential number, it should be `if($row["task_assigned_user"]) { echo array_count_values($countassigneduser)[$row["task_assigned_user"]]; }` then. – CBroe May 05 '22 at 11:44
  • Here is mage of tables. https://ibb.co/1sB2HSV - task_assigned_user is not always filled. Sorry if some information wasnt there. Not on purpose. I am really new to php. – MauiRiis May 05 '22 at 11:44
  • So it was only because there could be an empty cell in the column. I didnt think of that. – MauiRiis May 05 '22 at 11:47