在codeigniter中搜索多个表数据

问题描述 投票:0回答:1

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

这是我要搜索的过程。Search view

这是数据库表...

Db table

查看

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

Controller

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;
}
codeigniter search
1个回答
0
投票

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

<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 parametar 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;
}
© www.soinside.com 2019 - 2024. All rights reserved.