kitten2781 Posted April 17, 2008 Share Posted April 17, 2008 I want to store wins and losses in a php session, but I can't figure out how to do it. Any help is appreciated. ??? Here is my code: <?php session_start(); ?> <html> <head> <title>RPS</title> </head> <body> <?php $player_choice = $_POST['selection']; $game_choice = array("Rock", "Paper", "Scissors"); $rand_key = array_rand($game_choice, 1); $computer_choice = $game_choice[$rand_key]; $rock = $_POST["rock_image"]; $paper = $_POST["paper_image"]; $scissors = $_POST["scissors_image"]; ?> <div align="center"><h2>Rock - Paper - Scissors in PHP</h2></div> <hr> <form action="RPSv1.php" method="post" enctype="multipart/form-data"> Select one object:<br> <input type="radio" name="selection" value="Rock"> Rock<br> <input type="radio" name="selection" value="Paper"> Paper<br> <input type="radio" name="selection" value="Scissors"> Scissors<br> <input type="hidden" name="MAX_FILE_SIZE" value="300000"> <br><input type="submit" value="Play"> <hr> <p><b>Result: <?php echo $result; ?> </b></p> <table border="1" summary=""> <tr> <td> <?php if ($player_choice == "Rock") { echo "<img src='rock.gif' border='0'>"; } else if ($player_choice == "Scissors") { echo "<img src='scissors.gif' border='0'>"; } else if ($player_choice == "Paper"){ echo "<img src='paper.gif' border='0'>"; } else { echo " "; } ?> </td> <td> <?php $losses = 0; $wins = 0; if ($player_choice == "Rock" && $computer_choice == "Paper") echo " Loses to "; $losses++; else if ($player_choice == "Paper" && $computer_choice == "Scissors") echo " Loses to "; $losses++; else if ($player_choice == "Scissors" && $computer_choice == "Rock") echo " Loses to "; $losses++; else if ($player_choice == "Paper" && $computer_choice == "Rock") echo " Beats "; $wins++; else if ($player_choice == "Rock" && $computer_choice == "Scissors") echo " Beats "; $wins++; else if ($player_choice == "Scissors" && $computer_choice == "Paper") echo " Beats "; $wins++; else { echo "Tie."; } ?> </td> <td> <?php if ($computer_choice == "Rock") { echo "<img src='rock.gif' border='0'>"; } else if ($computer_choice == "Scissors") { echo "<img src='scissors.gif' border='0'>"; } else { echo "<img src='paper.gif' border='0'>"; } ?> </td> </tr> </table> <hr> <?php echo $wins, "wins <br>"; echo $losses, "losses <br>"; ?> <hr> </form> <?php session_destroy(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/101593-storing-winslosses-in-session/ Share on other sites More sharing options...
Psycho Posted April 17, 2008 Share Posted April 17, 2008 Here, this uses sessions and the code is much cleaner. <?php session_start(); $choices = array('rock', 'paper', 'scissors'); $result1 = array('Win', 'Loss', 'Tie'); $result2 = array('beats', 'loses to', 'ties'); if (in_array($_POST['selection'],array_keys($choices))) { $player = $_POST['selection']; $computer = array_rand(array_keys($choices)); //Determine the results if ($player == $computer) { //Tie $result = 2; $_SESSION['ties']++; } else if ($player==($computer+1) || ($player==0 && $computer==2)) { //Player wins $result = 0; $_SESSION['wins']++; } else { //Player loses $result = 1; $_SESSION['losses']++; } } ?> <html> <head> <title>RPS</title> </head> <body> <div align="center"><h2>Rock - Paper - Scissors in PHP</h2></div> <hr> <form action="<?php echo $_SERVER[php_SELF]; ?>" method="post" enctype="multipart/form-data"> Select one object:<br> <input type="radio" name="selection" value="0"> Rock<br> <input type="radio" name="selection" value="1"> Paper<br> <input type="radio" name="selection" value="2"> Scissors<br> <br><input type="submit" value="Play"> </form> <hr> <?php if ($result1) { //Display the results ?> <p><b>Result: <?php echo $result1[$result]; ?></b></p> <table border="1" summary=""> <tr> <td> <?php echo ucfirst($choices[$player]); ?><br> <img src="<?php echo $choices[$player]; ?>.gif" border="0"> </td> <td><?php echo $result2[$result]; ?></td> <td> <?php echo ucfirst($choices[$computer]); ?><br> <img src="<?php $choices[$computer]; ?>.gif" border="0"> </td> </tr> </table> <hr> <?php echo (int)$_SESSION['wins']; ?> wins <br> <?php echo (int)$_SESSION['losses']; ?> losses <br> <?php echo (int)$_SESSION['ties']; ?> ties <br> <hr> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/101593-storing-winslosses-in-session/#findComment-519828 Share on other sites More sharing options...
kitten2781 Posted April 18, 2008 Author Share Posted April 18, 2008 Thank you for your help! Quote Link to comment https://forums.phpfreaks.com/topic/101593-storing-winslosses-in-session/#findComment-520523 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.