首页 > 解决方案 > 在codeigniter中搜索多个表数据

问题描述

您在搜索邮政编码时选择的表会从所选数据库表中引入数据。但我不知道该怎么做。请帮我。我的数据库 tblcaregiver 中有 3 个表。tblfamily,tblprovider。

这是我要搜索的过程.. 搜索视图

这是数据库表...

数据库表

看法

<div class="serc-title">Search User</div>
<div>
    <div class="input-group mb-4">
        <form action="<?php echo site_url('provider/dashboard/search_keyword');?>" method="post">
            <input type="text" name = "keyword" required="required" value="<?php if(isset($searching_data)){echo $searching_data; } ?>" />
            <input type="submit" value = "Search" />
        </form>
    </div>
</div>

<h5 class="header-title mb-0">Potential Clients</h5>
<div class="table-responsive">
    <?php if(isset($zipcode_serching_results)){ ?>
    <?php if(!empty($zipcode_serching_results)){ ?>
        <h5 class="header-title mb-0">Potential Caregivers</h5>
        <div class="table-responsive">
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <td>Telephone</td>
                        <td>Zipcode</td>
                        <td>Email</td>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach($zipcode_serching_results as $row){ ?>
                        <tr>
                            <td><?php echo $row->tele ?></td>
                            <td><?php echo $row->zipcode ?></td>
                            <td><?php echo $row->emailid ?></td>
                        </tr>
                    <?php } ?>
                </tbody>
            </table>
        </div>
    <?php }else{ ?>
    <div>
        <h4 style="color: #999">Zipcode not found</h4>
    </div>
    <?php } } ?>
</div>

控制器

public function search_keyword() {
    $keyword = $this->input->post('keyword');
    $data['zipcode_serching_results'] = $this->Provider_Profile_Model->search($keyword);
    $data['searching_data'] = $keyword;

    $userid = $this->session->userdata('uid');
    $data['profile'] = $this->Provider_Profile_Model->getprofile($userid);
    $this->load->view('provider/dashboard', $data);
}

模型

public function search($keyword) {
  $this->db->like('zipcode', $keyword);
  $query = $this->db->get('tblfamily')->result();
  return $query;
}

标签: codeignitersearch

解决方案


首先,您必须在表单中添加带有表格选项的选择标签

<select id="tables" name="tables" form="ID-OF-YOUR-FORM-HERE">
  <option value="tblcaregiver">tblcaregiver</option>
  <option value="tblfamily">tblfamily</option>
  <option value="mercedes">Mercedes</option>
  <option value="tblprovider">tblprovider</option>
</select>

接下来,您必须在控制器中获取选择数据,并将其传递给您的模型

public function search_keyword() {
    $keyword = $this->input->post('keyword');
    // fetch selected table 
    $table = $this->input->post('tables');
    $data['zipcode_serching_results'] = $this->Provider_Profile_Model->search($keyword, $table); // 2nd parameter added
    $data['searching_data'] = $keyword;

    $userid = $this->session->userdata('uid');
    $data['profile'] = $this->Provider_Profile_Model->getprofile($userid);
    $this->load->view('provider/dashboard', $data);
}

最后,更新您的模型,并动态选择表

public function search($keyword, $table) {
  $this->db->like('zipcode', $keyword);
  $query = $this->db->get($table)->result();
  return $query;
}

推荐阅读