【发布时间】:2014-02-22 12:23:17
【问题描述】:
我是 Codeigniter 的新手,过去三天我一直在做这件事,但我已经走到了死胡同。我正在运行以下查询以从表中获取夹具详细信息。
我有两个表 team 和 fixture,它们有一个多对多的关系,导致一个名为 team_has _fixture 的 Join 表
下面是我的表格
team
team_id
team_name
team_logo
fixture
fixture_id
fixture_text
fixture_comp
fixture_type
fixture_level
fixture_date
team_has_fixture
team_team_id
team_fixture_id
team_team_id2
这是我的模型中的代码示例
型号
$results = array();
$this->db->select('*');
$this->db->from('team_has_fixture as tf');
$this->db->join('team as t1', 'tf.team_team_id = t1.team_id');
$this->db->join('team as t2', 'tf.team_team_id2 = t2.team_id');
$this->db->join('fixture', 'tf.fixture_fixture_id = fixture.fixture_id');
$query = $this->db->get();
if($query->num_rows() > 0)
{
$results = $query->result();
}
return $results;
我正在尝试创建一个夹具,其中包含两个团队的徽标,但是当我运行此代码时,只显示一个徽标。
正确检索了所有夹具详细信息,但仅检索了一个团队徽标,并且我需要两个徽标。
我的模型代码中用于检索数据的以下两行相互覆盖,导致只有第二行工作。
$this->db->join('team as t1', 'tf.team_team_id = t1.team_id');
$this->db->join('team as t2', 'tf.team_team_id2 = t2.team_id');
如果我注释掉第二行,第一行会带回赛程和第一支球队标志的所有细节。如果我不注释掉第二行,查询会带回所有夹具详细信息和第二支球队的标志——它似乎覆盖了上面的行
我已经在此处和其他资源中搜索了解决方案,我真的坚持这一点,希望能提供任何帮助。
我也不确定如何要求在我的视图中显示第二个徽标。
在我的视图中显示结果的代码是
查看
<?php
if (is_array($results))
{
if( !empty($results) )
{
foreach($results as $row)
{
echo '<div class="col-sm-12 col-md-12">';
echo '<div class="thumbnail">';
echo '<tr>';
echo '<h4>';
echo '<td>'.$row->fixture_type.'</td>'."</br></br>";
echo '<td>'.'<img src="'."$row->team_logo".'"> '.$row->fixture_text.' <img src="'."$row->team_logo".'"> '.'</td>'."</br></br>";
echo '<td>'.$row->fixture_level.'</td>'."</br></br>";
echo '<td>'.$row->fixture_comp.'</td>'."</br>";
echo '</h4>';
echo '<td>'.$row->fixture_date.'</td>'."</br></br>";
echo '</tr>';
echo '</div>';
echo '</div>';
}
}
使用 echo $this->db->last_query();显示我的最后一个查询,我得到以下内容
SELECT * FROM (`team_has_fixture` as tf) JOIN `team` as t1 ON `t1`.`team_id` = `tf`.`team_team_id` JOIN `fixture` ON `tf`.`fixture_fixture_id` = `fixture`.`fixture_id`
使用 var_dump($query->result());显示我的查询结果我得到以下内容
[0]=> object(stdClass)#22 (15)
{
["team_team_id"]=> string(1) "5"
["fixture_fixture_id"]=> string(2) "62"
["team_team_id2"]=> string(2) "15"
["team_id"]=> string(2) "15"
["team_name"]=> string(8) "Kilkenny"
["team_logo"]=> string(25) "../../crests/kilkenny.png"
["fixture_id"]=> string(2) "62"
["fixture_text"]=> string(16) "Clare V Kilkenny"
["fixture_type"]=> string(7) "Hurling"
["fixture_comp"]=> string(36) "Allianz Hurling League 2014 Roinn 1A"
["fixture_round"]=> string(7) "Round 1"
["fixture_level"]=> string(8) "Roinn 1A"
["fixture_time"]=> string(3) "2pm"
["fixture_date"]=> string(26) "Sunday February 16th, 2014"
["fixture_venue_id"]=> string(1) "5"
}
如您所见,我得到了其中一个队徽,但没有得到另一个。这是我试图解决的问题。非常感谢任何帮助。
【问题讨论】:
-
在
$query = $this->db->get();之后添加echo $this->db->last_query();并在您的问题中添加查询。
标签: php sql codeigniter codeigniter-2 inner-join