用于循环的错误逻辑,得到意想不到的结果

时间:2014-08-19 01:28:52

标签: php sql

我正在尝试创建一个结果页面,显示从第1周到第17周的所有用户选择。

我想实现以下目标。

player | w1 | w2 | w3 | w4 | w5 | w6 | w7 | w8 | w9 | w10 | w11 | w12 | w13 | w14 | w15 | w16 | w17 |
userAA | NY | WAS| NP | NP | NP | NP | NP | NP | NP | NP  | NP  | NP  | NP  | NP  | NP  | NP  | NP  |
userBB | PHI| NP | DEN| NP | NP | NP | NP | NP | NP | NP  | NP  | NP  | NP  | NP  | NP  | NP  | NP  |

我当前的代码输出的内容是

player | w1 | w2 | w3 | w4 | w5 | w6 | w7 | w8 | w9 | w10 | w11 | w12 | w13 | w14 | w15 | w16 | w17 |
userAA | NY | NP | NP | NP | NP | NP | NP | NP | NP | NP  | NP  | NP  | NP  | NP  | NP  | NP  | NP  | NP  | WAS | NP  | NP  | NP  | NP  | NP  | NP  | NP  | NP  | NP  | NP  | NP  | NP  | NP  | NP  | NP  |
userBB | PHI| NP | NP | NP | NP | NP | NP | NP | NP | NP  | NP  | NP  | NP  | NP  | NP  | NP  | NP  | NP  | NP  | DEN | NP  | NP  | NP  | NP  | NP  | NP  | NP  | NP  | NP  | NP  | NP  | NP  | NP  | NP  |

如果用户没有某一周的选择,那么我想把它放在那里,然后输出NP。

这是我的代码。我的逻辑出了什么问题?

$sqlU = " SELECT * FROM users WHERE userID NOT IN ('1') ORDER BY userName ";

+----------+---------+
| userName |  avatar |
+----------+---------+
|  userAA  |    1    |
+----------+---------+
|  userBB  |    2    |
+----------+---------+

$sqlP = " SELECT userName, pickID, schedule.weekNum, gameTimeEastern, ";
$sqlP .=    " IF (pickID=visitorID, visitorResult, homeResult) AS finalResult ";
$sqlP .=    " FROM users ";
$sqlP .=    " JOIN picks ";
$sqlP .=    " ON users.userID = picks.userID ";
$sqlP .=    " JOIN schedule ";
$sqlP .=    " ON picks.gameID = schedule.gameID ";
$sqlP .=    " ORDER BY userName";

+----------+---------+----------+-----------------+
| userName |  pickID | weekNum  | gameTimeEastern |
+----------+---------+----------+-----------------+
|  userAA  |   NY    |    1     |    some time    |
+----------+---------+----------+-----------------+
|  userAA  |   WAS   |    2     |    some time    |
+----------+---------+----------+-----------------+
|  userBB  |   PHI   |    1     |    some time    |
+----------+---------+----------+-----------------+
|  userBB  |   DEN   |    3     |    some time    |
+----------+---------+----------+-----------------+

date_default_timezone_set('US/Eastern');
$easternTime = date('Y-m-d H:i:s');

//querys for userNames
$queryU = mysql_query($sqlU) or die(mysql_error());
while ($resultU = mysql_fetch_array($queryU)) {
    echo '  <tr class="user" id="' . $resultU[userID] . '">' . "\n";

    echo '      <td class="user-info"><img src="images/nfl/' . $resultU[avatar] . '.png" alt="' . $resultU[userName] . '"><span class="user-name"><a href="/test/stats.php?player=' . $resultU[userName]. '" ><span>' . $resultU[userName] . '</span></a></span></td>' . "\n";
    //querys for picks
    $queryP = mysql_query($sqlP) or die(mysql_error());
    while ($resultP = mysql_fetch_array($queryP)) {
        //get picks
        if ($resultP[userName] == $resultU[userName]) {
            for ($i = 1; $i <= 17; $i++) {
                if ($resultP[weekNum] == $i) {
                    //if final result is entered show picks with result
                    if( $resultP[finalResult] != '' ) {
                        echo '<td class="user-single-pick ' . $resultP[finalResult] . '"><img src="images/nfl/' . strtolower($resultP['pickID']) . 'sml.png" alt="' . $resultP[pickID] . '"></td>' . "\n";
                    //if result is not entered show picks without results
                    } else {
                        //if games has started show picks
                        if( $easternTime > $resultP[gameTimeEastern] ) {
                            echo '<td class="user-single-pick"><img src="images/nfl/' . strtolower($resultP['pickID']) . 'sml.png" alt="' . $resultP[pickID] . '"></td>' . "\n";
                        //if games has not started yet show placeholder picks
                        } else {
                            echo '<td class="user-single-pick"><img class="team-logo" src="images/nfl/hphsml.png" alt="helmet placeholder"></td>' . "\n";
                        }
                    }
                } else {
                    echo '<td class="user-single-pick"><span class="no-pick">NP</span></td>' . "\n";
                }
            }
        }
    }
    echo '  </tr>' . "\n";
}

1 个答案:

答案 0 :(得分:1)

你需要两个循环。第一个循环应该读取查询中的所有结果,并将值放在多维数组中。第二个循环应该打印所有列,它可以测试数组中是否缺少值。

$sqlP = " SELECT userName, avatar, pickID, schedule.weekNum, gameTimeEastern, ";
$sqlP .=    " IF (pickID=visitorID, visitorResult, homeResult) AS finalResult ";
$sqlP .=    " FROM users ";
$sqlP .=    " JOIN picks ";
$sqlP .=    " ON users.userID = picks.userID ";
$sqlP .=    " JOIN schedule ";
$sqlP .=    " ON picks.gameID = schedule.gameID ";
$sqlP .=    " ORDER BY userName";

$userdata = array();
$queryP = mysql_query($sqlP) or die(mysql_error());
while ($resultP = mysql_fetch_assoc($queryP)) {
    $username = $resultP['userName'];
    if (!isset($userdata[$username])) {
        $userdata[$username] = array('avatar' => $resultP['avatar']);
    }
    $userdata[$username][$resultP['weekNum']] = $resultP;
}

foreach ($userdata as $username => $picks) {
    echo '  <tr class="user" id="' . $username . '">' . "\n";
    echo '      <td class="user-info"><img src="images/nfl/' . $picks['avatar'] . '.png" alt="' . $username . '"><span class="user-name"><a href="/test/stats.php?player=' . $username. '" ><span>' . $username . '</span></a></span></td>' . "\n";
    for ($weeknum = 1; $weeknum <= 17; $weeknum++) {
        echo '<td>' . (isset($picks[$weeknum]) ? $picks[$weeknum]['pickID'] : 'NP' ) . '</td>';
    }
    echo "</tr>\n";
}