0

I made a card game with PHP but there I'm facing some issue.

if ($_SESSION["bet"] != NULL)
{
    echo "Your starting bankroll is: " . $_SESSION["bankroll"] . "<br>";
    echo "Your bet is: " . $_SESSION["bet"] . "<br>";
}

I'm getting an input from the user. The problem is, when the game loads at first, and the user enters an input and clicks submit, the game won't work. The condition ($_SESSION["bet"] != NULL) is giving true and bankroll is not defined.

Is there a way I can set this up properly? Is there some PHP method that can initialize the variable once then only works it on session start, then the rest of the code can take care of how that variable gets updated? The bankroll variable gets initialized if the user clicks submit without anything in it right now, so the game still works but it starts improperly.

if ($_SESSION["bet"] == NULL)
{  
  $_SESSION["bankroll"] = 1000;
}

The bankroll variable gets initialized to 1000 every time user submits a NULL input. I want to change this.

More code... Updating...

session_start();

$_SESSION["bet"] = $_POST["bet"];

echo "<br>";

//print_r($_SESSION);

if ($_SESSION["bet"] != NULL)

{

echo "Your starting bankroll is: " . $_SESSION["bankroll"] . "<br>";

echo "Your bet is: " . $_SESSION["bet"] . "<br>";

}


if ($_SESSION["bet"] == NULL)

{  

  $_SESSION["bankroll"] = 1000;

}


else if ($_SESSION["bet"] > 1000 || $_SESSION["bet"] < 0)

{  

   echo " Please enter between 0 and 1000.";

}


else if ($_SESSION["bet"] > $_SESSION["bankroll"])

{

  echo "You can't enter more than what you have.";

}

else

{

$deck = array();

for($x = 0; $x < 54; $x++) {

    $deck[$x] = $x;

}

shuffle($deck);

//Then more stuff.  This one for example...

    if(($houseSuits[0] == -100) || ($houseSuits[1] == -100) || ($houseSuits[2] == -100) || ($houseSuits[3] == -100))
         {
            echo "<br>";
            echo '<center> PLAYER WINS! (HOUSE HAS JOKER) </center>';
            echo "<br>";
            $_SESSION["bankroll"] = $_SESSION["bankroll"] + $_SESSION["bet"]; //THESE NEED TO BE ADDRESSED.
         }

I JUST WANT TO FIND A WAY TO INITIALIZE BANKROLL TO 1000 AT START. THE WAY I'M DOING IT IS BY SUBMITTING A NULL VALUE THEN ASSUMING USER NEVER SUBMITS NULL VALUE AGAIN.

I WOULD LIKE BANKROLL TO BE INITIALIZED TO 1000, THEN FOR THE GAME TO TAKE CARE OF HOW BANKROLL GETS UPDATED.

I FOUND A WAY TO DO IT BUT IT'S NOT A PROPER WAY SO THAT'S WHY I'M ASKING FOR HELP.

THANK YOU.

Vinay
  • 7,442
  • 6
  • 25
  • 48
Fortunata
  • 37
  • 7
  • You need to show some more relevant code. How do you initialise sessions? What is the first code that happens? – James May 19 '18 at 16:41
  • Perhaps my issue isn't with sessions... It's just every time a user submits an empty value, bankroll gets set to 1000. I want to change this... And the game won't start unless user enters an empty (or null) value. – Fortunata May 19 '18 at 16:42
  • I think it is enough information, the game is around 300 lines of code. I just don't want bankroll to get reset to 1000 every time user enters nothing, and I want the game to start when user enters a valid input. – Fortunata May 19 '18 at 16:44
  • without seeing your code I have no idea how you are doing this. You need some way to initialise a user, their bankroll set to whatever you give. Then when they make a bet it reduces their bankroll. seems you are mixing all of this functionality up when it's separate. – James May 19 '18 at 16:45
  • I updated it... I wish I can fix this small error, it would complete this project 100% – Fortunata May 19 '18 at 16:53

1 Answers1

0

Ok try this.

So if the bankroll is not set, then set it, once it's set it wont get set again because it's set.

Then any conditions after are fine.

session_start();

// Initialise bankroll if not already
if (!isset($_SESSION['bankroll'])) {
    $_SESSION['bankroll'] = 1000;
    echo "Your starting bankroll is: " . $_SESSION["bankroll"] . "<br>";
}

$_SESSION['bet'] = $_POST['bet'];

if ($_SESSION['bet'] != NULL) {
    echo "Your bet is: " . $_SESSION['bet'] . "<br>";
}


if ($_SESSION['bet'] > 1000 || $_SESSION['bet'] < 0) {

    echo " Please enter between 0 and 1000.";

} elseif ($_SESSION['bet'] > $_SESSION['bankroll']) {

    echo "You can't enter more than what you have.";

} else {
    // Your card game stuff
}

Notes: you may have issues with using session as it'll store their bet wherever they are, so issuing a bet sets the session, then navigate elsewhere and come back and their bet will still be as before. Maybe this doesn't matter.
You also might not want to check if the bet is null, more perhaps empty or whatever. Test either way to be sure.

James
  • 4,644
  • 5
  • 37
  • 48
  • Testing it, it may be working... I'll update ... Let me see – Fortunata May 19 '18 at 17:09
  • What might be useful is an else to where it sets it to instead show "your current bankroll is now". So first time sets it and states "start bankroll is" then just lets them know what it is now (after bets are removed etc) – James May 19 '18 at 17:11
  • 1
    Thanks a lot for your help, this is much better than the way I coded it, idk why I did not consider isset.. I wrote this several years ago and just wanted to polish it up now. Wish I can ask you more questions though, thanks again! – Fortunata May 19 '18 at 17:26
  • Can't give you check mark because I'm too new here. – Fortunata May 19 '18 at 17:32
  • Yeah no worries :)( glad to help. You can accept the answer though if you want to :) – James May 19 '18 at 18:04