我要做的是创建一个数组,其中包含标题列表,标题名称,数据库行的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
)
)
答案 0 :(得分: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;
}
我对你的架构一无所知,所以这是一个黑暗的镜头。
祝你好运。