我在 MySQL 中有 2 个表:
预期的表匹配:
id | idA | idB
1 | 21 | 1
2 | 21 | 2
3 | 22 | 3
4 | 8 | 2
5 | 9 | 21
6 | 10 | 7
表数据:
id | projectId | name
1 | 2 | Chicken
2 | 2 | Pork
3 | 2 | Fish
...
21 | 11 | Potato
22 | 11 | Carrot
23 | 11 | Chili
基于表匹配,例如,我想在 idA 仅等于 projectId 11 的行中获取 idA 和 idB 的名称。
我在 CodeIgniter 中构建了它,我已经成功foreach idA name
,但仍然无法获得 idB 名称。
我希望我的观点是这样的(以 projectId = 11 为例):
name A | nameB
potato | chicken
potato | Pork
carrot | Fish
但是,现在我得到了
name A | nameB
potato | chicken
potato |
carrot |
这是我的代码
模型:
function get_projectA() {
$project_id = $this->uri->segment(4, 0);
$this->db->select('*');
$this->db->from('matches');
$this->db->join('data', 'matches.idA = data.id');
$this->db->where('projectID', $project_id);
$query = $this->db->get();
return $query->result();
}
function get_projectB() {
$project_id = $this->uri->segment(4, 0);
$this->db->select('idB');
$this->db->from('matches');
$this->db->join('image', 'matches.idA = data.id');
$this->db->where('projectID', $project_id);
$query = $this->db->get();
$a = $query->result_array();
foreach ($query->result_array() as $row)
{
$c = $row['idB'];
$query2=$this->db->query("SELECT * FROM data where id='$c'");
$aaa = $query2->result_array();
echo '<pre>';
print_r($aaa);
}
}
看法:
<tbody id="test">
<?php
foreach($get_projectA as $projectA) {
foreach($get_projectB as $projectB) {
?>
<tr>
<td>
<p><?php echo $projectA->name; ?></p>
</td>
<td>
<p><?php echo $projectB['name']; ?></p>
</td>
<td>
<p>3</p>
</td>
<td>
<p>4</p>
</td>
</tr>
<?php } } ?>
</tbody>