MD Aminul
MD Aminul

Reputation: 53

Search multiple table data in codeigniter

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.

This is the process I want to search.. Search view

This is DB table...

Db table

View

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

Model

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

Upvotes: 1

Views: 899

Answers (1)

failedCoder
failedCoder

Reputation: 1424

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

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

Upvotes: 1

Related Questions