Php 显示数据库中的记录[codeigniter]
我有一个名为person的表,它按id显示。该表有许多列,其中一列名为product\u id。第二个表product显示所有产品的名称。假设我在表product中有一个产品的字段名为rice,他的id4位于person表中。当我显示person表行时,我想显示rice,而不是他的idPhp 显示数据库中的记录[codeigniter],php,mysql,codeigniter,Php,Mysql,Codeigniter,我有一个名为person的表,它按id显示。该表有许多列,其中一列名为product\u id。第二个表product显示所有产品的名称。假设我在表product中有一个产品的字段名为rice,他的id4位于person表中。当我显示person表行时,我想显示rice,而不是他的id <?php public function person() { $id= $this->uri->segment(3); $query = $this->model_pe
<?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);
}
?>
view_person.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
}
?>
人名
做作
产品名称
要获得这样的输出,您需要在模型中运行连接查询
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_person.php中,只需打印产品名称而不是产品id
<tr>
<th>Product name</th>
<td><?=$p->product_name;?></td>
<!--i want to dispay product name found in product table-->
</tr>
产品名称
向我们展示了数据库关系和模型代码的清晰图像/解释。我刚刚添加了获取person表行的模型。现在我不能亲自展示产品名称为什么我没有考虑加入,没有办法!非常感谢,很有效
<tr>
<th>Product name</th>
<td><?=$p->product_name;?></td>
<!--i want to dispay product name found in product table-->
</tr>