...

View Full Version : Broken code, strange problems debugging



bptuner
07-22-2012, 04:24 AM
Broken code
Hi, I've been writing an odds calculator for greyhounds and it worked fine until I made some tiny changes, now it's completely ruined.

<?php*

$p = 1;
echo "<HTML><body><form name='dogstats' action='tms2012.php' method='POST'>
<input type='hidden' name='action' value='calc'>
<br><font size=10 face=verdana><b><i>calc</b></i></font><br><br><table border=1>";

for($p = 1; $p < 9; $p++)
{
echo "<tr><td>
Number<input type='textbox' name='d" . $p . "n' size='2' value='" . $p . "'></td><td>Current odds <input type='textbox' name='d" . $p . "co' size=5></td><td>Last odds<input type='textbox' size=5 name='d" . $p . "lo'></td><td>
2nd last odds<input type='textbox' size=5 name='d" . $p . "lo2'></td><td>Last place<input type='textbox' size=1 name='d" . $p . "lp'></td><td>2nd Last place<input type='textbox' size=1 name='d" . $p . "lp2'></td>
</tr>";
}


This is the start of my code (not sure why there's an astricks next to the <?php tag, forum added that)

Upon compile and run, I get a T_VARIABLE error, so I commented out the variable on line 3 ($p), then I get unexpected ECHO on the line following it and if I comment that out, I get a unexpected FOR error which is the following statement.. this debugging pattern made it very difficult as I believe it's not reflecting the actual error

I'm doing all this on an iPad so it's hard for me to properly analyze this code, but I've combed over and over it and cant find the issue. I thought I would see if someone could post any insight before I just delete the entire code and start again.*

I've checked for missing or excessive semi colons and proper use of quotation marks, all seems in check. It's almost stupid that this code doesn't want to run, I can't find a fault with it.

Below is the entire code, I would be beyond grateful if anyone could identify the problem or whether it just doesn't want to run because it hates me.

<?php*

$p = 1;
echo "<HTML><body><form name='dogstats' action='tms2012.php' method='POST'>
<input type='hidden' name='action' value='calc'>
<br><font size=10 face=verdana><b><i>calc</b></i></font><br><br><table border=1>";

for($p = 1; $p < 9; $p++)
{
echo "<tr><td>
Number<input type='textbox' name='d" . $p . "n' size='2' value='" . $p . "'></td><td>Current odds <input type='textbox' name='d" . $p . "co' size=5></td><td>Last odds<input type='textbox' size=5 name='d" . $p . "lo'></td><td>
2nd last odds<input type='textbox' size=5 name='d" . $p . "lo2'></td><td>Last place<input type='textbox' size=1 name='d" . $p . "lp'></td><td>2nd Last place<input type='textbox' size=1 name='d" . $p . "lp2'></td>
</tr>";
}
echo "</table><br><input type='submit' value='calculate'></form></body></html>";

$first = -0.5;
$second = -0.25;
$third = -0.1;
$fourth = 0.1;
$fifth = 0.25;
$sixth = 0.5;
$seventh = 0.75;
$eighth = 1;

If($_POST['action'] == "calc"){
$d1n = $_POST['d1n'];
$d1lo = $_POST['d1lo'];
$d1lo2 = $_POST['d1lo2'];
$d1lp = $_POST['d1lp'];
$d1lp2 = $_POST['d1lp2'];

$d1r = ($d1lo + $d1lo2) / 2;
switch($d1lp){
case 1:
$d1r = $d1r + $first;
break;
case 2:
$d1r = $d1r + $second;
break;
case 3:
$d1r = $d1r + $third;
break;
case 4:
$d1r = $d1r + $fourth;
break;
case 5:
$d1r = $d1r + $fifth;
break;
case 6:
$d1r = $d1r + $sixth;
break;
case 7:
$d1r = $d1r + $seventh;
break;
case 8:
$d1r = $d1r + $eighth;
break;
}


switch($d1lp2){
case 1:
$d1r = $d1r + $first;
break;
case 2:
$d1r = $d1r + $second;
break;
case 3:
$d1r = $d1r + $third;
break;
case 4:
$d1r = $d1r + $fourth;
break;
case 5:
$d1r = $d1r + $fifth;
break;
case 6:
$d1r = $d1r + $sixth;
break;
case 7:
$d1r = $d1r + $seventh;
break;
case 8:
$d1r = $d1r + $eighth;
break;
}


$d2n = $_POST['d2n'];
$d2lo = $_POST['d2lo'];
$d2lo2 = $_POST['d2lo2'];
$d2lp = $_POST['d2lp'];
$d2lp2 = $_POST['d2lp2'];

$d2r = ($d2lo + $d2lo2) / 2;
switch($d2lp){
case 1:
$d2r = $d2r + $first;
break;
case 2:
$d2r = $d2r + $second;
break;
case 3:
$d2r = $d2r + $third;
break;
case 4:
$d2r = $d2r + $fourth;
break;
case 5:
$d2r = $d2r + $fifth;
break;
case 6:
$d2r = $d2r + $sixth;
break;
case 7:
$d2r = $d2r + $seventh;
break;
case 8:
$d2r = $d2r + $eighth;
break;
}

switch($d2lp2){
case 1:
$d2r = $d2r + $first;
break;
case 2:
$d2r = $d2r + $second;
break;
case 3:
$d2r = $d2r + $third;
break;
case 4:
$d2r = $d2r + $fourth;
break;
case 5:
$d2r = $d2r + $fifth;
break;
case 6:
$d2r = $d2r + $sixth;
break;
case 7:
$d2r = $d2r + $seventh;
break;
case 8:
$d2r = $d2r + $eighth;
break;
}


$d3n = $_POST['d3n'];
$d3lo = $_POST['d3lo'];
$d3lo2 = $_POST['d3lo2'];
$d3lp = $_POST['d3lp'];
$d3lp2 = $_POST['d3lp2'];

$d3r = ($d3lo + $d3lo2) / 2;
switch($d3lp){
case 1:
$d3r = $d3r + $first;
break;
case 2:
$d3r = $d3r + $second;
break;
case 3:
$d3r = $d3r + $third;
break;
case 4:
$d3r = $d3r + $fourth;
break;
case 5:
$d3r = $d3r + $fifth;
break;
case 6:
$d3r = $d3r + $sixth;
break;
case 7:
$d3r = $d3r + $seventh;
break;
case 8:
$d3r = $d3r + $eighth;
break;
}

switch($d3lp2){
case 1:
$d3r = $d3r + $first;
break;
case 2:
$d3r = $d3r + $second;
break;
case 3:
$d3r = $d3r + $third;
break;
case 4:
$d3r = $d3r + $fourth;
break;
case 5:
$d3r = $d3r + $fifth;
break;
case 6:
$d3r = $d3r + $sixth;
break;
case 7:
$d3r = $d3r + $seventh;
break;
case 8:
$d3r = $d3r + $eighth;
break;
}





$d4n = $_POST['d4n'];
$d4lo = $_POST['d4lo'];
$d4lo2 = $_POST['d4lo2'];
$d4lp = $_POST['d4lp'];
$d4lp2 = $_POST['d4lp2'];

$d4r = ($d4lo + $d4lo2) / 2;
switch($d4lp){
case 1:
$d4r = $d4r + $first;
break;
case 2:
$d4r = $d4r + $second;
break;
case 3:
$d4r = $d4r + $third;
break;
case 4:
$d4r = $d4r + $fourth;
break;
case 5:
$d4r = $d4r + $fifth;
break;
case 6:
$d4r = $d4r + $sixth;
break;
case 7:
$d4r = $d4r + $seventh;
break;
case 8:
$d4r = $d4r + $eighth;
break;
}


switch($d4lp2){
case 1:
$d4r = $d4r + $first;
break;
case 2:
$d4r = $d4r + $second;
break;
case 3:
$d4r = $d4r + $third;
break;
case 4:
$d4r = $d4r + $fourth;
break;
case 5:
$d4r = $d4r + $fifth;
break;
case 6:
$d4r = $d4r + $sixth;
break;
case 7:
$d4r = $d4r + $seventh;
break;
case 8:
$d4r = $d4r + $eighth;
break;
}


$d5n = $_POST['d5n'];
$d5lo = $_POST['d5lo'];
$d5lo2 = $_POST['d5lo2'];
$d5lp = $_POST['d5lp'];
$d5lp2 = $_POST['d5lp2'];

$d5r = ($d5lo + $d5lo2) / 2;
switch($d5lp){
case 1:
$d5r = $d5r + $first;
break;
case 2:
$d5r = $d5r + $second;
break;
case 3:
$d5r = $d5r + $third;
break;
case 4:
$d5r = $d5r + $fourth;
break;
case 5:
$d5r = $d5r + $fifth;
break;
case 6:
$d5r = $d5r + $sixth;
break;
case 7:
$d5r = $d5r + $seventh;
break;
case 8:
$d5r = $d5r + $eighth;
break;


}
switch($d5lp2){
case 1:
$d5r = $d5r + $first;
break;
case 2:
$d5r = $d5r + $second;
break;
case 3:
$d5r = $d5r + $third;
break;
case 4:
$d5r = $d5r + $fourth;
break;
case 5:
$d5r = $d5r + $fifth;
break;
case 6:
$d5r = $d5r + $sixth;
break;
case 7:
$d5r = $d5r + $seventh;
break;
case 8:
$d5r = $d5r + $eighth;
break;

}

$d6n = $_POST['d6n'];
$d6lo = $_POST['d6lo'];
$d6lo2 = $_POST['d6lo2'];
$d6lp = $_POST['d6lp'];
$d6lp2 = $_POST['d6lp2'];

$d6r = ($d6lo + $d6lo2) / 2;
switch($d6lp){
case 1:
$d6r = $d6r + $first;
break;
case 2:
$d6r = $d6r + $second;
break;
case 3:
$d6r = $d6r + $third;
break;
case 4:
$d6r = $d6r + $fourth;
break;
case 5:
$d6r = $d6r + $fifth;
break;
case 6:
$d6r = $d6r + $sixth;
break;
case 7:
$d6r = $d6r + $seventh;
break;
case 8:
$d6r = $d6r + $eighth;
break;
}

switch($d6lp2){
case 1:
$d6r = $d6r + $first;
break;
case 2:
$d6r = $d6r + $second;
break;
case 3:
$d6r = $d6r + $third;
break;
case 4:
$d6r = $d6r + $fourth;
break;
case 5:
$d6r = $d6r + $fifth;
break;
case 6:
$d6r = $d6r + $sixth;
break;
case 7:
$d6r = $d6r + $seventh;
break;
case 8:
$d6r = $d6r + $eighth;
break;
}



$d7n = $_POST['d7n'];
$d7lo = $_POST['d7lo'];
$d7lo2 = $_POST['d7lo2'];
$d7lp = $_POST['d7lp'];
$d7lp2 = $_POST['d7lp2'];

$d7r = ($d7lo + $d7lo2) / 2;
switch($d7lp){
case 1:
$d7r = $d7r + $first;
break;
case 2:
$d7r = $d7r + $second;
break;
case 3:
$d7r = $d7r + $third;
break;
case 4:
$d7r = $d7r + $fourth;
break;
case 5:
$d7r = $d7r + $fifth;
break;
case 6:
$d7r = $d7r + $sixth;
break;
case 7:
$d7r = $d7r + $seventh;
break;
case 8:
$d7r = $d7r + $eighth;
break;
}
switch($d7lp2){
case 1:
$d7r = $d7r + $first;
break;
case 2:
$d7r = $d7r + $second;
break;
case 3:
$d7r = $d7r + $third;
break;
case 4:
$d7r = $d7r + $fourth;
break;
case 5:
$d7r = $d7r + $fifth;
break;
case 6:
$d7r = $d7r + $sixth;
break;
case 7:
$d7r = $d7r + $seventh;
break;
case 8:
$d7r = $d7r + $eighth;
break;
}

$d8n = $_POST['d8n'];
$d8lo = $_POST['d8lo'];
$d8lo2 = $_POST['d8lo2'];
$d8lp = $_POST['d8lp'];
$d8lp2 = $_POST['d8lp2'];

$d8r = ($d8lo + $d8lo2) / 2;
switch($d8lp){

case 1:
$d8r = $d8r + $first;
break;
case 2:
$d8r = $d8r + $second;
break;
case 3:
$d8r = $d8r + $third;
break;
case 4:
$d8r = $d8r + $fourth;
break;
case 5:
$d8r = $d8r + $fifth;
break;
case 6:
$d8r = $d8r + $sixth;
break;
case 7:
$d8r = $d8r + $seventh;
break;
case 8:
$d8r = $d8r + $eighth;
break;
}

switch($d8lp2){

case 1:
$d8r = $d8r + $first;
break;
case 2:
$d8r = $d8r + $second;
break;
case 3:
$d8r = $d8r + $third;
break;
case 4:
$d8r = $d8r + $fourth;
break;
case 5:
$d8r = $d8r + $fifth;
break;
case 6:
$d8r = $d8r + $sixth;
break;
case 7:
$d8r = $d8r + $seventh;
break;
case 8:
$d8r = $d8r + $eighth;
break;

}
$picks = array($d1n => $d1r, $d2n => $d2r, $d3n => $d3r, $d4n => $d4r, $d5n => $d5r, $d6n => $d6r, $d7n => $d7r, $d8n => $d8r);
asort($picks);
$dr;
$dn;
$x = 0;
$list = "";
$toppicks = "";

foreach ($picks as $dn => $dr)
{
$x = $x + 1;

if($x < 5){
$toppicks = $toppicks . $dn . ",";
}

$list = $list . "<tr><td>" . $dn . "</td><td>" . $dr . "</td></tr>";
}
echo "<table border=1><tr><td><b><font color=red>TOP PICKS</font>: " . substr($toppicks, 0, -1) . "</td></tr>";
echo $list . "</table>";

}
Echo "</body></HTML>";
?>

Sorry it's long and messy, I really need to create a standardized function for all those switches.


Thanks

tangoforce
07-22-2012, 07:49 PM
With the amount of code you've "dumped n run" with, it would have been a good idea to read the sticky topic about using the [php] tags to wrap your code in.

You've made the effort to use the bold tags so you're clearly capable of using the php tags so why have you decided not to? - It's a direct request from the moderators of this forum because it makes your code so much easier to read.

See the signature for an example of why we like it in the php tags. With the amount of code you've posted, it really would be useful :thumbsup:

Also you say you made some changes to it and now it doesn't work but you haven't told us what this code should do and what is now happening. You've not told us about any error messages etc..

Arcticwarrio
07-22-2012, 08:24 PM
[ php ][ /php ] and


without spaces

cyberhostbd
07-22-2012, 08:28 PM
You may need to read this post http://www.codingforums.com/showthread.php?t=68462. It will show you how to post PHP code on board. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum