I'm learning PHP and need a little help please

Discussion in 'PHP' started by maplater, Jun 7, 2010.

  1. #1
    Hi, I'm teaching myself PHP and am trying to make this really simple game of just who can guess a randomized number between 0-10 first, the user or the computer. Though I can't seem to get it to work, it just shows the text and form and when you click submit, seemingly nothing happens. Here's the code:

    <html>
    <head>
    </head>
    <body>

    Let's Play A Game! You have to guess the number 1-10 before the computer does, you go first:

    <?php
    $random = rand(0,10);
    $win = 0

    do {
    ?>
    <form action="guessnumber.html" method="post">
    Enter a number 0-10: <input name="number" type="text">
    <input name="submit" type="submit">

    <?php
    if (isset($_POST['submit'])) {

    $number = $_POST['number'];
    if ($number != $random) {
    $comp = rand(0,10);
    $win = ($comp == $random);
    echo "The computer choose: $comp";
    }
    else {
    $mewin = 2;
    }
    } while ($win = 0);
    if ($win = 2) {
    echo "You Win!!";
    }
    else {
    echo "The Computer Won :("
    }

    ?>

    </body>
    </html>

    Any help please?
     
    maplater, Jun 7, 2010 IP
  2. Lam3r

    Lam3r Active Member

    Messages:
    235
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Seems to me you are a bit confused about the logic of the game in general. Let me show an example similar to yours, though I'll need to change some of the code to make it functional, and I'm just going to make it one round, since that's much simpler than making a continuous game like you're trying to do. If you want a more advanced example, let me know and I'll reply with one :D.

    <html>
    <head>
    </head>
    <body>
    
    Let's Play A Game! You have to guess the number 1-10 before the computer does, you go first:
    
    <?php
    $random = rand(0,10);
    $win = 0;
    ?>
    <form method="post">
    Enter a number 0-10: <input name="number" type="text">
    <input name="submit" type="submit">
    
    <?php
    if (isset($_POST['submit'])) {
    	$number = $_POST['number'];
    	if ($number != $random) {
    		echo "You lost! You guessed $number while the generated number was $random!";
    	}
    }
    else {
    	echo "You won! The correct number was $random!"
    }
    
    ?>
    
    </body>
    </html>
    PHP:
     
    Lam3r, Jun 7, 2010 IP
  3. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Try this. Also some hosting packages don't parse php if your extension is .html

    <html> 
    <head> 
    </head> 
    <body> 
    
    <p>Let's Play A Game! You have to guess the number 1-10 before the computer does, you go first:</p>
    
    <?php
    $random = rand(0,10); 
    $win = 0;
    ?>
    <form action="guessnumber.html" method="post">
    Enter a number 0-10: <input name="number" id="number" type="text">
    <input name="submit" type="submit">
    
    <?php
    if (isset($_POST['submit'])) {
    
    $number = $_POST['number'];
    if ($number != $random) 
    {
    $comp = rand(0,10);
    if($comp == $random){ $win = 2; }
    echo "The computer choose: $comp";
    }
    else
     {
    $win = 1;
    }
    if ($win == 1) {
    echo "You Win!!";
    }
    if($win == 2)
    {
    echo "The Computer Won "
    }
    }
    
    ?>
    </body>
    </html>
    
    Code (markup):
     
    Imozeb, Jun 7, 2010 IP
  4. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #4
    Also some hosting packages don't parse php if your extension is .html

    I have never heard that
    Every server have apache installed will works atmost independent to extension
     
    roopajyothi, Jun 8, 2010 IP
  5. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I'm using godaddy servers and the php code doesn't seem to be working when I try to run the file with a .html extension. I think they said I have to do something in my .htaccess file (addHandler)
     
    Imozeb, Jun 8, 2010 IP
  6. Scoty

    Scoty Active Member

    Messages:
    620
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    60
    #6
    Just checking, but first thing I saw was the form sends it to guessnumber.html, change the file to .php and try again, or as you said use htaccess to change .php file to .html if that's what you really want.
     
    Scoty, Jun 8, 2010 IP
  7. actress143

    actress143 Peon

    Messages:
    132
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    hey godaddy is waste friends......., i have faced lot of problems.., regarding script execution... it is unfit

    i am very confident about this as i faced lot of troubles.
     
    actress143, Jun 8, 2010 IP
  8. maplater

    maplater Peon

    Messages:
    97
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hey thanks everyone for helping, I'm really trying to get the looping part down. I went back through the code and edited some stuff and came up with this below. However now it automatically outputs the form over and over rather than stopping and waiting for me to input a number. How do I get the screen to clear and just show the form once?

    <?php
    $random = rand(0,10);
    $win = 0;
    do {
    ?>
    <form action="guessnumber.php" method="post">
    Enter a number 0-10: <input name="number" type="text">
    <input name="submit" type="submit">
    </form>
    <?php
    $number = $_POST['number'];
    if ($number == $random) {
    $win = 2;
    }
    else {
    $comp = rand(0,10);
    $win = ($comp == $random);
    echo "The computer choose: $comp";
    }

    } while ($win == 0);

    if ($win = 2) {
    echo "You Win!!";
    }
    else {
    echo "The Computer Won :(";
    }
    ?>


    Thanks again so much.
     
    maplater, Jun 8, 2010 IP
  9. maplater

    maplater Peon

    Messages:
    97
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    maplater, Jun 8, 2010 IP
  10. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #10
    Godaddy is only suited for Domain registration
    Better go with HostGator or Dreamhost (I love DH's Supoport)
     
    roopajyothi, Jun 8, 2010 IP
  11. silviuks

    silviuks Peon

    Messages:
    43
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Depends how server is configured. I far as i know, by default .html is .html and .php is .php. you can play with htaccess to solve this issue ...
     
    silviuks, Jun 9, 2010 IP
  12. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #12
    Yeah htaccess just help to change the links that appear
    And they wont play with SHELL/Binary Packages or PHP inside that
     
    roopajyothi, Jun 9, 2010 IP