codeigniter - 在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;
}
解决方案
首先,您必须在表单中添加带有表格选项的选择标签
<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;
}
推荐阅读
- sql - SQL Case When 语句用于计算多个表上的行数
- go - 如何在 Visual Studio Code 中为 Golang 启用竞赛检测器?
- jenkins - Jenkins:外部节点无法在 Kubernetes 上注册到 master
- sql - SQL:组合两个没有笛卡尔积的表
- angular - 我应该在应用启动时立即加载所有内容吗?(最佳实践)
- asp.net-mvc - 如何在 asp.net mvc 实体框架中创建视图模型、控制器和视图以加入多对多表
- sql - 是否可以使用标准构建器执行一对多外键联接?
- c++ - 通过柯南安装仅标头包时出错
- mysql - sequelize 中未处理的拒绝 SequelizeEagerLoadingError - Nodejs
- asp.net-mvc - MVC:从数据库中获取值列表