【问题标题】:codeigniter search with join table使用连接表进行 codeigniter 搜索
【发布时间】:2016-05-16 01:57:12
【问题描述】:

这是我的控制器

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

这是我的看法

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

伙计们,我在这里需要帮助 我是codeigniter的新手。 我需要进行搜索,但搜索结果需要我的数据库中的 2 个表。 timedateuser_product_order_id 来自 uhd_user_product_orderuser_order_idsender_namepayment_typeuser_order_reference(此搜索键)来自 uhd_user_order

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

你能帮我如何加入 2 表,以便我可以从搜索中看到最佳结果

【问题讨论】:

  • 没关系,我明白了,我已经修好了
  • 你能发表你的答案吗?

标签: php mysql database codeigniter join


【解决方案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();
}

【讨论】:

    【解决方案2】:
    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();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-18
      • 1970-01-01
      • 2013-05-26
      • 2021-08-26
      • 1970-01-01
      • 2013-11-14
      相关资源
      最近更新 更多