sql - Wrong logic with php for loop, getting unexpected result -


i trying create result page show users picks week 1 week 17.

i achieve following.

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  | 

my current code outputting this

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

i have if user not have pick week np output.

here code. wrong logic?

$sqlu = " select * users userid not in ('1') order username ";  +----------+---------+ | username |  avatar | +----------+---------+ |  useraa  |    1    | +----------+---------+ |  userbb  |    2    | +----------+---------+  $sqlp = " select username, pickid, schedule.weeknum, gametimeeastern, "; $sqlp .=    " if (pickid=visitorid, visitorresult, homeresult) finalresult "; $sqlp .=    " users "; $sqlp .=    " join picks "; $sqlp .=    " on users.userid = picks.userid "; $sqlp .=    " join schedule "; $sqlp .=    " on picks.gameid = schedule.gameid "; $sqlp .=    " order username";  +----------+---------+----------+-----------------+ | username |  pickid | weeknum  | gametimeeastern | +----------+---------+----------+-----------------+ |  useraa  |   ny    |    1     |    time    | +----------+---------+----------+-----------------+ |  useraa  |     |    2     |    time    | +----------+---------+----------+-----------------+ |  userbb  |   phi   |    1     |    time    | +----------+---------+----------+-----------------+ |  userbb  |   den   |    3     |    time    | +----------+---------+----------+-----------------+  date_default_timezone_set('us/eastern'); $easterntime = date('y-m-d h:i:s');  //querys 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 picks     $queryp = mysql_query($sqlp) or die(mysql_error());     while ($resultp = mysql_fetch_array($queryp)) {         //get picks         if ($resultp[username] == $resultu[username]) {             ($i = 1; $i <= 17; $i++) {                 if ($resultp[weeknum] == $i) {                     //if final result entered show picks 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 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"; } 

you need 2 loops. first loop should read results query, , put values in multidimensional array. second loop should print columns, , can test whether value missing array.

$sqlp = " select username, avatar, pickid, schedule.weeknum, gametimeeastern, "; $sqlp .=    " if (pickid=visitorid, visitorresult, homeresult) finalresult "; $sqlp .=    " users "; $sqlp .=    " join picks "; $sqlp .=    " on users.userid = picks.userid "; $sqlp .=    " join schedule "; $sqlp .=    " on picks.gameid = schedule.gameid "; $sqlp .=    " order 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 $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";     ($weeknum = 1; $weeknum <= 17; $weeknum++) {         echo '<td>' . (isset($picks[$weeknum]) ? $picks[$weeknum]['pickid'] : 'np' ) . '</td>';     }     echo "</tr>\n"; } 

Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -