Jump to content

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/165237-something-wrong-with-for-loop/
Share on other sites

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);

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>';


?>

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>';


?>

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.