Jump to content

[SOLVED] Please Help. PHP code works intermittently.


jpbellavance

Recommended Posts

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                   
?>

Link to comment
Share on other sites

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. 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.