Jump to content

Need help with if statement..


Darkpower

Recommended Posts

Hey!

 

I have an IF statement who checks if you have 10 points or not, but if you have 10 then you can play. But every number costs 10 points, let say that you have 20 points to play with and you are trying to choose 3 numbers (30 points), then you can also play! How can I do an IF statement who checks that you can only buy so much number you can afford. I want to prevent this.

 

$usr[7] = points in table users.

 

My code:

<?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){
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 need at least 10 points to play!</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/164848-need-help-with-if-statement/
Share on other sites

$points = $usr[7]; // = 20 for sake of example
if($points < 10){
    echo "Not enough points";
}
$plays_requested = 3; // for sake of example
if(($plays_requested * 10) <= $points){
   for($i=1;$i<=$plays_requested;$i++){
        // play game
   }
} else {
    echo "You don't have enough points to play ".$plays_requested." games";
}

Now I have implemented the code, but it says when I choose 1 number:

You don't have enough points to buy 1 number(s). But I have 100 points to play with.

I think it has to do with the for loop or something I have done wrong with.

 

 

Code

<?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) <= $points){
   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>

I found the solution: $points needs to change to $usr[7].

 

But have another problem, 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...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.