Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Apr 2013
    Posts
    10
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Do I need to use a database to do this?

    I am making a basic casino type game where you start off with $100, put in a bet and there is a 50% chance of it being doubled.

    The problem I am having is I can only run the code once. Once I try to repeat the operation and essentially carry the user's total cash from one bet to the next, the variables are reset.

    Here is my code. I would really appreciate it if someone could show me how to make it so my game can be replayed and the cash variable is carried over without being reset (if it is possible to do without a database).

    PHP Code:
    <?php
        
    //get data from form
        
        
    $user_cash 100;    
        
    $user_bet $_POST['User_Bet'];
        
    $user_cash $user_cash $user_bet;
        
    //Generate Random Number
        
    $dice_roll rand(0,100);
        
    //Decide if user won
        
    if($dice_roll >= 50){
            
    $user_bet $user_bet*2;
            };
        if(
    $dice_roll 50){
            
    $user_bet 0;
            };
        
    //

        
    $user_cash $user_cash $user_bet;
    ?>

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    You can store the value in the session, so that the next time the page is loaded it will be available in the $_SESSION superglobal.

  • #3
    New Coder
    Join Date
    Apr 2013
    Posts
    10
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Inigoesdr View Post
    You can store the value in the session, so that the next time the page is loaded it will be available in the $_SESSION superglobal.
    Thanks for the answer; could you show me what the code would look like for my specific case?


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •