简体   繁体   中英

codeigniter search with join table

this is my controller

function search_keyword()
{
    $cari    =   $this->input->GET('cari');
    $data['dapat']    =   $this->order_test_m->search($cari);
    $this->load->view('admin/a',$data);
}

this is my model

    function search($cari)
    {
        $this->db->from("uhd_user_order AS t1");
        $this->db->join("uhd_user_product_order AS t2", "t1.user_order_id = t2.user_order_id");
        $this->db->where('user_order_reference',$cari);
        $query = $this->db->get('uhd_user_order');
        return $query->result();
    }

this is my view

                    <tr>
                        <th>No.</th>
                        <th>Customer Name</th>
                        <th>Product Code</th>
                        <th>Payment Type</th>
                        <th>Delivery Date</th>
                        <th>Total Price</th>
                        <th style="text-align: center;padding-right: 15px;">Action</th>
                    </tr>
            <?php if($dapat !== NULL) { ?>
                <?php foreach ($dapat as $row => $test) {

                    ?>
                    <tr>
                        <td><?= $test->user_order_id?></td>
                        <td><?= $test->sender_name?></td>
                        <td><?= $test->user_product_order_id?></td>
                        <td><?= $test->payment_type ?></td>
                        <td><?= $test->time.$test->date ?></td>
                        <td><?= $test->delivery_price?></td>
                    </tr>
                <?php }
            }else{
                echo "<td colspan='3'>no customer for the result!</td>";

            }
            ?>
        </table>

guys i need help here im new in codeigniter. i need to make a search but the search result are needing 2 table from my database. time , date and user_product_order_id are from the uhd_user_product_order , and user_order_id , sender_name , payment_type , and user_order_reference (this the search key) are from uhd_user_order

in the view i can view it from my table uhd_user_order, but i cant view the time , date and the user_product_order_id

can you help me how to join the 2 table so i can see the best result from the search

Use this code on your model

public function search($cari){

      $this->db->select('*');
      $this->db->from("uhd_user_order AS t1");
      $this->db->join("uhd_user_product_order AS t2", "t2.user_order_id = t1.user_order_id");  # confirm user_order_id in both table
      $this->db->where('user_order_reference',$cari);
      $query = $this->db->get();
      return $query->result();
}
function search($cari)
{
    $this->db->from("uhd_user_order AS t1");
    $this->db->join("uhd_user_product_order AS t2", "t1.user_order_id = t2.user_order_id");
    $this->db->where('user_order_reference',$cari);
    $query = $this->db->get('uhd_user_order');
    return $query->result();
}

become

    function search($cari)
    {
        $this->db->join("uhd_user_product_order" , "uhd_user_order.user_order_id = uhd_user_product_order.user_order_id");
        $this->db->where('user_order_reference',$cari);
        $query = $this->db->get('uhd_user_order');
        return $query->result();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM