Web Analytics Made Easy -
StatCounter adding a counter to code - CodingForum

Announcement

Collapse
No announcement yet.

adding a counter to code

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • adding a counter to code

    hello, I am a biginner.
    bellow is a little game code.
    I want to add a counter to it, to show How many tries?
    thank you.

    PHP Code:
    <?php   
    $num_guess
    =rand (,1000);
    print 
    "try: $num_guess"

    $n 0;
    $i 2;
    $msg="";
    $try0;

    if (Isset(
    $guess))
    {
    $guess$_POST['guess'];

    do {

    if (
    $guess>$num_guess)
    {
    $try $try +1;
    $msg="number $guess is  Big! Try Smaller number. ";
    }

    elseif(
    $guess<$num_guess)
    {
    $try $try +1;
    $msg="Number $guess is Small ! Try Big Number";
    }

    } while (
    $guess<>$num_guess);
    $msg="welcome To This Little Game, Please Enter a Number";
    }

    <
    form action="" method="POST">
            
    Guess Number: <input type="text" name="guess">
     <
    br>
            <
    input type="submit" name="submit" value="Submit Form"
    </
    form>

    $n $try;
    $msg="Well Done You Win in $n times try";

    print 
    $msg;
    print 
    "<BR>";print "try: $n"
    $try 0;  
     
    ?>

  • #2
    Excuse me the above code was wrong. the correct code is this:
    I want to add a counter to it, to show How many tries executioned?

    <?php

    $num_guess=42;
    $msg="";
    $guess= $_POST['guess'];

    if (! Isset($guess))

    {
    $msg="welcome To This Little Game, Please Enter a Number";
    }
    elseif ($guess>$num_guess)
    {
    $msg="number $guess is Big! Try Smaller number.";
    }

    elseif($guess<$num_guess)

    {
    $msg="Number $guess is Small ! Try Big Number";
    }

    else // vagti mosavi bashe
    {
    $msg="Well Done You Win";
    }

    print $msg
    ?>

    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    Guess Number: <input type="text" name="guess">
    <br>
    <input type="submit" name="submit" value="Submit Form"><br>
    </form>

    Comment


    • #3
      This is the forum for sharing well written PHP code for others to use in their sites.

      I think you posted to the wrong forum.
      Stephen
      Learn Modern JavaScript - http://javascriptexample.net/
      Helping others to solve their computer problem at http://www.felgall.com/

      Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

      Comment


      • #4
        Moved to the correct forum.
        Stop solving problems you don’t yet have!

        Comment


        • #5
          please tell me to witch forum moved.
          Thank you.

          Comment


          • #6
            It has been moved to the PHP forum (although there are no witches on any of the forums here).
            Stephen
            Learn Modern JavaScript - http://javascriptexample.net/
            Helping others to solve their computer problem at http://www.felgall.com/

            Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

            Comment


            • #7
              PHP Code:
              <?php
              $count 
              = isset($_POST['count']) ? $_POST['count'] : 0//Use post value if defined. If not set to 1.
              if (!isset($_POST["guess"])) 
              {     
                
              $message "Welcome to the guessing machine!";     
                
              $_POST["numtobeguessed"] = 42//<-- Number you want guesses

              else if (
              $_POST["guess"] > $_POST["numtobeguessed"]) 
              //greater than    
                
              $message $_POST["guess"]." is too big! Try a smaller number.";

              else if (
              $_POST["guess"] < $_POST["numtobeguessed"]) 

              //less than    
                
              $message $_POST["guess"]." is too small! Try a larger number.";

              else 

              // must be equivalent    
                
              $message "Well done! You guessed the right number in ".$count." attempt(s)!";        
              //Include the $count variable to the $message to show the user how many tries to took them.}$count++;
              }
              ?>
              and the HTML
              Code:
              <html>
                  <head>
                      <title>A PHP number guessing script</title>
                  </head>
                  <body>
                  <h1><?php echo $message; ?></h1>
                      <form action="" method="POST">
                      <p><strong>Type your guess here:</strong>
                          <input type="text" name="guess"></p>
                          <input type="hidden" name="numtobeguessed"
                                 value="<?php echo $_POST["numtobeguessed"]; ?>" ></p>
                          <input type="hidden" name="count"
                                 value="<?php echo $count; ?>" ></p>
                  <p><input type="submit" value="Submit your guess"/></p>
                      </form>
                  </body>
              </html>
              Riley
              Riley-Shannon.com - My Portfolio
              FraktalServices.com - My Company

              Comment

              Working...
              X