我在PHP循环中遇到if语句问题。我有一个名为$ all_teams的团队。假设它包含Team Blue,Team Red和Team Yellow。我还有一个数据库,其中对象被分配给团队。
DatabaseID | State | Team | ...
12 Ready Blue
33 Finished Red
65 Ready Blue
我现在正在尝试运行以下代码:
<?php
$query = "SELECT DISTINCT * FROM `dmt_objects` WHERE PipelineAndWorkflow = 'RWP WF Ready for 4Sprint' ORDER BY `Team` DESC ;";
$select_projects = mysql_query($query);
foreach($all_teams as $team){
echo '<tr id = '.$team.'><td>'.$team.'</td><td>';
while($all_objects = mysql_fetch_array($select_projects)){
if($all_objects['Team'] == $team){
echo ''.$all_objects['DatabaseID'].'</br>';
}
}
echo '</td></tr>';
}
?>
我正在尝试创建一个看起来像这样的表。
Blue | 12
65
Red | 33
Yellow |
if-statment
if($all_objects['Team'] == $team)
永远不会......为什么?我该如何解决我的问题?
答案 0 :(得分:1)
在每次循环后重置结果集指针(现在忽略您使用的是不推荐的mysql_ *函数): -
<?php
$query = "SELECT DISTINCT *
FROM `dmt_objects`
WHERE PipelineAndWorkflow = 'RWP WF Ready for 4Sprint'
AND Team IN ('".implode("','", $all_teams)."')
ORDER BY `Team` DESC ;";
if ($select_projects = mysql_query($query))
{
foreach($all_teams as $team)
{
echo "<tr id = '$team'><td>$team</td><td>";
while($all_objects = mysql_fetch_assoc($select_projects))
{
if($all_objects['Team'] == $team)
{
echo ''.$all_objects['DatabaseID'].'</br>';
}
}
echo '</td></tr>';
mysql_data_seek($select_projects, 0);
}
}
else
{
die(mysql_error());
}
?>
答案 1 :(得分:0)
警告请勿使用mysql_
使用mysqli_
或PDO
代替
请仔细阅读:mysql_ deprecated
<?php
$query = "SELECT DISTINCT * FROM `dmt_objects` WHERE PipelineAndWorkflow = 'RWP WF Ready for 4Sprint' ORDER BY `Team` DESC ;";
$select_projects = mysql_query($query);
$all_objects = mysql_fetch_array($select_projects)
foreach($all_teams as $team){
echo '<tr id = '.$team.'><td>'.$team.'</td><td>';
foreach($all_objects as $obj)
if($obj['Team'] == $team){
echo ''.$obj['DatabaseID'].'</br>';
}
}
echo '</td></tr>';
}
?>