在codeigniter中搜索多个表数据 [英] Search multiple table data in codeigniter

查看:44
本文介绍了在codeigniter中搜索多个表数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The table you select when searching the zipcode brings in data from the selected DB table. But I do not know how to do it. Please help me. I have 3 table in my database tblcaregiver. 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;
}

推荐答案

首先,您必须在表单中添加带有表选项的select标记

First you have to add select tag with table options to your form

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

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

Next, you have to fetch the select data in your controller, and pass it on to your model

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

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

Finally, update your model, and dynamically select table

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

这篇关于在codeigniter中搜索多个表数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆