dongmi4809 2019-01-31 05:26
浏览 116
已采纳

如何在加入2个表后在Codeigniter视图中使用过滤器

I am a fresher in Codeigniter and stuck with a basic requirement to display a filter in my view. I have a table (companies) with member details and another table named "accounts". There I have stored Payment details of each member. So The Question is, I have to fetch details of All members and the members who missed payment into one view. I have joined 2 tables to match the member ID (bh_id) with accounts table, So I could get the missed member details when the member ID is not present in accounts. I have both output with me But Instead of displaying this as 2 views I want to display it in a single view by using a dropdown filter Like 1) All Members 2) Missed Members.

Please See the attached screenshots:

TABLE-companies :

enter image description here

TABLE-accounts :

enter image description here

Proposed view :

enter image description here

Now let see my code what I have so far. Company_model.php

//Joining accounts to get missed member details in view
public function missed_members(){
    $this->db->select("*");
    $this->db->from("companies");
    $this->db->join('accounts','accounts.bh_id = companies.bh_id','left');
    $this->db->where('accounts.bh_id IS NULL');
    $this->db->group_by('companies.bh_id');
    $query = $this->db->get();
    return $query->result();
}

Company.php (Controller)

public function index()
{

    //All Companies
    $data['company_data']= $this->Company_model->companies("companies");
    // Missed Members
    $data['missedmembers'] = $this->Company_model->missed_members();
    $data['tab'] = 'tab1';
    $data["page"] = "companies/company";
    $this->load->view('dashboard', $data);
}

company.php (VIEW)

    <section id="main-content">
  <section class="wrapper">
  <!-- page start-->
    <div class="row">
      <div class="col-lg-12">
      <!--breadcrumbs start -->
          <ul class="breadcrumb">
            <li><a href="#"><i class="fa fa-building-o"></i> Accounts</a></li>
            <li><a href="#">Member</a></li>
          </ul>
      <!--breadcrumbs end -->
          <section class="panel">
            <header class="panel-heading"> 
                  <a href="<?= base_url("company/add_company") ?>">
                    <button class="btn btn-primary btn-sm">
                      <i class="fa fa-plus-circle" aria-hidden="true"></i> Add New Member
                    </button>
                  </a> 

               <span class="tools pull-right">
                  <a href="javascript:;" class="fa fa-chevron-down"></a>
                  <a href="javascript:;" class="fa fa-times"></a>
               </span>
               <select>
                 <option>All Members</option>
                 <option>Missed Members</option>
               </select>
            </header>
            <div class="panel-body">
              <section>
                <div class="adv-table">
                <table class="display table table-bordered table-striped" id="dynamic-table">
                  <thead class="cf">
                  <tr>
                      <th>Book No</th>
                      <th>Name</th>
                      <th>Phone</th>
                      <th>Area</th>
                      <th>Staff Name</th>
                      <th>Book Details</th>
                      <th>Edit</th>
                      <th>Delete</th>
                  </tr>
                  </thead>
                  <tbody>
                  <?php 
                  foreach ($company_data as $value)
                    {
                  ?> 
                   <tr>
                     <td>
                        <?= $value->bh_m_id; ?>
                     </td>  
                     <td>
                        <?= $value->bh_name; ?>
                     </td> 
                      <td>
                        <?= $value->bh_phone; ?>
                     </td>
                     <td>
                        <?= $value->bh_area; ?>
                     </td>
                     <td>
                        <?= $value->ca_name; ?>
                     </td>
                     <td>
                        <a href="<?= base_url("accounts/index")?>/<?= $value->bh_id ?>">
                          <button class="btn btn-primary btn-xs">View Book</button>
                        </a>      
                     </td>         
                     <td>

                        <a href="<?= base_url("company/edit") ?>/<?= $value->bh_id ?>">
                           <button class="btn btn-success btn-xs"><span class="glyphicon glyphicon-edit"> </span> Edit</button>
                        </a> 
                     </td>
                     <td>
                        <a href="<?= base_url("company/delete") ?>/<?= $value->bh_id ?>"
                                onclick="return confirm('Do You Really Want To Delete This Record')">
                            <button class="btn btn-danger btn-xs"><span class="glyphicon glyphicon-trash"> </span> Delete</button> 
                        </a>
                     </td>
                   </tr>
                   <?php 
                    } 
                   ?> 
                </tbody>
              </table>
            </div>
            </section>
          </div>
        </section>
      </div>
    </div>
  </section>
</section>

Please help me to achieve this. Thanks for the Help :)

  • 写回答

1条回答 默认 最新

  • donglinxia1541 2019-02-12 10:59
    关注

    There are two ways to do that
    First with ajax
    Second hide and show

    Because you are a newbie in Codeigniter, I will explain you second one
    first thing make two table design with respective ids
    use

    <table style='display:block;' class="table table-bordered table-striped" id="allMember_table">
    respective code like you wrote in your view now
    </table>
    

    instead of

    <table class="display table table-bordered table-striped" id="dynamic-table">
    

    and for missed Members

    <table style='display:none;' class="table table-bordered table-striped" id="missedMember_table">
    respective code like you wrote in your view now
    </table>
    

    Now paste dropdown code from here:

    <select onchange="change_view(this.value);">
       <option value="all">All Members</option>
       <option value="missed">Missed Members</option>
    </select>
    

    Now paste javascript function from here:
    paste it at bottom of the view

    <script type="text/javascript">
    
    function change_view(value){
      if(value == "all"){
         document.getElementById("allMember_table").style.display = "block";
         document.getElementById("missedMember_table").style.display = "none";
      }else{
         document.getElementById("missedMember_table").style.display = "block";
         document.getElementById("allMember_table").style.display = "none";
      }
    }
    
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献