使用连接表进行codeigniter搜索

时间:2016-05-16 01:57:12

标签: php mysql database codeigniter join

这是我的控制器

serializer.acceptableContentTypes = NSSet(array: ["application/xml", "text/xml", "text/plain"]) as Set<NSObject>

这是我的模特

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

这是我的观点

    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();
    }
我需要帮助的人 我是codeigniter的新手。 我需要进行搜索,但搜索结果需要我的数据库中的2个表。 <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> timedate来自user_product_order_iduhd_user_product_orderuser_order_idsender_name和{{1} (此搜索键)来自payment_type

在视图中我可以从我的表uhd_user_order查看它,但我无法查看user_order_referenceuhd_user_ordertime

你能帮助我如何加入2表,这样我就能从搜索中看到最好的结果

2 个答案:

答案 0 :(得分:1)

在您的模型上使用此代码

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

答案 1 :(得分:0)

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

成为

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