简体   繁体   中英

If-statement in while loop with mysql is never true PHP

I am having trouble with a if-statement in a while loop i PHP. I have an array with teams named $all_teams. Let's say that it contains Team Blue, Team Red and Team Yellow. I also have a database where objects are assigned to teams.

DatabaseID | State |  Team | ... 
12           Ready    Blue
33           Finished Red
65           Ready    Blue

I am now trying to run the following code:

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

    }
    ?>

I am trying to creat a table that should look like this.

Blue   | 12
         65
Red    | 33
Yellow |

The if-statment

if($all_objects['Team'] == $team)

is never true... Why? How can I fix my problem?

Resetting the result set pointer after each loop (and for now ignoring that you are using the deprecated mysql_* functions):-

<?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());
}
?>

WARNING DON'T USE mysql_ use mysqli_ or PDO instead
Read this carefully: 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>';

}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM