Darkpower Posted July 8, 2009 Share Posted July 8, 2009 Need help with my for loop, i think it is the problem. Explanation: If you buy 1 number, everything works fine. But if you buy 2 or more numbers, it duplicates! Like this: You have 100 points to play with. You choose number 1 and 2 and push play, the numbers doesn't match BUT it costs you 40 points when playing, not 20.. Random number is 7. You guessed 1 2 . You have 80 points. CORRECT Random number is 7. You guessed 1 2 1 2. You have 60 points. WRONG, and this is how it looks... <?php include "antet.php"; include "func.php"; if (!isset($_SESSION["user"][1])) { header('Location: login.php'); die(); } $usr=user($_SESSION["user"][0]); ?> <html> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <title>Lottery - Win points</title> <style type="text/css"> body { background: #CCCCCC; } p,input { font-size: 11px; font-family: "Verdana", "Helvetica", "Arial", sans-serif; color: #244189; } label { cursor: pointer; } </style> </head> <body> <?php global $userrow, $db_id; $poang = $userrow["points"]; $skrivut = $gissningar = ''; if($_POST{'ok'}) { $poang = $_POST{'poang'}; $slump = rand(1,9); $skrivut .= 'Random number is <b>'.$slump.'</b>. '; $antal = count($_POST{'siffra'}); if($usr[7] < 10 && $usr[7] >=1){ echo "<b>You don't have enough points to play!</b><br>"; } $plays_requested = $antal; if(($plays_requested * 10) <= $usr[7]){ for($i=1;$i<=$plays_requested;$i++){ if (isset($antal) && !empty($antal)) { foreach($_POST{'siffra'} as $tal) { if(is_numeric($tal)) { $gissningar .= ' '.$tal; if($tal==$slump) { $poang = $userrow['points']+100; $query = "UPDATE users SET points=points+$poang WHERE id='".mysql_real_escape_string($_SESSION["user"]["0"])."' LIMIT 1"; mysql_query($query, $db_id); } else { $poang = $userrow['points']-10; $query = "UPDATE users SET points=points+$poang WHERE id='".mysql_real_escape_string($_SESSION["user"]["0"])."' LIMIT 1"; mysql_query($query, $db_id); } } } } } } else { echo "<b>You don't have enough points to buy ".$plays_requested." number(s).</b>"; } $query="SELECT points FROM users where id='".mysql_real_escape_string($_SESSION["user"]["0"])."' LIMIT 1"; $result = mysql_query($query, $db_id); $myNum = mysql_fetch_array($result); $poang = $myNum[0]; if($gissningar!='') { $skrivut .= 'You guessed <b>'.$gissningar.'</b>. '; } if($poang<=0) { $skrivut .= '<b>You don\'t have any points left!</b>'; } else { $skrivut .= 'You have <b>'.$poang.'</b> points.'; } } echo ' <p>Every number you buy costs 10 points. If you win: Your correct number(10 points) + 100 points.</p> <form action="'.$_SERVER{'PHP_SELF'}.'" method="post"> <p><input type="checkbox" name="siffra[]" id="t1" value="1" /> <label for="t1">1</label> <input type="checkbox" name="siffra[]" id="t2" value="2" /> <label for="t2">2</label> <input type="checkbox" name="siffra[]" id="t3" value="3" /> <label for="t3">3</label> <input type="checkbox" name="siffra[]" id="t4" value="4" /> <label for="t4">4</label> <input type="checkbox" name="siffra[]" id="t5" value="5" /> <label for="t5">5</label> <input type="checkbox" name="siffra[]" id="t6" value="6" /> <label for="t6">6</label> <input type="checkbox" name="siffra[]" id="t7" value="7" /> <label for="t7">7</label> <input type="checkbox" name="siffra[]" id="t8" value="8" /> <label for="t8">8</label> <input type="checkbox" name="siffra[]" id="t9" value="9" /> <label for="t9">9</label> <input type="hidden" name="poang" value="'.$poang.'" /> <input type="submit" name="ok" value="Play" /></p> </form> <p>'.$skrivut.'</p>'; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/165237-something-wrong-with-for-loop/ Share on other sites More sharing options...
ignace Posted July 8, 2009 Share Posted July 8, 2009 You only need to use global if you are within a different scope then the global scope (thus in a function or class) global $userrow, $db_id; $_POST{'ok'} // should be $_POST['ok'] Could you please only post the relevant code and maybe even add some more comments in english. I am not familiar with the language and that makes it harder to follow the logic. You are using { and } to access array's so that should give you some warnings. Add these 2 lines to the top of your script: error_reporting(E_ALL); ini_set('display_errors', TRUE); Quote Link to comment https://forums.phpfreaks.com/topic/165237-something-wrong-with-for-loop/#findComment-871393 Share on other sites More sharing options...
Darkpower Posted July 8, 2009 Author Share Posted July 8, 2009 I have changed everything to english now, so it will make it more easy to find the problem. I have also changed all POST with ['...']. Deleted: global $userrow, $db_id; <?php $points = $userrow["points"]; $Print = $guesses = ''; if($_POST['ok']) { $points = $_POST['points']; $random = rand(1,9); $Print .= 'Random number is <b>'.$random.'</b>. '; $amount = count($_POST['digit']); if($usr[7] < 10 && $usr[7] >=1){ echo "<b>You don't have enough points to play!</b><br>"; } $plays_requested = $amount; if(($plays_requested * 10) <= $usr[7]){ for($i=1;$i<=$plays_requested;$i++){ if (isset($amount) && !empty($amount)) { foreach($_POST['digit'] as $number) { if(is_numeric($number)) { $guesses .= ' '.$number; if($number==$random) { $points = $userrow['points']+100; $query = "UPDATE users SET points=points+$points WHERE id='".mysql_real_escape_string($_SESSION["user"]["0"])."' LIMIT 1"; mysql_query($query, $db_id); } else { $points = $userrow['points']-10; $query = "UPDATE users SET points=points+$points WHERE id='".mysql_real_escape_string($_SESSION["user"]["0"])."' LIMIT 1"; mysql_query($query, $db_id); } } } } } } else { echo "<b>You don't have enough points to buy ".$plays_requested." number(s).</b>"; } $query="SELECT points FROM users where id='".mysql_real_escape_string($_SESSION["user"]["0"])."' LIMIT 1"; $result = mysql_query($query, $db_id); $myNum = mysql_fetch_array($result); $points = $myNum[0]; if($guesses!='') { $Print .= 'You guessed <b>'.$guesses.'</b>. '; } if($points<=0) { $Print .= '<b>You don\'t have any points left!</b>'; } else { $Print .= 'You have <b>'.$points.'</b> points.'; } } echo ' <p>Every number you buy costs 10 points. If you win: Your correct number(10 points) + 100 points.</p> <form action="'.$_SERVER{'PHP_SELF'}.'" method="post"> <p><input type="checkbox" name="digit[]" id="t1" value="1" /> <label for="t1">1</label> <input type="checkbox" name="digit[]" id="t2" value="2" /> <label for="t2">2</label> <input type="checkbox" name="digit[]" id="t3" value="3" /> <label for="t3">3</label> <input type="checkbox" name="digit[]" id="t4" value="4" /> <label for="t4">4</label> <input type="checkbox" name="digit[]" id="t5" value="5" /> <label for="t5">5</label> <input type="checkbox" name="digit[]" id="t6" value="6" /> <label for="t6">6</label> <input type="checkbox" name="digit[]" id="t7" value="7" /> <label for="t7">7</label> <input type="checkbox" name="digit[]" id="t8" value="8" /> <label for="t8">8</label> <input type="checkbox" name="digit[]" id="t9" value="9" /> <label for="t9">9</label> <input type="hidden" name="points" value="'.$points.'" /> <input type="submit" name="ok" value="Play" /></p> </form> <p>'.$Print.'</p>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/165237-something-wrong-with-for-loop/#findComment-871423 Share on other sites More sharing options...
Darkpower Posted July 9, 2009 Author Share Posted July 9, 2009 Anyone :-/? Quote Link to comment https://forums.phpfreaks.com/topic/165237-something-wrong-with-for-loop/#findComment-871899 Share on other sites More sharing options...
ignace Posted July 9, 2009 Share Posted July 9, 2009 When i run your script i get: Notice: Undefined variable: userrow in D:\darkpower.php on line 6 <?php error_reporting(E_ALL); ini_set('display_errors', TRUE); $points = $userrow['points']; $Print = $guesses = ''; if(!empty($_POST) && isset($_POST['ok'])) { $points = $_POST['points']; $random = rand(1,9); $Print .= 'Random number is <b>'.$random.'</b>. '; $amount = count($_POST['digit']); if($usr[7] < 10 && $usr[7] >=1) { echo "<b>You don't have enough points to play!</b><br>"; } $plays_requested = $amount; if(($plays_requested * 10) <= $usr[7]) { for($i=1;$i<=$plays_requested;$i++) { if (isset($amount) && !empty($amount)) { foreach($_POST['digit'] as $number) { if(is_numeric($number)) { $guesses .= ' '.$number; if($number==$random) { $points = $userrow['points']+100; $query = "UPDATE users SET points=points+$points WHERE id='".mysql_real_escape_string($_SESSION['user']['0'])."' LIMIT 1"; mysql_query($query, $db_id); } else { $points = $userrow['points']-10; $query = "UPDATE users SET points=points+$points WHERE id='".mysql_real_escape_string($_SESSION['user']['0'])."' LIMIT 1"; mysql_query($query, $db_id); } } } } } } else { echo "<b>You don't have enough points to buy ".$plays_requested." number(s).</b>"; } $query="SELECT points FROM users where id='".mysql_real_escape_string($_SESSION["user"]["0"])."' LIMIT 1"; $result = mysql_query($query, $db_id); $myNum = mysql_fetch_array($result); $points = $myNum[0]; if($guesses!='') { $Print .= 'You guessed <b>'.$guesses.'</b>. '; } if($points<=0) { $Print .= '<b>You don\'t have any points left!</b>'; } else { $Print .= 'You have <b>'.$points.'</b> points.'; } } echo ' <p>Every number you buy costs 10 points. If you win: Your correct number(10 points) + 100 points.</p> <form action="'.$_SERVER{'PHP_SELF'}.'" method="post"> <p><input type="checkbox" name="digit[]" id="t1" value="1" /> <label for="t1">1</label> <input type="checkbox" name="digit[]" id="t2" value="2" /> <label for="t2">2</label> <input type="checkbox" name="digit[]" id="t3" value="3" /> <label for="t3">3</label> <input type="checkbox" name="digit[]" id="t4" value="4" /> <label for="t4">4</label> <input type="checkbox" name="digit[]" id="t5" value="5" /> <label for="t5">5</label> <input type="checkbox" name="digit[]" id="t6" value="6" /> <label for="t6">6</label> <input type="checkbox" name="digit[]" id="t7" value="7" /> <label for="t7">7</label> <input type="checkbox" name="digit[]" id="t8" value="8" /> <label for="t8">8</label> <input type="checkbox" name="digit[]" id="t9" value="9" /> <label for="t9">9</label> <input type="hidden" name="points" value="'.$points.'" /> <input type="submit" name="ok" value="Play" /></p> </form> <p>'.$Print.'</p>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/165237-something-wrong-with-for-loop/#findComment-871953 Share on other sites More sharing options...
Octave91 Posted July 9, 2009 Share Posted July 9, 2009 Try using echo at regular intervals to chk values, and in the for loop to see if its giving desired results Quote Link to comment https://forums.phpfreaks.com/topic/165237-something-wrong-with-for-loop/#findComment-871957 Share on other sites More sharing options...
Octave91 Posted July 9, 2009 Share Posted July 9, 2009 wat is $userrow['points']; where did this come from Quote Link to comment https://forums.phpfreaks.com/topic/165237-something-wrong-with-for-loop/#findComment-871961 Share on other sites More sharing options...
Darkpower Posted July 9, 2009 Author Share Posted July 9, 2009 You need Db connections.. You are missing my files: <?php include "antet.php"; include "func.php"; Oh, sorry. You need this line: global $userrow; $points = $userrow["points"]; This is place 7 in table users: $usr[7], array for points. Quote Link to comment https://forums.phpfreaks.com/topic/165237-something-wrong-with-for-loop/#findComment-871989 Share on other sites More sharing options...
Darkpower Posted July 9, 2009 Author Share Posted July 9, 2009 I know, some things inside in the FOR loop must change place: for($i=1;$i<=$plays_requested;$i++){ Because, if i haven't used this FOR loop, then could you buy alot of numbers when you have 10 points or above. This FOR loop checks if you have enough points to buy more numbers than one number Quote Link to comment https://forums.phpfreaks.com/topic/165237-something-wrong-with-for-loop/#findComment-872003 Share on other sites More sharing options...
Darkpower Posted July 10, 2009 Author Share Posted July 10, 2009 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/165237-something-wrong-with-for-loop/#findComment-872960 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.