Quick help please...

Started by Fog, December 28, 2013, 08:17:46 pm

Previous topic - Next topic

Fog

I'm get this error:

Warning: Invalid argument supplied for foreach() in /home/eastsid2/public_html/fog/phppickem-master/standings.php on line 20

This is line 20:

foreach($stats[winners] as $winner => $winnerID) {

Here's the whole code


<?php
require('includes/application_top.php');

$weekStats = array();
$playerTotals = array();
$possibleScoreTotal 0;
calculateStats();

include(
'includes/header.php');
?>

<h1>Standings</h1>
<h2>Weekly Stats</h2>
<table cellpadding="4" cellspacing="0" class="table1">
<tr><th align="left">Week</th><th align="left">Winner(s)</th><th>Score</th></tr>
<?php
if (isset($weekStats)) {
$i 0;
foreach($weekStats as $week => $stats) {
$winners '';
foreach($stats[winners] as $winner => $winnerID) {
$tmpUser $login->get_user_by_id($winnerID);
switch ($user_names_display) {
case 1:
$winners .= ((strlen($winners) > 0) ? ', ' '') . trim($tmpUser->firstname ' ' $tmpUser->lastname);
break;
case 2:
$winners .= ((strlen($winners) > 0) ? ', ' '') . $tmpUser->userName;
break;
default: //3
$winners .= ((strlen($winners) > 0) ? ', ' '') . '<abbrev title="' trim($tmpUser->firstname ' ' $tmpUser->lastname) . '">' $tmpUser->userName '</abbrev>';
break;
}
}
$rowclass = (($i == 0) ? ' class="altrow"' '');
echo ' <tr' $rowclass '><td>' $week '</td><td>' $winners '</td><td align="center">' $stats[highestScore] . '/' $stats[possibleScore] . '</td></tr>';
$i++;
}
} else {
echo ' <tr><td colspan="3">No weeks have been completed yet.</td></tr>' "\n";
}
?>

</table>
<p>&nbsp;</p>

<h2>User Stats</h2>
<table cellpadding="0" cellspacing="0">
<tr valign="top">
<td>
<b>By Name</b><br />
<table cellpadding="4" cellspacing="0" class="table1">
<tr><th align="left">Player</th><th align="left">Wins</th><th>Pick Ratio</th></tr>
<?php
if (isset($playerTotals)) {
//arsort($playerTotals);
$i 0;
foreach($playerTotals as $playerID => $stats) {
$rowclass = (($i == 0) ? ' class="altrow"' '');
$pickRatio $stats[score] . '/' $possibleScoreTotal;
$pickPercentage number_format((($stats[score] / $possibleScoreTotal) * 100), 2) . '%';
switch ($user_names_display) {
case 1:
echo ' <tr' $rowclass '><td class="tiny">' $stats[name] . '</td><td class="tiny" align="center">' $stats[wins] . '</td><td class="tiny" align="center">' $pickRatio ' (' $pickPercentage ')</td></tr>';
break;
case 2:
echo ' <tr' $rowclass '><td class="tiny">' $stats[userName] . '</td><td class="tiny" align="center">' $stats[wins] . '</td><td class="tiny" align="center">' $pickRatio ' (' $pickPercentage ')</td></tr>';
break;
default: //3
echo ' <tr' $rowclass '><td class="tiny"><abbrev title="' $stats[name] . '">' $stats[userName] . '<abbrev></td><td class="tiny" align="center">' $stats[wins] . '</td><td class="tiny" align="center">' $pickRatio ' (' $pickPercentage ')</td></tr>';
break;
}
$i++;
}
} else {
echo ' <tr><td colspan="3">No weeks have been completed yet.</td></tr>' "\n";
}
?>

</table>
</td>
<td>&nbsp;</td>
<td>
<b>By Wins</b><br />
<table cellpadding="4" cellspacing="0" class="table1">
<tr><th align="left">Player</th><th align="left">Wins</th><th>Pick Ratio</th></tr>
<?php
if (isset($playerTotals)) {
arsort($playerTotals);
$i 0;
foreach($playerTotals as $playerID => $stats) {
$rowclass = (($i == 0) ? ' class="altrow"' '');
$pickRatio $stats[score] . '/' $possibleScoreTotal;
$pickPercentage number_format((($stats[score] / $possibleScoreTotal) * 100), 2) . '%';
switch ($user_names_display) {
case 1:
echo ' <tr' $rowclass '><td class="tiny">' $stats[name] . '</td><td class="tiny" align="center">' $stats[wins] . '</td><td class="tiny" align="center">' $pickRatio ' (' $pickPercentage ')</td></tr>';
break;
case 2:
echo ' <tr' $rowclass '><td class="tiny">' $stats[userName] . '</td><td class="tiny" align="center">' $stats[wins] . '</td><td class="tiny" align="center">' $pickRatio ' (' $pickPercentage ')</td></tr>';
break;
default: //3
echo ' <tr' $rowclass '><td class="tiny"><abbrev title="' $stats[name] . '">' $stats[userName] . '</abbrev></td><td class="tiny" align="center">' $stats[wins] . '</td><td class="tiny" align="center">' $pickRatio ' (' $pickPercentage ')</td></tr>';
break;
}
$i++;
}
} else {
echo ' <tr><td colspan="3">No weeks have been completed yet.</td></tr>' "\n";
}
?>

</table>
</td>
<td>&nbsp;</td>
<td>
<b>By Pick Ratio</b><br />
<table cellpadding="4" cellspacing="0" class="table1">
<tr><th align="left">Player</th><th align="left">Wins</th><th>Pick Ratio</th></tr>
<?php
if (isset($playerTotals)) {
$playerTotals sort2d($playerTotals'score''desc');
$i 0;
foreach($playerTotals as $playerID => $stats) {
$rowclass = (($i == 0) ? ' class="altrow"' '');
$pickRatio $stats[score] . '/' $possibleScoreTotal;
$pickPercentage number_format((($stats[score] / $possibleScoreTotal) * 100), 2) . '%';
switch ($user_names_display) {
case 1:
echo ' <tr' $rowclass '><td class="tiny">' $stats[name] . '</td><td class="tiny" align="center">' $stats[wins] . '</td><td class="tiny" align="center">' $pickRatio ' (' $pickPercentage ')</td></tr>';
break;
case 2:
echo ' <tr' $rowclass '><td class="tiny">' $stats[userName] . '</td><td class="tiny" align="center">' $stats[wins] . '</td><td class="tiny" align="center">' $pickRatio ' (' $pickPercentage ')</td></tr>';
break;
default: //3
echo ' <tr' $rowclass '><td class="tiny"><abbrev title="' $stats[name] . '">' $stats[userName] . '</abbrev></td><td class="tiny" align="center">' $stats[wins] . '</td><td class="tiny" align="center">' $pickRatio ' (' $pickPercentage ')</td></tr>';
break;
}
$i++;
}
} else {
echo ' <tr><td colspan="3">No weeks have been completed yet.</td></tr>' "\n";
}
?>

</table>
</td>
</tr>
</table>
<p>&nbsp;</p>
<?php
include('includes/footer.php');
?>


Chen Zhen

Fog,

  There is no need to switch back and forth between php and html in that way. With php you just echo the html in single-quotes (apostrophes) using backslashes when you want to echo the actual character of a single-quote (because you're using them to contain the displayed content).
You need to ensure the array is not null prior to the foreach loop. When the array is null it will throw that error when you attempt a loop with it.

  It's a bit of a mess but you either want it to read:

foreach($weekStats as $week => $stats) {
        if (empty($stats['winners']))
             continue;
$winners = '';
foreach($stats['winners'] as $winner => $winnerID) {


or if you're trying to loop through the whole $stats array as I suspect:

foreach($weekStats as $week => $stats) {
        if (empty($stats))
             continue;
$winners = '';
foreach($stats as $winner => $winnerID) {

Fog

That makes sense, but I need to by pass any null and start with game id 163 and week 12.

It's a silly error that doesn't do anything be make the standing page look like this.

Fog

Sorry UD..I'm just trying to modify somebody else's game and program and I have had this error from the start.

It's been bugging me... ::)

Fog

I changed it up with what you suggested UD...it got rid of the error but now displays this:

Chen Zhen


Fog,

  I can help you out with questions regarding bits of code here & there but I do not have the time atm to rewrite that little project you are doing.
Use the first example which is exactly what you posted but has logic to circumvent the error. You are going to have to use trial and error to get the results you wish.

Fog

UD...please don't spend any time on this.

It seems when you start from week one it works but when you try to start on week 12 you get an error.    8)

You should see what I did for a NASCAR Game we made for my users.  I modified the NFL Pickems PHP Game I found, and changed up the NFL teams for NASCAR Drivers.

Driver vs Driver...

Changed out the NFL teams for Drivers..

Weeks for Races

Replaced all of the NFL imagines with NASCAR driver imagines.

I Have a current 2014 NASCAR Schedule on the DataBase...16x37 (592 tables, what a pain in the bass) 

I have 16 match ups for 37 weeks...

I have 32 drivers so I have to create 31 different match ups...