我想要在代码点火器中为每个用户单独计数


I want Individual count For each users in codeigniter

我试图显示每个用户的所有候选计数。但它只显示总数。如何显示他们上传的每个用户的单个计数。帮助e解决这个问题。。我的意思是,我想要确切的查询。。。提前感谢下面是用户控制器

用户控制器:

function manage(){
  $userId = $this->phpsession->get("userId");
  $userType = $this->phpsession->get('userType');
  $date = date("Y-m-d");
  if(!$userId && !$this->phpsession->get("userType")){
  redirect(base_url());
}
$options['join'] = true;
$data['users'] = $this->user_model->GetUser($options);
$data['totalprofile'] = $this->candidate_model->GetCandidate(array("count"=>true));
$data['page_title']="User Details";
$this->layout->view("user/manage",$data);
}

候选人型号:

function GetCandidate($options = array()) {  
  if(isset($options['candidateId']))
    $this->db->where('candidateId',$options['candidateId']);
  if(isset($options['userId']))
      $this->db->where('canUserId',$options['userId']);
  if(isset($options['userBranch']))
      $this->db->where('canUserBranch',$options['userBranch']);
  if(isset($options['candidateFname']))
    $this->db->where('candidateFname',$options['candidateFname']);
  if(isset($options['candidateMname']))
    $this->db->where('candidateMname',$options['candidateMname']);
  if(isset($options['candidateLname']))
    $this->db->where('candidateLname',$options['candidateLname']);
  if(isset($options['candClient']))
    $this->db->where('candClient',$options['candClient']);
  if(isset($options['candRequirement']))
    $this->db->where('candRequirement',$options['candRequirement']);

  if(isset($options['activateddate']))
    $this->db->where('activateddate',$options['activateddate']);
  if(isset($options['limit']) && isset($options['offset']))
    $this->db->limit($options['limit'], $options['offset']);
  else if(isset($options['limit']))
    $this->db->limit($options['limit']);
  if(isset($options['join']) ){
    $this->db->select('can . * ,c.clientName,r.requirementName,r.designation,u.userName,u.userMName,u.userLName');
    $this->db->from('candidates as can');
    $this->db->join('clients as c','can.candClient=c.clientId ');
    $this->db->join('requirement as r','r.requirementId=can.candRequirement');
    $this->db->join('users as u','can.canUserId=u.userId ');
    $this->db->order_by("candidateId", "desc");
    $query = $this->db->get();
    if(@$options['candidateId']) return $query->row(0);
    return $query->result();
  }
  $this->db->order_by("candidateId", "desc");
  $query = $this->db->get('candidates');
  if(isset($options['count'])) return $query->num_rows();
  if(@$options['candidateId']) return $query->row(0);
  return $query->result();
}

用户视图:

<th style="width: 5px">UID</th>    
<td><?php echo $totalprofile; ?>      
$this->db->order_by("candidateId", "desc");
   // Add a Where condition to get results only for selected user
   $this->db->where('UserId', XXXXX);
$query = $this->db->get('candidates');

添加where条件只会导致该用户的行

下面的代码可能很有用。我还没有写完整的代码,只是写必需的代码。

控制器中的代码-

function manage(){
  $options['join'] = true;
  $data['users'] = $this->user_model->GetUser($options);
  $data['totalprofile'] = $this->candidate_model->GetCandidate(array("count"=>true));
}

由于您还没有为"GetUser"函数编写代码,我假设它会得到一些数组。让我们将其存储在$options中。

模型中的代码-

function GetCandidate($options){
  $can = array();
  foreach($options as $key => $value){
     $conditions = array('canUserId'=>$value['userId'], 'canUserBranch'=> $value['userBranch']);  // put the array of conditions
     $this->db->select('*');
     $this->db->from('candidates');
     $this->db->where(array('', $conditions)); 
     $can[$value['userId']] = $this->db->get()->num_rows(); // use userId as key here as you will be able to access the count array element in view page using this keys
  }
  return $can;
}

视图中的代码-

foreach($users as $key => $value){
    echo "User Id: "$value['userId'].", Candidate Count per user: ". $totalprofile[$value['userId']];
}

只要使用上面的代码,让我知道你的反馈。