0

我的视图文件中的数组有问题。我正在尝试做的是让它显示每个标题的标题列表和竞争者。截至目前,当我有应该是的值时,它显示 Vacant 作为每个标题的冠军和每个标题的每个竞争者的 TBD工作但不是。

如果 ChampionID 的值在表中为 0,则它应该显示 Vacant,否则它应该获取冠军名称并显示它。对于每个竞争者,如果它在数据库中为 0,那么它应该显示 TBD,如果不是,那么它应该显示他们的名字。

这是查询:

/**
 * Get title champions
 *
 * @return  object/NULL
 */
function getTitlesChampions()
{       
    $this->db->select('titlesList.titleName');
    $this->db->select('rosterList.rosterName AS champion');
    $this->db->select('con1.rosterName AS contender1');
    $this->db->select('con2.rosterName AS contender2');
    $this->db->select('con3.rosterName AS contender3');
    $this->db->from('titlesChampions');
    $this->db->join('titlesList', 'titlesList.id = titlesChampions.titlesListID');
    $this->db->join('rosterList', 'rosterList.id = titlesChampions.championID', 'left');
    $this->db->join('rosterList AS con1', 'con1.id = titlesChampions.contender1ID', 'left');
    $this->db->join('rosterList AS con2', 'con2.id = titlesChampions.contender2ID', 'left');
    $this->db->join('rosterList AS con3', 'con3.id = titlesChampions.contender3ID', 'left');
    $query = $this->db->get();
    if ($query->num_rows() > 0) 
    {
        return $query->result();
    }
    return null;
}

titleChampions 表中的值是这样的:

id - 1  titlesListID - 1  championID - 1  contender1ID - 1   contender2ID - 1  contender3ID - 1
id - 2  titlesListID - 2  championID - 1  contender1ID - 0   contender2ID - 0  contender3ID - 0
id - 3  titlesListID - 3  championID - 1  contender1ID - 0   contender2ID - 0  contender3ID - 0

我在视图文件中运行数组的 print_r ,这是打印出来的:

Array
(
[0] => stdClass Object
    (
        [titleName] => Undisputed Heavyweight Title
        [champion] => Kid Wonder
        [contender1] => Kid Wonder
        [contender2] => Kid Wonder
        [contender3] => Kid Wonder
    )

[1] => stdClass Object
    (
        [titleName] => Outlaw Title
        [champion] => Kid Wonder
        [contender1] => 
        [contender2] => 
        [contender3] => 
    )

[2] => stdClass Object
    (
        [titleName] => Tag Team Titles
        [champion] => Kid Wonder
        [contender1] => 
        [contender2] => 
        [contender3] => 
    )

)


    <?php if (!is_null($titlesChampionsList)) { foreach ($titlesChampionsList AS $champion) { 
                    echo '<tr>'; 
                    echo '<td>'.$champion->id.'</td>'; 
                    echo '<td>'.$champion->titleName.'</td>'; 
                    echo '<td>';
                    if ($champion->champion == 0)
                    {
                        echo 'Vacant';
                    }
                    else {
                        $champion->champion;
                    }
                    echo '</td>'; 
                    echo '<td>';
                    if ($champion->contender1 == 0)
                    {
                        echo 'TBD';
                    }
                    else {
                        $champion->contender1;
                    }
                    echo '</td>'; 
                    echo '<td>';
                    if ($champion->contender2 == 0)
                    {
                        echo 'TBD';
                    }
                    else {
                        $champion->contender2;
                    }
                    echo '</td>'; 
                    echo '<td>';
                    if ($champion->contender3 == 0)
                    {
                        echo 'TBD';
                    }
                    else {
                        $champion->contender3;
                    }
                    echo '</td>'; 
                    echo '<td style="text-align: center">'; 
                    $data = array('name' => 'user', 'value' => $champion->id); 
                    echo '<a href="bios/edituser"><img src='.base_url().'assets/img/icons/packs/fugue/24x24/plus-circle.png /></a>'; 
                    echo '<img src='.base_url().'assets/img/icons/packs/fugue/24x24/cross-circle.png id="delete"/>'; 
                    echo '</td>';
                    echo '</tr>
                    '; } } ?>

有任何想法吗?

4

1 回答 1

1

假设该数组实际上是函数返回的内容:

foreach ($array as $key => $titleObject) {
    $champion = ($titleObject->champion ? $titleObject->champion : 'Vacant');
    echo $titleObject->titleName.' champion is '.$champion.'<br />';
    $contender1 = ($titleObject->contender1 ? $titleObject->contender1 : 'tbd');
    $contender2 = ($titleObject->contender2 ? $titleObject->contender2 : 'tbd');
    $contender3 = ($titleObject->contender3 ? $titleObject->contender3 : 'tbd');
    echo $titleObject->titleName.' contenders are '.$contender1.', '.$contender2.' and '.$contender3.'<br />';
}
于 2012-04-23T16:11:18.643 回答