0
我知道codeigniter的:如何統計Codeigniter中的結果?
$ this-> db-> count_all('table_name');
而且我可以使用條件和獲取過濾結果。但在我的情況下,我不知道如何在我的代碼實現在這裏:
我想計算所有經銷商與key =「some-value」。
這是我的看法,以顯示所有代理商:
<?php $this->load->view('admin/components/page_head') ?>
<section class="container-fluid">
<ul class="nav nav-pills"> <li class="active"> <h2><i class="glyphicon glyphicon-user"></i>  Resellers <span class="badge"><?php echo $this->db->count_all('reseller');?></span></h2></li></ul>
<h3> <?php echo anchor('admin/reseller/add_reseller', '<i class="glyphicon glyphicon-plus"></i> Add a Reseller'); ?></h3>
<table class="table table-primary">
<thead>
<tr>
<th>SIP Userid</th>
<th>SIP Password</th>
<th>Unique ID</th>
<th>Allocation Block</th>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Intial User Required</th>
<th>Location</th>
<th>Balance</th>
<th>Country</th>
<th>Country Code</th>
<th>Status</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php if(count($resellers)): foreach($resellers as $reseller): ?>
<tr>
<td contenteditable="true" onClick="edit_Reseller(this)" onBlur="save_reseller(this,'sip_username','<?php echo $reseller->id; ?>')" ><?php echo $reseller->sip_username; ?></td>
<td contenteditable="true" onClick="edit_Reseller(this)" onBlur="save_reseller(this,'sip_password','<?php echo $reseller->id; ?>')" ><?php echo $reseller->sip_password; ?></td>
<td contenteditable="true" onClick="edit_Reseller(this)" onBlur="save_reseller(this,'key','<?php echo $reseller->id; ?>')" ><?php echo $reseller->key; ?></td>
<td contenteditable="true" onClick="edit_Reseller(this)" onBlur="save_reseller(this,'allocation_block','<?php echo $reseller->id; ?>')" ><?php echo $reseller->allocation_block; ?></td>
<td contenteditable="true" onClick="edit_Reseller(this)" onBlur="save_reseller(this,'name','<?php echo $reseller->id; ?>')" ><?php echo $reseller->name; ?></td>
<td contenteditable="true" onClick="edit_Reseller(this)" onBlur="save_reseller(this,'email','<?php echo $reseller->id; ?>')" ><?php echo $reseller->email; ?></td>
<td contenteditable="true" onClick="edit_Reseller(this)" onBlur="save_reseller(this,'phone','<?php echo $reseller->id; ?>')" ><?php echo $reseller->phone; ?></td>
<td contenteditable="true" onClick="edit_Reseller(this)" onBlur="save_reseller(this,'user_num','<?php echo $reseller->id; ?>')" ><?php echo $reseller->user_num; ?></td>
<td contenteditable="true" onClick="edit_Reseller(this)" onBlur="save_reseller(this,'address','<?php echo $reseller->id; ?>')" > <?php echo $reseller->address; ?></td>
<td contenteditable="true" onClick="edit_Reseller(this)" onBlur="save_reseller(this,'balance','<?php echo $reseller->id; ?>')" ><?php echo $reseller->balance; ?></td>
<td contenteditable="true" onClick="edit_Reseller(this)" onBlur="save_reseller(this,'country','<?php echo $reseller->id; ?>')" ><?php echo $reseller->country; ?></td>
<td contenteditable="true" onClick="edit_Reseller(this)" onBlur="save_reseller(this,'country_code','<?php echo $reseller->id; ?>')" ><?php echo $reseller->country_code; ?></td>
<td contenteditable="true" onClick="edit_Reseller(this)" onBlur="save_reseller(this,'status','<?php echo $reseller->id; ?>')" ><?php echo $reseller->status; ?></td>
<td><?php echo btn_edit('admin/reseller/edit/' . $reseller->id); ?></td>
<td><?php echo btn_delete('admin/reseller/delete/' . $reseller->id); ?></td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="3">We could not find any users.</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</section>
<?php $this->load->view('admin/components/page_tail') ?>
現在我有另一個表稱爲用戶,並在我已經在經銷商的密鑰存儲在所謂的關鍵領域,所以我想指望所有具有密鑰=「某個密鑰」的用戶。
這是需要的,因爲在經銷商的名單我想顯示哪些經銷商有多少用戶。
我這樣做:
$key="Rajan-92-1-100-1";
$this->load->model('reseller_m');
$this->load->view('admin/reseller/count');
$this->db->where('key',$key);
$this->db->from('users');
$count = $this->db->count_all_results();
echo $count;
,但我在這裏提供的關鍵靜態,我想爲每個經銷商
更新動態計數這樣:
控制器
public function index()
{
$usertype=$this->session->userdata('usertype');
if($usertype ==="admin")
{
// Fetch all users
$this->data['resellers'] = $this->reseller_m->get();
$data['resellers'] = array();
if ($results) {
foreach($results as $result) {
$data['resellers'][] = array(
'sip_username' => $result['sip_username'],
'sip_password' => $result['sip_password'],
'key' => $result['key'],
'total' => $this->reseller_m->count($result['key']);
// Add your other data here.
);
}
}
// Load view
$this->data['subview'] = 'admin/reseller/index';
$this->load->view('admin/_layout_main', $this->data);
}
else
{
$this->load->view('permission');
}
}
觀:
<td> <?php echo $reseller['total'];?></td>
的型號:
public function count($key)
{
$this->db->where('key', $key);
$query = $this->db->get($this->db->dbprefix . 'users');
return $query->num_rows();
}
public function get()
{
$query = $this->db->get($this->db->dbprefix . 'users');
if ($query->num_rows() > 0)
{
return $query->results_array();
}
else
{
return FALSE;
}
}
在控制器你爲什麼要攝取鍵靜態 – Rajan
我已經做到了在陣列的動態控制器上 – user4419336
所以,在我看來,如圖所示在問題代碼中,我應該使用哪行代碼來獲取計數? – Rajan