简体   繁体   English

带有联接表的codeigniter搜索

[英]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. 伙计们,我在Codeigniter中需要新的帮助。 i need to make a search but the search result are needing 2 table from my database. 我需要进行搜索,但是搜索结果需要从我的数据库中获取2张表。 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 timedateuser_product_order_id来自uhd_user_product_orderuser_order_idsender_namepayment_typeuser_order_reference (此搜索关键字)来自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 在视图中,我可以从我的表uhd_user_order中查看它,但是我无法查看timedateuser_product_order_id

can you help me how to join the 2 table so i can see the best result from the search 您能帮我如何加入2表吗,以便我可以从搜索中看到最佳结果

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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