Mutley Posted April 6, 2007 Share Posted April 6, 2007 This is driving me crazy, it makes no sense. I select a number out of a database called "$n_slot3", the number, is '-1'. I then add, this number to "$p1_slot3", which is the number '3'. Now: 3 + -1 = 2 My sum is with variables: $p1_total3 = $pl_slot3 + $n_slot3; Now for some reason, when I echo "$p1_total3"; it says '-1'. When I echo "$p1_slot3 + $n_slot3 = $p1_total3"; it gives me -12! For the life of me I can't figure out this at all, my numbers are correct but when the variables add up and I echo them they are completly wrong. What is even stranger is, the other sums I do using the same method work fine. Here is my code: $count=0; $sql = "SELECT p.points FROM products p INNER JOIN loadout o ON p.prod_id = o.prod_id WHERE o.user_id = '$player1' ORDER BY o.slot DESC"; $res = mysql_query($sql) or die (mysql_error()."<p>$sql</p>"); while (list($points) = mysql_fetch_row($res)) { $newVar = "p1_slot_mixed" . $count; $$newVar = $points; $count++; } // Because slot 'car' is not a number the variables need sorting in order $p1_slot0 = $p1_slot_mixed0;// Car $p1_slot1 = $p1_slot_mixed5; $p1_slot2 = $p1_slot_mixed4; $p1_slot3 = $p1_slot_mixed3; $p1_slot4 = $p1_slot_mixed2; $p1_slot5 = $p1_slot_mixed1; $track_id = 1; $result16 = mysql_query("SELECT neg_slot1, neg_slot2, neg_slot3, neg_slot4, neg_slot5 FROM tracks WHERE track_id = '$track_id' LIMIT 1"); while($row16 = mysql_fetch_array( $result16 )) { $n_slot1 = $row16['neg_slot1']; $n_slot2 = $row16['neg_slot2']; $n_slot3 = $row16['neg_slot3']; $n_slot4 = $row16['neg_slot4']; $n_slot5 = $row16['neg_slot5']; } $p1_total1 = $p1_slot1 + $n_slot1; $p1_total2 = $p1_slot2 + $n_slot2; $p1_total3 = $n_slot3 + $pl_slot3; echo "$p1_total3<br />"; echo "$p1_slot3 + $n_slot3 = $p1_total3"; $p1_total4 = $p1_slot4 + $n_slot4; $p1_total5 = $p1_slot5 + $n_slot5; Any ideas? ??? Link to comment https://forums.phpfreaks.com/topic/45945-solved-using-variables-for-maths-puzzle/ Share on other sites More sharing options...
per1os Posted April 6, 2007 Share Posted April 6, 2007 Try casting the variable before adding $p1_total3 = $pl_slot3 + (int)$n_slot3; See what happens. Link to comment https://forums.phpfreaks.com/topic/45945-solved-using-variables-for-maths-puzzle/#findComment-223172 Share on other sites More sharing options...
Mutley Posted April 6, 2007 Author Share Posted April 6, 2007 Didn't effect it Frost. Link to comment https://forums.phpfreaks.com/topic/45945-solved-using-variables-for-maths-puzzle/#findComment-223175 Share on other sites More sharing options...
per1os Posted April 6, 2007 Share Posted April 6, 2007 $p1_total3 = ($pl_slot3 + (int)$n_slot3); Maybe try the parans. Link to comment https://forums.phpfreaks.com/topic/45945-solved-using-variables-for-maths-puzzle/#findComment-223177 Share on other sites More sharing options...
Mutley Posted April 6, 2007 Author Share Posted April 6, 2007 Nope, here are my numbers: echo "$p1_slot1, $p1_slot2, $p1_slot3, $p1_slot4, $p1_slot5"; 2, 5, 3, 2, 2 echo "$n_slot1, $n_slot2, $n_slot3, $n_slot4, $n_slot5"; -9, 1, -1, -3, 3 $p1_total3 = $p1_slot3 + $n_slot3; Should be: 3 + -1 = 2 But is: = -1 Link to comment https://forums.phpfreaks.com/topic/45945-solved-using-variables-for-maths-puzzle/#findComment-223190 Share on other sites More sharing options...
Mutley Posted April 6, 2007 Author Share Posted April 6, 2007 No matter what number I set $p1_slot3 to it still equals '-1'. Considering I'm taking that away, maybe it defaults to 0 some how? EDIT: Solved, simply a "L" instead of a one. Link to comment https://forums.phpfreaks.com/topic/45945-solved-using-variables-for-maths-puzzle/#findComment-223196 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.