Page 1 of 1

Help with PHP Code

Posted: Tue Jul 05, 2011 5:23 pm
by Patsman77
PHP-General - Help with PHP Code

--------------------------------------------------------------------------------
Hello All,

I am hoping that someone can tell me where I am going wrong. I am pulling out my hair trying to figure this out. Here is what I am trying accomplish:
Survivor Football Pool -
1.) Before game starts, a "?" is in place of the team that the person selected.
2.) After game starts, the team that player choose becomes visible
3.) If win, cell is green, if lost, cell is red

Right now 1 and 3 are working fine
2 is a little shakey. I can't get the team to be visible once game starts, but it will show if I enter the score which signifies the game is over.

Here is the code:

Code: Select all

Here is the code:

PHP Code:
<?php 



$datenow=time() + 7200; 
$sql="select min(gametime) as t FROM ((select gametime from phpfb_schedule WHERE week=$week) as x)"; // When does the first game of the week start? 
$tresult = mysql_query($sql); 
$tres = mysql_fetch_object($tresult); 
$mintime = $tres->t; 
//echo "$mintime $datenow<br>"; 
$alllocked = ($mintime < $datenow); 
//echo "$week $alllocked<br>"; 
$base = "SELECT Max(Week) as x, user FROM ((SELECT DISTINCT survivorpool.* from survivorpool, phpfb_schedule WHERE ((pick=hid AND hscore>vscore) OR (pick=vid AND hscore<vscore) AND Pick<>'OUT' AND Pick<>'BAD' AND Pick<>'XXX' OR survivorpool.week=1)) as y) GROUP BY User ORDER BY x DESC"; 
//$base = "select user, count(*) as x FROM survivorpool WHERE Pick<>'OUT' AND Pick<>'BAD' AND Pick<>'XXX' GROUP BY User ORDER BY x DESC"; 
$resultOrig = mysql_query($base); 
$currentnum = 0; 
$numrows = mysql_num_rows($resultOrig); 
while ($all[$currentnum++] = mysql_fetch_object($resultOrig)){} 
mysql_free_result($resultOrig); 
for ($x = 0; $x < $numrows; $x++){ 
$next = $all[$x]; 
$p++; 
//while ($next = mysql_fetch_object($resultOrig)){ 
$current=$next->user; 
$user=$current; 

//$sql = "select user, Week, Pick FROM survivorpool WHERE user='$current' AND Week<$week"; 
$sql = "select user, Week, Pick FROM survivorpool WHERE user='$current' AND Week<=$week ORDER BY Week ASC"; 
//echo "$sql<br>"; 
$lastweek=0; 
$done=false; 
$resultA = mysql_query($sql) or die(mysql_error()); 
//echo mysql_errno() . " code<br>"; 
//echo mysql_num_rows($resultA) . " rows, " . mysql_num_fields($resultA) . " fields<br>"; 
//$row = mysql_fetch_object($resultA); 
//echo $row->user . " " . $row->Week . " " . $row->Pick . " END<br>"; 
//echo "$week<br>"; 

while(($result=mysql_fetch_object($resultA))){ 
    $done=false; 
     
    while (!$done){ 
//    echo $lastweek . " " . ($result->Week-1) . "<br>"; 
//    var_dump($row); 
//    echo $result->user . " " . $result->Week . " " . $result->Pick . "<br>"; 
        if ($lastweek == ($result->Week - 1)){ 
//            echo "OK on $lastweek<br>"; 
            $lastweek++; 
            $sql2="SELECT * FROM phpfb_schedule WHERE (hid='" . $result->Pick . "' OR vid='" . $result->Pick . "') AND week=" . $result->Week; 
//            echo "$sql2<br>"; 
            $resultB = mysql_query($sql2); 
//            echo mysql_num_rows($resultB) . "<br>"; 
            if ($result2=mysql_fetch_object($resultB)){ 
                if ((($result->Pick == $result2->hid && $result2->hscore <= $result2->vscore) || ($result->Pick == $result2->vid && $result2->hscore >= $result2->vscore)) && !($result2->hscore == 0 && $result2->vscore == 0)){ // Wrong pick 
//                    echo "$user made wrong pick at $lastweek<br>"; 
                    $done=true; 
                    $picks[$user][$lastweek][0] = $result->Pick; 
                    $picks[$user][$lastweek][1] = 1; 
                } 
                else{ // Correct pick or in progress 
                    if ($result2->vscore == 0 && $result2->hscore == 0){ // Game in progress 
                        $picks[$user][$lastweek][1] = 2; 
                        $done=true; 
                        if (!($lastweek == $week && !$alllocked)){ 
                            $picks[$user][$lastweek][0] = $result->Pick; 
                        } 
                        else{ 
                            $picks[$user][$lastweek][0] = "?"; 
                        } 
                    } 
                    else{ 
//                        echo "$user made good pick at $lastweek<br>"; 
                        $picks[$user][$lastweek][0] = $result->Pick; 
                        $picks[$user][$lastweek][1] = 0; 
                    } 
                    $done=true; 
                } 
            } 
            else{ // Invalid pick 
                $picks[$user][$lastweek][0] = "BAD"; 
                $picks[$user][$lastweek][1] = 1; 
                $done=true; 
            } 
        } 
        else{ //Missed week 
            $lastweek++; 
//            echo "$user misssed $lastweek<br>"; 
            if (!($lastweek == $week && !$alllocked)){ 
                $picks[$user][$lastweek][0] = "MISSED"; 
            } 
            else{ 
                $picks[$user][$lastweek][0] = "?"; 
            } 
            $picks[$user][$lastweek][1] = 1; 
        } 
    } 
} 
//echo "$user $lastweek $week<br>"; 
if ($lastweek<$week){ //Missed week 
    $lastweek++; 
//    echo "$user missed $lastweek<br>"; 
    if ($alllocked){ 
//        echo "$user MISSED<br>"; 
        $picks[$user][$lastweek][0] = "MISSED"; 
        $picks[$user][$lastweek][1] = 1; 
    } 
    else{ 
//        echo "$user?<br>"; 
        $picks[$user][$lastweek][0] = "?"; 
        $picks[$user][$lastweek][1] = 2; 
    } 
    $done=true; 
} 

//echo $next->User . " " . $next->x . "<BR>"; 
$done = false; 

echo "<tr><td>$p</td><td>" . $next->user . "</td>"; 
for ($count = 1; $count<=$week && !$done; $count++) 
{ 
    if ($picks[$user][$count][1] == 1){ // Wrong pick 
        $done=true; 
        echo "<td bgcolor=\"red\"><img src=\"/images/" . $picks[$user][$count][0] . "l.gif\"></td>"; 
    } 
     
    else if ($picks[$user][$count][1] == 2){ // Game in progress 
        $done = true; 
        if (match_done($picks[$user][$count][0], $count)) { 
        echo "<td bgcolor=\"white\">?</td>"; 
        } else { 
        if ($picks[$user][$count][0] != "?") echo "<td bgcolor=\"white\"><img src=\"/images/" . $picks[$user][$count][0] . "l.gif\"></td>"; 
        else echo "<td bgcolor=\"white\">?</td>"; 
    }} 
    else{ // correct pick 
        echo "<td bgcolor=\"green\"><img src=\"/images/" . $picks[$user][$count][0] . "l.gif\"></td>"; 
    } 
} 
if (mysql_num_rows($resultA) < $week-1) echo "<td bgcolor=\"black\"><font color=\"white\">OUT</font></td>"; 
echo "</tr>\n"; 
} 

?> 
</table> 

If anyone can see where I am going wrong I would greatly appreciate it!

Thanks,

Patsman77

Re: Help with PHP Code

Posted: Wed Jul 06, 2011 2:51 am
by social_experiment
Patsman77 wrote:2.) After game starts, the team that player choose becomes visible
Could you explain what you mean by becomes visible?

Is the whole code sample related to the problem you are having?

Re: Help with PHP Code

Posted: Wed Jul 06, 2011 2:18 pm
by Patsman77
Thanks for the reply!

By visible, I mean that once a game starts that involves a team in the standings page, it becomes seen in the standings.

For example...
I choose New England as my team for this week.
The game starts at 1 PM on Sunday.
From Tuesday till Sunday at 12:59 PM it shows a "?" in place of my team. This is done so other people can't see who I took for my team.
At 1:oo PM (start of the game that NE is playing) my team now is visible for all to see in the standings.
Once I enter a score, my team will have a green cell for a win or a red cell for a loss.

Hope this helps.

Thanks,

Patsman77

Re: Help with PHP Code

Posted: Wed Jul 06, 2011 4:37 pm
by social_experiment
You will probably have to use something like a cron job, i don't know alot about those though ;)

Re: Help with PHP Code

Posted: Thu Jul 07, 2011 8:09 am
by Charles256
Mind just posting the snippet of your code that you think is failing ? Or are you saying you have no idea how to approach the problem and need some general suggestions?

Re: Help with PHP Code

Posted: Thu Jul 07, 2011 1:38 pm
by Patsman77
I think this is where I need to be looking. This is the part of the code that targets what I am trying to do:
for ($count = 1; $count<=$week && !$done; $count++)
{
if ($picks[$user][$count][1] == 1){ // Wrong pick
$done=true;
echo "<td bgcolor=\"red\"><img src=\"/images/" . $picks[$user][$count][0] . "l.gif\"></td>";
}

else if ($picks[$user][$count][1] == 2){ // Game in progress
$done = true;
if (match_done($picks[$user][$count][0], $count)) {
echo "<td bgcolor=\"white\">?</td>";
} else {
if ($picks[$user][$count][0] != "?") echo "<td bgcolor=\"white\"><img src=\"/images/" . $picks[$user][$count][0] . "l.gif\"></td>";
else echo "<td bgcolor=\"white\">?</td>";
}}
else{ // correct pick
echo "<td bgcolor=\"green\"><img src=\"/images/" . $picks[$user][$count][0] . "l.gif\"></td>";
}
}
if (mysql_num_rows($resultA) < $week-1) echo "<td bgcolor=\"black\"><font color=\"white\">OUT</font></td>";
echo "</tr>\n";

Re: Help with PHP Code

Posted: Thu Jul 07, 2011 4:41 pm
by Patsman77
Issue resolved