0

我要做的是创建一个数组,其中包含标题列表,标题名称,数据库行的 id,然后是冠军和竞争者的名称。对于冠军和竞争者的值,我需要做一个额外的查询来检索这个人的名单名称。如果冠军的值为 0,则需要将 Vacant 添加到该位置的阵列中,如果竞争者的值为 0,则它使用 TBD 作为阵列。这是我正在使用的内容,其中包括查询和 print_r 输出。

我的问题是我不确定我需要在哪里/如何使用每个标题的冠军和竞争者的这些值运行附加查询。

    /**
     * Get titles champions
     *
     * @return  object/NULL
     */
    function getTitlesChampions()
    {      
        $this->db->select('titlesList.id');
        $this->db->select('titlesList.titleName');
        $this->db->select('titlesChampions.championID');
        $this->db->select('titlesChampions.contender1ID');
        $this->db->select('titlesChampions.contender2ID');
        $this->db->select('titlesChampions.contender3ID');
        $this->db->from('titlesChampions');
        $this->db->join('titlesList', 'titlesList.id = titlesChampions.titlesListID');
        $query = $this->db->get();  
        if ($query->num_rows() > 0) {
            echo "<pre>";
            print_r ($query->result());
            echo "</pre>";

        }                                    
    }

Array
(
[0] => stdClass Object
    (
        [id] => 1
        [titleName] => Undisputed Heavyweight Title
        [championID] => 1
        [contender1ID] => 1
        [contender2ID] => 1
        [contender3ID] => 1
    )

[1] => stdClass Object
    (
        [id] => 2
        [titleName] => Outlaw Title
        [championID] => 1
        [contender1ID] => 0
        [contender2ID] => 0
        [contender3ID] => 0
    )

[2] => stdClass Object
    (
        [id] => 3
        [titleName] => Tag Team Titles
        [championID] => 1
        [contender1ID] => 0
        [contender2ID] => 0
        [contender3ID] => 0
    )

)
4

1 回答 1

1

最佳实践是一组额外的连接和子查询,但考虑到模式的(假定)状态,拥有辅助功能可能是最简单的。

对于找到的每个结果,您将遍历并通过调用来分配名称getRosterName()。该函数将返回并向 Champions 对象添加附加信息。

/**
 * Get titles champions
 *
 * @return  object/NULL
 */
function getTitlesChampions()
{
    $this->db->select('titlesList.id');
    $this->db->select('titlesList.titleName');
    $this->db->select('titlesChampions.championID');
    $this->db->select('titlesChampions.contender1ID');
    $this->db->select('titlesChampions.contender2ID');
    $this->db->select('titlesChampions.contender3ID');
    $this->db->from('titlesChampions');
    $this->db->join('titlesList', 'titlesList.id = titlesChampions.titlesListID');
    $query = $this->db->get();  

    if ($query->num_rows() > 0) {

        $result = query->result();

        foreach($result as $row)
        {
            // Iterate through
            $row->championName      =   $this->getRosterName($row->championID);
            $row->contender1Name    =   $this->getRosterName($row->contender2ID);
            $row->contender2Name    =   $this->getRosterName($row->contender2ID);
            $row->contender3Name    =   $this->getRosterName($row->contender3ID);
        }

        // Return it
        return $result;

    }
    return null;
}

/*

/* Returns the name */
function getRosterName($rosterId = null)
{

    if($rosterId && $rosterID > 0)
    {
        $this->db->select('RosterName');
        $this->db->where('rosterId', $rosterId);
        $query = $this->db->get('roster'); // Or whatever your `roster` table is named.
        return $query->row()->firstName; // Or whatever the name is you want returned
    }
    return null;
}

我对您的架构一无所知,所以这是在黑暗中拍摄的。

祝你好运。

于 2012-04-24T22:45:57.673 回答