【问题标题】:How to Use a Filter in Codeigniter View after Joining 2 Tables加入 2 个表后如何在 Codeigniter 视图中使用过滤器
【发布时间】:2019-01-31 05:26:38
【问题描述】:

我是 Codeigniter 的新手,并坚持在我的视图中显示过滤器的基本要求。我有一个包含成员详细信息的表(公司)和另一个名为“帐户”的表。我在那里存储了每个成员的付款详细信息。所以问题是,我必须在一个视图中获取所有成员和错过付款的成员的详细信息。我已加入 2 个表以将成员 ID (bh_id) 与帐户表匹配,因此当帐户中不存在成员 ID 时,我可以获取错过的成员详细信息。我有两个输出,但是我不想将其显示为 2 个视图,而是想通过使用下拉过滤器将其显示在单个视图中,例如 1) 所有成员 2) 错过的成员。

请看附件截图:

表公司:

表帐户:

提议的观点:

现在让我们看看我目前的代码。 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(控制器)

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 (查看)

    <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>

请帮助我实现这一目标。感谢您的帮助:)

【问题讨论】:

  • 您可以在视图中使用 2 个表,默认设置为 1 个表处于活动状态,并通过单击下拉菜单启用其他表

标签: php mysql codeigniter filter


【解决方案1】:

有两种方法可以做到这一点
首先使用 ajax
第二次隐藏和显示

因为你是 Codeigniter 的新手,我给你解释第二个
首先要做两个具有各自ID的表格设计
使用

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

而不是

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

对于错过的会员

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

现在从这里粘贴下拉代码:

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

现在从这里粘贴 javascript 函数:
将其粘贴到视图底部

<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>

【讨论】:

  • 感谢赫曼特的回复。我为暂时添加了 2 个不同的视图。刚刚尝试了您的解决方案,它太棒了。对我来说工作得很好。欣赏它。谢谢!
猜你喜欢
  • 2022-01-23
  • 1970-01-01
  • 2018-05-20
  • 1970-01-01
  • 2023-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-24
相关资源
最近更新 更多