2

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?

e.klara.k
  • 369
  • 1
  • 6
  • 17
  • 1
    Have you tried printing the values on each side of the expression and seeing what output you get? – baarkerlounger Sep 16 '15 at 09:00
  • Partly this would only work for the first team. You need to reset the results of the query (probably using mysql_data_seek) – Kickstart Sep 16 '15 at 09:04
  • @db579 If I move echo ''.$all_objects['DatabaseID'].''; to before the If-statement it prints out all the IDs in the first team's row – e.klara.k Sep 16 '15 at 09:04
  • Just echo $all_objects['Team'] and $team right before the if statement. You should be able to see what's going on from that. – baarkerlounger Sep 16 '15 at 09:05

2 Answers2

1

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());
}
?>
Kickstart
  • 21,403
  • 2
  • 21
  • 33
  • Thank you! Is there an easy way to switch from mysql? – e.klara.k Sep 16 '15 at 09:25
  • For many things it is easy. Mysqli supports parameterised queries and switching to those is a lot more work (while also making the queries less readable, and making it difficult to echo out failed queries in a usable state). You can use mysqli in much the same way as mysql_* functions and most need little more than a change of name. Main one that is a bit more difficult to change is mysqli_real_escape_string, where the link identifier became compulsory and also became the first parameter rather than the 2nd parameter in mysql_real_escape_string. – Kickstart Sep 16 '15 at 09:31
  • you didn't give a warning for mysqli_ :( :( – Halayem Anis Sep 16 '15 at 09:35
0

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

}
?>
Halayem Anis
  • 7,654
  • 2
  • 25
  • 45
  • why mysqli have to use instead mysql? i'm also developing web and newbie. so don't know much. – Rohan Khude Sep 16 '15 at 09:15
  • 1
    The mysql_* functions are deprecated. Ie, they are no longer being developed and will in a future version of php be removed (from the manual pages, php 7 will have removed these functions). – Kickstart Sep 16 '15 at 09:20
  • @RohanKhude : i added a link for official documentation. it's a very old extension, more than 17 years... vulnerable for SQL Injection and it's connect to MySQL server at a low-level. There is many threads on SO regarding this. Hope that i helped you with my answear and warning :) – Halayem Anis Sep 16 '15 at 09:21
  • but now i have developed about 50 pages of php. with mysql. How can i update it with mysqli – Rohan Khude Sep 16 '15 at 09:24
  • migrate :) http://stackoverflow.com/questions/4598854/how-do-i-migrate-my-site-from-mysql-to-mysqli – Halayem Anis Sep 16 '15 at 09:30