jpbellavance Posted October 4, 2009 Share Posted October 4, 2009 I am trying to learn PHP on my own by creating a card game we all know as WAR. For right now I can get the code to randomly pick a number and then assign that number to a card within the deck. After that the card is assigned a numerical value to be weighed against the computer's card. It seems to work correctly but when I draw repeatedly from the deck it may work, it may not. When I use Safari it works better than when I use Explorer or Firefox. Also sometimes the banner gets deleted and only the Alt Text is displayed. I have added the code below and hope someone will be able to look at it. I have deleted some of the code related to the assigning of values to the card as it is only repeated over and over again. I am sorry I submitted this twice but I have changed the code a fair amount compared to the first post and I was not able to delete my other one. THanks, JPB <?php include ('header.html'); if (isset($_POST['submitted'])){//User did click. $twocards = create_newdeck(); echo ' <table width = "400" align="center"> <tr> <td width="200" align="center"><b>Computers Card</b></td> <td width="200" align="center"><b>Users Card</b></td> </tr> <tr> <td width="200" align="center">' . $twocards[0] . '</td> <td width="200" align="center">' . $twocards[2] . '</td> </tr> <tr> <td width="200" align="center">Card Value: ' . $twocards[1] . '</td> <td width="200" align="center">Card Value: ' . $twocards[3] . '</td> </tr> </table>'; }else{ // USER DID NOT CLICK. echo '<p align="center"><b>Please draw a card.</b></p>'; }//END OF if (isset($_POST['submitted'])) statement ?> <form method = "post" = "war.php"> <table width="770" align="center" cellpadding="5" cellspacing="5"> <tr> <td align="center"><input type="submit" name="submit" value="Draw"/></td> </tr> </table> <input type="hidden" name="submitted" value="TRUE"/> </form>'; <?php include('footer.html'); ?> <?php function create_newdeck(){ $randomcard1 = rand(1,56); $randomcard2 = rand(1,56); $deck = array(1=> '1C', '2C', '3C', '4C', '5C', '6C', '7C', '8C', '9C', '10C', 'JC', 'QC', 'KC', 'AC', '1S', '2S', '3S', '4S', '5S', '6S', '7S', '8S', '9S', '10S', 'JS', 'QS', 'KS', 'AS', '1H', '2H', '3H', '4H', '5H', '6H', '7H', '8H', '9H', '10H', 'JH', 'QH', 'KH', 'AH', '1D', '2D', '3D', '4D', '5D', '6D', '7D', '8D', '9D', '10D', 'JD', 'QD', 'KD', 'AD'); //MAKE SURE THE SAME CARD CANNOT BE DRAWN. while ($randomcard1 == $randomcard2){//GET NEW CARD $randomcard2 = rand(1,56); } $usercard = $deck[$randomcard1]; $usercardvalue = getcardvalue($usercard); $computercard = $deck[$randomcard2]; $computercardvalue = getcardvalue($computercard); $cardarray = array($usercard, $usercardvalue, $computercard, $computercardvalue); return $cardarray; }//END OF FIRST IF. function getcardvalue($card) { //ASSIGN VALUE TO CARD switch ($card) { case '1C': $value = 1; return($value); break; case '1S': $value = 1; return($value); break; case '1H': $value = 1; return($value); break; case '1D': $value = 1; return($value); break; case '2C': $value = 2; return($value); break; case '2S': $value = 2; return($value); break; case '2H': $value = 2; return($value); break; case '2D': $value = 2; return($value); break; case '3C': $value =3; return($value); break; case '3S': $value = 3; return($value); break; case '3H': $value = 3; return($value); break; }//END OF SWITCH }//END OF FUNCTION ?> Quote Link to comment https://forums.phpfreaks.com/topic/176430-solved-please-help-php-code-works-intermittently/ Share on other sites More sharing options...
mikesta707 Posted October 4, 2009 Share Posted October 4, 2009 Can you describe what happens when it doesn't work? do the cards not change? blank page? doesn't output anything? Quote Link to comment https://forums.phpfreaks.com/topic/176430-solved-please-help-php-code-works-intermittently/#findComment-929976 Share on other sites More sharing options...
jpbellavance Posted October 4, 2009 Author Share Posted October 4, 2009 I am sorry. You can see what happens right here. What happens is different based upon the browser. In Safari it seems to work OK. In IE and Firefox it may work twice. http://www.battlescapes.com/CardGame/war.php Thanks for responding! Quote Link to comment https://forums.phpfreaks.com/topic/176430-solved-please-help-php-code-works-intermittently/#findComment-929978 Share on other sites More sharing options...
mikesta707 Posted October 4, 2009 Share Posted October 4, 2009 Im in opera, and it works perfectly fine... Quote Link to comment https://forums.phpfreaks.com/topic/176430-solved-please-help-php-code-works-intermittently/#findComment-929982 Share on other sites More sharing options...
jpbellavance Posted October 4, 2009 Author Share Posted October 4, 2009 Any ideas why it might not work in Firefox or IE? Also there was a typo in the code I submitted that was fixed soon after I posted in regards to the method="post" part of the form. Thanks, JPB Quote Link to comment https://forums.phpfreaks.com/topic/176430-solved-please-help-php-code-works-intermittently/#findComment-929985 Share on other sites More sharing options...
mikesta707 Posted October 4, 2009 Share Posted October 4, 2009 Hmm interested. Yeah just checked out it in Firefox. That doesn't really make sense since PHP is parsed by the server, so the browser doesn't really have anything to do with PHP. It seems as if it doesn't think you are submitting the post. Here try instead of this if (isset($_POST['submitted'])){//User did click. you just check that the submit button is checked, IE if (isset($_POST['submit'])){//User did click. Quote Link to comment https://forums.phpfreaks.com/topic/176430-solved-please-help-php-code-works-intermittently/#findComment-929989 Share on other sites More sharing options...
jpbellavance Posted October 4, 2009 Author Share Posted October 4, 2009 I think I have the solution. Because I am using a free hosting account with my hosting service they place this junk above page with their logo and ad info. When I moved it to a paid service I use with no company banners or such it worked perfectly. Try it here now. http://www.killerbeewebdesign.com/CardGame/war.php I wonder how it could interfere with the PHP? I would like to thank you for your help anyway. I was pulling my hair out for 4 hours dropping code here and there trying to figure it out when all along it was because of the hosting service. Virtual beer on me. Thanks, JPB Quote Link to comment https://forums.phpfreaks.com/topic/176430-solved-please-help-php-code-works-intermittently/#findComment-929992 Share on other sites More sharing options...
mikesta707 Posted October 4, 2009 Share Posted October 4, 2009 hmm im not sure how that junk would affect the PHP, but if you fixed it awesome. make sure you click the topic solved button at the bottom of the post. woot for virtual beer. cheers Quote Link to comment https://forums.phpfreaks.com/topic/176430-solved-please-help-php-code-works-intermittently/#findComment-929998 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.