gpt4 book ai didi

php - 在使用 foreach() 之后,相同的数据库表值一次又一次地重复

转载 作者:行者123 更新时间:2023-11-29 03:20:15 26 4
gpt4 key购买 nike

我有一个模型,我在其中连接了两个表并选择了一些表值,然后我将它传递到我的 Controller ,然后从 Controller 将它传递到 View 页面。在 View 页面中,我使用 foreach() 来获取值,然后显示它。但问题是同一个表行一次又一次地重复。

模型

public function image_list()
{
$this->db->select('interior_testing.type_id,interior_testing.profile_pic, interior_testing.type, interior_testing.location, interior_testing.name, interior_image_testing.image_path');
$this->db->from('interior_testing');
$q=$this->db->join('interior_image_testing', 'interior_image_testing.type_id = interior_testing.type_id');
$this->db->where('interior_testing.category_id', 3);
$q = $this->db->get();
return $q->result();
}

Controller

public function index()
{
$this->load->model("Interior_listing_model","interior");
$data['particles'] = $this->interior->image_list();

// Load Interior Listing View
$this->load->view("interior/interior",$data);
}

查看

<?php foreach($particles as $particle): ?> 
<div class="col-lg-4 col-md-4 col-sm-4 col-6 workimg">
<img src="assets/img/<?= $particle->image_path ?> " width="100%">
</div>
<?php endforeach; ?>

最佳答案

像下面这样清理你的查询,如果这不起作用你可能需要在你的查询中添加一个 group_by id

public function image_list()
{
$select = "it.type_id, it.profile_pic, it.type, it.location, it.name, im.image_path";

return $this->db->select($select)
//equivalent to name as abr
->join('interior_image_testing im', 'im.type_id = it.type_id');
->where('it.category_id', 3);
//equivalent to name as abr
->get('interior_testing it')->result();

}

关于php - 在使用 foreach() 之后,相同的数据库表值一次又一次地重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46427669/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com