如何在Codeigniter中计算结果?

时间:2015-10-05 09:49:23

标签: php codeigniter

我知道codeigniter的问题:

$这 - > DB-> count_all('表名&#39);

并且我可以使用where where和fetch过滤结果。但在我的情况下,我不知道如何在我的代码中实现:

我想用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>&nbsp Resellers&nbsp<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') ?>

现在我有另一个名为users的表,并且我有一个名为key的字段,其中存储了代理商的密钥,因此我想要计算所有具有key =&#34; some-key&#34;的用户。

这是必需的,因为在转销商的列表中,我想显示哪个转销商有多少用户。

我这样做了:

$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;
            }
    }

1 个答案:

答案 0 :(得分:2)

我会尽力去试试。

当我需要计算在哪里时,我使用return $query->num_rows();

模型功能

class Reseller_m extends CI_Model {

  public function count($key) {
    $this->db->where('key', $key);
    $query = $this->db->get($this->db->dbprefix . 'users');

    return $query->num_rows();
  }

 public function get() {
     // Not sure if you have any thing else here.
    $query = $this->db->get($this->db->dbprefix . 'users');

    if ($query->num_rows() > 0) {
          return $query->results_array();
          // return $query->row_array();
    } else {
          return FALSE;
    }
  }

}

控制器

当您需要在表格中动态计数时,您可以调用模型函数,如下所示。在数组中,我发现像下面的数据数组更容易。

public function index() {

    $usertype = $this->session->userdata('usertype');

    if ($usertype === "admin") {

    $this->load->model('reseller_m');

    $results = $this->reseller_m->get();

    // Removed $this->data and just have $data see what works. 

    $data['resellers'] = array();

    if ($results) {
      foreach($results as $result) {
       $data['resellers'][] = array(
          'id' => $result['id'],
          'sip_username' => $result['sip_username'],
          'sip_password' => $result['sip_password'],
          'key' =>  $result['key'],
          'total' => $this->reseller_m->count($result['key']),
          'allocation_block' => $result['allocation_block'],
          'name' => $result['name'],
          'email' => $result['email'],
          'phone' => $result['phone'],
          'user_num' => $result['user_num'],
          'address' => $result['address'],
          'balance' => $result['balance'],
          'country_code' => $result['country_code'],
          'status' => ($result['status'] ? Enabled : Disabled)
       );
      }
    }

      $data['subview'] = 'admin/reseller/index';
      $this->load->view('admin/_layout_main', $data);

    } else {

       $this->load->view('permission');

    }
}

查看示例

<td  contenteditable="true" onClick="edit_Reseller(this)" onBlur="save_reseller(this,'total','<?php echo $reseller['id'];?>')" ><?php echo $reseller['total'];?></td>

表示例

<tbody>

<?php if ($resellers) {?>

<?php foreach ($resellers as $reseller) {?>
<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>

//  Added here Total Here
<td  contenteditable="true" onClick="edit_Reseller(this)" onBlur="save_reseller(this,'total','<?php echo $reseller['id'];?>')" ><?php echo $reseller['total'];?></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_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>

<?php } else { ?>

<p>No Results</p>

<?php }?>

</tbody>