代码之家  ›  专栏  ›  技术社区  ›  Diasline

显示数据库中的记录[codeigniter]

  •  0
  • Diasline  · 技术社区  · 7 年前

    我有一张名为 这是由id显示的。此表有许多列,其中一列名为 . 还有第二张桌子 产品 所有产品的名称。假设我有一个带有字段名的产品 在表中 产品 ,他的id 位于person表中。显示要显示的person表行时 大米 而不是他的 身份证件 .

    <?php
    public function person()
    {
        $id= $this->uri->segment(3);
        $query = $this->model_person->get_persons($id);
        $data['all_persons'] =$query;
        $this->load->view('admin/view_person', $data);
    
    }
    ?>
    

    public function get_persons($id)
    {
        $this->db->select("*");
         $this->db->from('person');
         $this->db->where('id',$id);
         $query = $this->db->get();
        return $query->result();
     }
    

    查看用户。php

    <?php foreach($all_persons as $p)
    {
    ?>
        <tr>
            <th >Person name</th>
            <td ><?=$p->name;?></td>
        </tr>
        <tr>
            <th >Person affectation</th>
            <td ><?=$p->affectation;?></td>
        </tr>
        <tr>
            <th>Product name</th>
            <td><?=$p->id_product;?></td>
            <!--i want to dispay product name found in product table-->
        </tr>
    <?php
    }
    ?>
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Shamsur Rahman    7 年前

    要获得这样的输出,需要在模型中运行连接查询。

     public function get_persons($id)
        {
            $this->db->select("*");
             $this->db->from('person');
             $this->db->join('product', 'product.id= person.product_id');
             $this->db->where('person.id',$id);
             $query = $this->db->get();
            return $query->result();
         }
    

    在view_人中。php只需打印product\u名称,而不是product\u id

    <tr>
            <th>Product name</th>
            <td><?=$p->product_name;?></td>
            <!--i want to dispay product name found in product table-->
        </tr>