JackJack Posted March 31, 2006 Share Posted March 31, 2006 What PHP code could I use to count something like wins and loses? Quote Link to comment Share on other sites More sharing options...
keithschm Posted March 31, 2006 Share Posted March 31, 2006 [!--quoteo(post=360454:date=Mar 31 2006, 02:35 PM:name=JackJack)--][div class=\'quotetop\']QUOTE(JackJack @ Mar 31 2006, 02:35 PM) [snapback]360454[/snapback][/div][div class=\'quotemain\'][!--quotec--]What PHP code could I use to count something like wins and loses?[/quote]well where is the data?and how is it getting there? Quote Link to comment Share on other sites More sharing options...
JackJack Posted March 31, 2006 Author Share Posted March 31, 2006 Basically I would like to have it so that everytime you win lose or draw it adds to your totaland when you go off the webpage it resets your count or won, lost and drawnHere is the code <?php$computer = rand(1,3);{if ($computer == 1) $choice = 'Rock' ;if ($computer == 2) $choice = 'Paper' ;if ($computer == 3) $choice = 'Scissors' ;}$action = $_GET['action'];if ($action == 'Rock' && $choice == 'Rock') {echo "<html><center><a href='http://www.battlemachines.net/test/rps.php?action=Rock'><img src='http://www.battlemachines.net/test/Rock.bmp'></a><a href='http://www.battlemachines.net/test/rps.php?action=Paper'><img src='http://www.battlemachines.net/test/Paper.bmp'></a><a href='http://www.battlemachines.net/test/rps.php?action=Scissors'><img src='http://www.battlemachines.net/test/Scissors.bmp'></a><br>You Choose <b>$action</b> vs. Computer Choose <b>$choice</b> <br> No One Won</html>";exit; }if ($action == 'Rock' && $choice == 'Paper') {echo "<html><center><a href='http://www.battlemachines.net/test/rps.php?action=Rock'><img src='http://www.battlemachines.net/test/Rock.bmp'></a><a href='http://www.battlemachines.net/test/rps.php?action=Paper'><img src='http://www.battlemachines.net/test/Paper.bmp'></a><a href='http://www.battlemachines.net/test/rps.php?action=Scissors'><img src='http://www.battlemachines.net/test/Scissors.bmp'></a><br>You Choose <b>$action</b> vs. Computer Choose <b>$choice</b> <br> You Lost</html>";exit; }if ($action == 'Rock' && $choice == 'Scissors') {echo "<html><center><a href='http://www.battlemachines.net/test/rps.php?action=Rock'><img src='http://www.battlemachines.net/test/Rock.bmp'></a><a href='http://www.battlemachines.net/test/rps.php?action=Paper'><img src='http://www.battlemachines.net/test/Paper.bmp'></a><a href='http://www.battlemachines.net/test/rps.php?action=Scissors'><img src='http://www.battlemachines.net/test/Scissors.bmp'></a><br>You Choose $action vs. Computer Choose <b>$choice</b> <br> You Won</html>";exit; }if ($action == 'Paper' && $choice == 'Rock') {echo "<html><center><a href='http://www.battlemachines.net/test/rps.php?action=Rock'><img src='http://www.battlemachines.net/test/Rock.bmp'></a><a href='http://www.battlemachines.net/test/rps.php?action=Paper'><img src='http://www.battlemachines.net/test/Paper.bmp'></a><a href='http://www.battlemachines.net/test/rps.php?action=Scissors'><img src='http://www.battlemachines.net/test/Scissors.bmp'></a><br>You Choose <b>$action</b> vs. Computer Choose <b>$choice</b> <br> You Won";exit; }if ($action == 'Paper' && $choice == 'Scissors') {echo "<html><center><a href='http://www.battlemachines.net/test/rps.php?action=Rock'><img src='http://www.battlemachines.net/test/Rock.bmp'></a><a href='http://www.battlemachines.net/test/rps.php?action=Paper'><img src='http://www.battlemachines.net/test/Paper.bmp'></a><a href='http://www.battlemachines.net/test/rps.php?action=Scissors'><img src='http://www.battlemachines.net/test/Scissors.bmp'></a><br>You Choose <b>$action</b> vs. Computer Choose <b>$choice</b> <br> You Lost";exit; }if ($action == 'Paper' && $choice == 'Paper') {echo "<html><center><a href='http://www.battlemachines.net/test/rps.php?action=Rock'><img src='http://www.battlemachines.net/test/Rock.bmp'></a><a href='http://www.battlemachines.net/test/rps.php?action=Paper'><img src='http://www.battlemachines.net/test/Paper.bmp'></a><a href='http://www.battlemachines.net/test/rps.php?action=Scissors'><img src='http://www.battlemachines.net/test/Scissors.bmp'></a><br>You Choose <b>$action</b> vs. Computer Choose <b>$choice</b> <br> No One Won";exit; }if ($action == 'Scissors' && $choice == 'Rock') {echo "<html><center><a href='http://www.battlemachines.net/test/rps.php?action=Rock'><img src='http://www.battlemachines.net/test/Rock.bmp'></a><a href='http://www.battlemachines.net/test/rps.php?action=Paper'><img src='http://www.battlemachines.net/test/Paper.bmp'></a><a href='http://www.battlemachines.net/test/rps.php?action=Scissors'><img src='http://www.battlemachines.net/test/Scissors.bmp'></a><br>You Choose <b>$action</b> vs. Computer Choose <b>$choice</b> <br> You Lost";exit; }if ($action == 'Scissors' && $choice == 'Paper') {echo "<html><center><a href='http://www.battlemachines.net/test/rps.php?action=Rock'><img src='http://www.battlemachines.net/test/Rock.bmp'></a><a href='http://www.battlemachines.net/test/rps.php?action=Paper'><img src='http://www.battlemachines.net/test/Paper.bmp'></a><a href='http://www.battlemachines.net/test/rps.php?action=Scissors'><img src='http://www.battlemachines.net/test/Scissors.bmp'></a><br>You Choose <b>$action</b> vs. Computer Choose <b>$choice</b> <br> You Won";exit; }if ($action == 'Scissors' && $choice == 'Scissors') {echo "<html><center><a href='http://www.battlemachines.net/test/rps.php?action=Rock'><img src='http://www.battlemachines.net/test/Rock.bmp'></a><a href='http://www.battlemachines.net/test/rps.php?action=Paper'><img src='http://www.battlemachines.net/test/Paper.bmp'></a><a href='http://www.battlemachines.net/test/rps.php?action=Scissors'><img src='http://www.battlemachines.net/test/Scissors.bmp'></a><br>You Choose <b>$action</b> vs. Computer Choose <b>$choice</b> <br> No One Won";exit; }?><html><body link="#000000" vlink="#000000" alink="#000000"><center><a href='http://www.battlemachines.net/test/rps.php?action=Rock'><img src='http://www.battlemachines.net/test/Rock.bmp'></a><a href='http://www.battlemachines.net/test/rps.php?action=Paper'><img src='http://www.battlemachines.net/test/Paper.bmp'></a><a href='http://www.battlemachines.net/test/rps.php?action=Scissors'><img src='http://www.battlemachines.net/test/Scissors.bmp'></a></html>Thank You Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted April 1, 2006 Share Posted April 1, 2006 Have a look at sessions. Just create three session win, lose and draw. Then just add a single line of code:[code]$_SESSION['win'] += 1;[/code] when the user wins or do [code]$_SESSION['lose'] += 1;[/code] or [code]$_SESSION['draw'] += 1;[/code] when the user draws.I ahve implemented this into your script, aswell as recode it. You'll soon see the difference and your code is alot more slimmer than before! Heres you new code:[code]<?phpsession_start();?><html><body link="#000000" vlink="#000000" alink="#000000"><center><?php//first check that our wins, loses and draws sessions aren't set so we dont reset them everytime we play agaion as we want to//keep our total wins, loses and drawsif(!isset($_SESSION['wins']) || !isset($_SESSION['loses']) || !isset($_SESSION['draws'])){ $_SESSION['wins'] = 'no'; $_SESSION['loses'] = 'no'; $_SESSION['draws'] = 'no';}$computer = rand(1,3);if ($computer == 1) $choice = 'Rock';if ($computer == 2) $choice = 'Paper';if ($computer == 3) $choice = 'Scissors';//This here is a normal if/else statement but its just an inline if/else statment$action = (isset($_GET['action']) ? $_GET['action'] : '');if ($action == 'Rock' && $choice == 'Rock') { echo "You Choose $action vs. Computer Choose $choice<br />No One Won"; //we drew here so lets increse the draws sesion by one $_SESSION['draws'] += 1;}elseif ($action == 'Rock' && $choice == 'Paper') { echo "You Choose <b>$action</b> vs. Computer Choose <b>$choice</b> <br> You Lost"; //we lost here so lets increse the loses sesion by one $_SESSION['loses'] += 1;}elseif ($action == 'Rock' && $choice == 'Scissors') { echo "You Choose $action vs. Computer Choose <b>$choice</b> <br> You Won</html>"; //we won here so lets increse the wins sesion by one $_SESSION['wins'] += 1;}elseif ($action == 'Paper' && $choice == 'Rock') { echo "You Choose <b>$action</b> vs. Computer Choose <b>$choice</b><br> You Won"; $_SESSION['wins'] += 1;}elseif ($action == 'Paper' && $choice == 'Scissors') { echo "You Choose <b>$action</b> vs. Computer Choose <b>$choice</b> <br> You Lost"; $_SESSION['loses'] += 1;}elseif ($action == 'Paper' && $choice == 'Paper') { echo "You Choose <b>$action</b> vs. Computer Choose <b>$choice</b> <br> No One Won"; $_SESSION['draws'] += 1;}elseif ($action == 'Scissors' && $choice == 'Rock') { echo "You Choose <b>$action</b> vs. Computer Choose <b>$choice</b> <br> You Lost"; $_SESSION['loses'] += 1;}elseif ($action == 'Scissors' && $choice == 'Paper') { echo "You Choose <b>$action</b> vs. Computer Choose <b>$choice</b> <br> You Won"; //we won here so lets increse the wins sesion by one $_SESSION['wins'] += 1;}elseif ($action == 'Scissors' && $choice == 'Scissors') { echo "You Choose <b>$action</b> vs. Computer Choose <b>$choice</b> <br> No One Won"; $_SESSION['loses'] += 1;}echo "<br /><hr><br />";echo "You have won a total of <b>" . $_SESSION['wins'] . "</b> times<br />";echo "You have lost a total of <b>" . $_SESSION['loses'] . "</b> times<br />";echo "You have drew a total of <b>" . $_SESSION['draws'] . "</b> times<br /><br />";?><a href="?action=Rock"><img src="Rock.bmp"></a><a href="?action=Paper"><img src="Paper.bmp"></a><a href="?action=Scissors"><img src="Scissors.bmp"></a></html>[/code] Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 1, 2006 Share Posted April 1, 2006 I refined the last poster's code a little to use arrays (and some embellishments using CSS):[code]<?phpsession_start();$rps = array('Rock','Paper','Scissors');$wins = array('Rock vs Scissors','Scissors vs Paper', 'Paper vs Rock');$loses = array('Rock vs Paper','Paper vs Scissors','Scissors vs Rock');?>< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">< html><head><title>Rock, Paper, Scissors</title><style type="text/css" media="screen">body, html { font-size: 100%; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; margin: 0; padding: 0;}#hdr { display: block; width: 95%; margin-left: auto; margin-right: auto; padding-top: .5em; padding-bottom: .5em; border-bottom: 1px solid black;}#rop { display: block; width: 95%; margin-left: auto; margin-right: auto; margin-top: 1em;}h1 { text-align: center; padding: 0; margin: 0;}a { text-decoration: none; color: Red; background-color: White; font-weight: bold;}a:hover { color: white; background-color: Red;}.bold { font-weight: bold;}.win { color: Lime; font-weight: bold;}.lost { color: Red; font-weight: bold;}.draw { color: Black; font-weight: bold;}</style></head><body><div id="hdr"><h1>Rock, Paper, Scissors</h1></div><div id="rop"><?php//first check that our wins, loses and draws sessions aren't set so we dont reset them everytime we play agaion as we want to//keep our total wins, loses and drawsif(!isset($_SESSION['wins']) || !isset($_SESSION['loses']) || !isset($_SESSION['draws'])){ $_SESSION['wins'] = 0; // these should be set to zero, not 'no'; $_SESSION['loses'] = 0; $_SESSION['draws'] = 0;}$computer = rand(0,2);$choice = $rps[$computer];//This here is a normal if/else statement but its just an inline if/else statment$action = (isset($_GET['action']) ? $_GET['action'] : '');if ($action != '') { // don't do the check if the user didn't supply an action echo "You Choose <span style='bold'>$action</span> vs. Computer Choose <span style='bold'>$choice</span>"; if ($action == $choice) { // we have a draw $_SESSION['draws']++; $cls = 'draw'; $str = 'No one won'; } if (in_array($action . ' vs ' . $choice, $wins)) { // User wins $_SESSION['wins']++; $cls = 'win'; $str = "You Won"; } if (in_array($action . ' vs ' . $choice, $loses)) { // Computer wins $_SESSION['loses']++; $cls = 'lost'; $str = "You Lost"; } echo '<br /><span class="' . $cls . '">' . $str . '</span>';echo "<br /><hr><br />";echo "You have won a total of <b>" . $_SESSION['wins'] . "</b> times<br />";echo "You have lost a total of <b>" . $_SESSION['loses'] . "</b> times<br />";echo "You have drew a total of <b>" . $_SESSION['draws'] . "</b> times<br /><br />";}?>Pick one:<a href="?action=Rock">Rock</a> <a href="?action=Paper">Paper</a> <a href="?action=Scissors">Scissors</a></div></body></html>[/code]If you look at the basic code, it has been shortened considerably.I tested this and it works. On the DOCTYPE and following line, remove the space after the "<" which I had to put in to get the code to post correctly. :-(Ken Quote Link to comment 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.