如何在foreach循环上进行“独特”检测

时间:2011-01-27 22:04:23

标签: php mysql loops

此时我遇到了这个非常复杂的查询,我得到了类似的结果:

挑战列表:

TEAM A
  - Challenge 1

TEAM A
  - Challenge 4

TEAM A
  - Challege 6

我想改为:

TEAM A
  - Challenge 1
  - Challenge 4
  - Challenge 6

我的问题是,由于查询是一个非常复杂的问题,也许我可以在循环中做到这一点但是,如果是这样的话,我们怎样才能实现这样的呢?

我可以问一个示例案例,以便我可以使用,以解决这个问题吗?

非常感谢, MEM

更新 查询是这样的: 翻译:

public function listachallengesPendentes()
{
  $select = $this->getAdapter()->select();
  $select->from(array("e"=>"teams"),array('name'));
  $select->join(array("de"=>"challengeperteam"),"e.cod_team = de.cod_teamFk",array());
  $select->join(array("d"=>"challenges"),"d.cod_challenge = de.cod_challengeFk",array('title'));

  $select->columns(array("e.cod_team"
                         ,"name_team"=>"e.name"
          ,"d.cod_challenge"
          ,"name_challenge"=>"d.title"
          ,"d.details"
          ,"d.score"
          ,"category"=>"d.cod_categoryFk"
          ,"de.proof"
          ,"de.date_concluded"
          ,"de.cod_challenge_team"
       ));

$select->where("de.status = 0");
$select->order(array('e.cod_team DESC', 'de.cod_challenge_team DESC'));

 return $this->getAdapter()->fetchAll($select);
 }

所以我需要添加一个独特的部分:s:D?

foreach实际上非常基本:

foreach ($challenges as $d){ 
//display the name:
echo $d['name_team']; 

...

}

更新2 干净的查询(未测试):

SELECT e.name
,d.cod_team
,d.cod_challenge
,d.title
,d.details
,d.score
,de.proof
,de.date_concluded
,de.cod_challenge_team
FROM teams e
INNER JOIN challengeperteam de ON de.cod_teamFk = e.cod_team
INNER JOIN challenges d ON d.cod_challenge = de.cod_challengeFk
WHERE  de.status = 0 
ORDER BY  e.cod_team DESC, de.cod_challenge_team DESC;

3 个答案:

答案 0 :(得分:1)

在非常基本的级别,即在循环中,您可以检测TEAM A变量是否等于当前(上一个)值,如果是,则不要再次打印它。这取决于在TEAM A列上排序的结果集。

但是,您也可以在SQL查询中执行此操作,因此,如果您可以提供当前的SQL查询,我可以解释您如何更新它。

答案 1 :(得分:1)

有些事情:

$current_team = null;
foreach($challenges as $challenge){
    if($current_team != $challenge->team){
        $current_team = $challenge->team;
        echo $current_team, "\n";
    }
    echo $challenge->challenge_name, "\n";
}

答案 2 :(得分:1)

你可以将数组结果存储在一个多维数组中,如下所示:

$query_Challenges = "SELECT `Team`,`Challenges` FROM YourTable";
$Challenges = mysql_query($query_Challenges, $dbconnection) or die(mysql_error());
$row_Challenges = mysql_fetch_assoc($Challenges);

$challengeResults = array();

do{
    if(!array_key_exists($row_Challenges['cod_team'])){
        $challengeResults[$row_Challenges['cod_team']] = array();
    }
    $challengeResults[$row_Challenges['cod_team']][] = $row_Challenges['cod_challenge_team'];
}while($row_Challenges = mysql_fetch_assoc($Challenges));

修改

查看你的查询语句,数据应该已经被你的ORDER子句正确排序了,所以如果你不需要重复打印团队,如代码块2所示,那么就像:

$team = '';
do {
    if($team != $row_Challenges['cod_team']){
        echo "TEAM $row_Challenges['cod_team']<br/>";
        $team = $row_Challenges['cod_team'];
    }
    echo " - $row_Challenges['cod_challenge_team']<br />";
}while($row_Challenges = mysql_fetch_assoc($Challenges));

只要有一个变量用作“当前团队”并且if语句用于表示“不打印下一个团队名称,除非它与当前团队名称不同”,你可以轻松地用foreach替换do循环“