php - Count number of users for the comma separated values -
i have comma-separated field base_users
in database. how query count totaluser
of group? have no problem calculate totaluser
if data not in comma-separated field.
select count(base_u_id) totaluser base_users base_u_group =".$row['base_gp_id']."
1)base_users |base_u_id | base_u_name | base_u_group | ------------------------------------------ | 1 | username1 | 1, 2, 4 | | 2 | username2 | 3 | | 3 | username3 | 3, 4 | | 4 | username4 | 1, 4 | 2)base_groups | base_gp_id | base_gp_name | ------------------------------ | 1 | group1 | | 2 | group2 | | 3 | group3 | | 4 | group4 | | 5 | group5 |
from sample database above, expected result be:
total user of group1 = 2 total user of group2 = 1 total user of group3 = 2 total user of group4 = 3 total user of group5 = 0
this have tried far:
<?php $getuser = base_executesql("select * base_users"); while($row_getuser = base_fetch_array($getuser)) { $explodedata = explode(", ",$row_getuser['base_u_group']); foreach($explodedata $data) { $getusergroupsql = base_executesql("select count(base_u_id) totaluser base_users user, base_groups gp gp.base_gp_id ='".$data."' "); while($usergroupprofile_row = base_fetch_array($getusergroupsql)) if (base_num_rows($getusergroupsql)!= 0) $totaluser = $usergroupprofile_row["totaluser"]; elseif (base_num_rows($getusergroupsql)== 0) $totaluser = 0; } } ?>
use below logic: hint
$group = array(1=>0,2=>0,3=>0,4=>0,5=>0); $base_u_group = array('1,2,4','3','3,4','1,4'); foreach($base_u_group $gr) { $split = explode(',', $gr); if (!empty($split)) { foreach($split $val) { if ($val) { $group[$val] = $group[$val] + 1; } } } } var_dump($group);
Comments
Post a Comment