简体   繁体   English

Foreach PHP不起作用

[英]Foreach php doesn't work

I am trying to execute this code but I get "Invalid argument supplied for foreach()". 我正在尝试执行此代码,但收到“为foreach()提供的无效参数”。 I can't understand why. 我不明白为什么。

    $team = array(); //The team array consist all the teams that have occured so far in the loop and is empty from the beginning
    if (mysql_num_rows($select_projects) > 0) {

    while($ROW = mysql_fetch_array($select_projects)) {

        //If there is a team assigned to the object that have not occurred before, 
        //create a tr with the teamname as id, 
        //otherwise create a tr with id No team.
        if($ROW['Team'] !== "" && (in_array($ROW['Team'], $team) == False)){  
            echo '<tr id="'.$ROW['Team'].'"> <td>'.$ROW['Team'].'</td>';

                foreach($ROW['PlannedSprint'] as $plannedsprint){

                    if ($plannedsprint == "541"){
                    echo '<td>'.$ROW[DatabaseID].'';
                    }
                }
            echo '</td></tr>';
            array_push($team, $ROW['Team']);
        }
        else if($ROW['Team'] == "" && (in_array($ROW['Team'], $team) == False)){ 
            echo '<tr id="no_team"> <td>No team</td></tr>';
            array_push($team, $ROW['Team']);
        }

    }
}

You should think a little bit of your Database design. 您应该考虑一下数据库设计。 I guess you're trying to achieve a one-to-many-relationship between the teams and their planned sprints. 我想您正在尝试在团队与其计划的冲刺之间建立一对多的关系 Consider the following tables: 请考虑以下表格:

Teams:
------------
|ID|Name    |...
|1 |teamOne |...
|2 |teamTwo |...


Sprints:
------------
|ID|Name|byTeam|...
|1 |foo |1     |...
|2 |bar |2     |...
|3 |baz |2     |...
|4 |toDo|NULL  |...

This is the correct way to do this, because a) only the necessary Information will be stored (if a team did 42 sessions, it's not useful to store the teams' name 42 times!) b) it avoids update-anomalies. 这是正确的方法,因为a)仅存储必要的信息(如果团队进行了42次会话,则将团队的名称存储42次是没有用的!)b)避免了更新异常。 (if you change the name of a team, you would need to change every sprint-row in your current model) c) you can easily add/remove columns (如果更改团队的名称,则需要更改当前模型中的每个sprint行)c)您可以轻松地添加/删除列

With this database, your code could be: (using a nice PDO-Feature:) 使用此数据库,您的代码可以是:(使用不错的PDO功能:)

$db = new PDO('mysql:host=localhost;dbname=workplan', 'mysqlUser', 'mysqlPassw');
$mainQuery = $db->query('SELECT COALESCE(Team.Name,\'No Team\') as teamName, Sprints.Name as taskName, Sprints.ID as taskID FROM Sprints LEFT JOIN Teams ON Sprints.byTeam = Teams.ID ORDER BY Teams.ID');
$resultToShow = $mainQuery->fetchAll(PDO::FETCH_ASSOC|PDO::FETCH_GROUP);
/*fetch_group will combine all rows with the same value for the _first column_ in an associative array. Therefore, the array will now look the following:
Array(
['No Team']=>   Array(
                    Array(
                        ['taskName']=> 'ToDo',
                        ['taskID']=> 4
                    ),
                ),
['teamOne']=>   Array(
                    Array(
                        ['taskName']=>'foo',
                        ['taskId']=>1
                    )
                ),
['teamTwo']=>   Array(
                    Array(
                        ['taskName']=>'Bar',
                        ['taskId']=>2
                    ),
                Array(
                        ['taskName']=>'Baz',
                        ['taskId']=>3
                    )
                )
)
*/
foreach($resultToShow as $teamName => $sprints) {
    /*if you really want to show every sprint as a _column_, use the following lines:
    echo '<tr id=' . $teamName . '><td>' . $teamName . '</td>';        
    foreach($sprints as $sprint){
        echo '<td>'.$sprint['taskID'].'</td>';
    }
    echo '</tr>';
    */
    //But i think you more want a grouped table, so you could use:
    echo '<tr class=' . $teamName . '><td colspan=2>' . $teamName . '</td>';
    foreach($sprints as $sprint){
        echo '<tr><td>'.$sprint['taskID'].'</td><td>'.$sprint['taskName'].'</td></tr>';
    }
}

I really hope this helps you, please don't hesitate to ask if not. 我真的希望这对您有所帮助,请不要犹豫,问是否可以。 I'd recommend you to read a little bit about entity-relationship-models, relationships and SQL joins. 我建议您阅读一些有关实体关系模型,关系和SQL连接的信息。 They will help you in the future to understand databases and design complex and high-quality Database-Applications :-) 他们将在将来帮助您了解数据库并设计复杂而高质量的数据库应用程序:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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