没有回应正确的值

时间:2012-04-23 16:05:54

标签: php codeigniter

我在视图文件中遇到了数组问题。我想要做的就是让它显示每个标题的标题和竞争者列表。截至目前,当我拥有应该工作的价值而不是每个冠军的每个冠军时,它会显示空置作为每个冠军的冠军和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;
}

titlesChampions表中的值是:

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>
                    '; } } ?>

有什么想法吗?

1 个答案:

答案 0 :(得分: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 />';
}