0

I was trying to write a code for fizz buzz game...and i completed it...but the problem is i need to take input from the user, my code only works for the input that i have declared inside the code...could anyone specify any way that i can take input from user...post method didn't work.. my code is as follows...

<html>
    <body>
        <form method="post">
            enter Value of A 
            <input type="text" name="A">
            enter value of B
            <input type="text" name="B">
            enter value of N
            <input value="text" name="N">       
        </form>
    </body>
</html>

<?php
    $N=$_POST["N"];
    $A=$_POST["A"];
    $B=$_POST["B"];

    while($N!=100) {   
        if($N%$A==0 & $N%$B==0) {
            echo"FizzBuzz\n";
        }
        elseif ($N%$A==0) {
            echo"Fizz\n";         
        }
        elseif ($N%$B==0) {
            echo"buzz\n";
        }     
        else
            echo"$N \n";

        $N=$N+1;
    }        
?>
6
  • Wut? What exactly do not work? Commented Apr 20, 2014 at 20:46
  • Why won't POST work... Your question is not really clear... It sounds like you are trying to monkey patch something instead of fixing the real issue at hand... Those three dots between sentences are not really needed... Commented Apr 20, 2014 at 20:46
  • okay, now you need a submit button to submit the form Commented Apr 20, 2014 at 20:55
  • thanx..man... i have a file from which i have to take input..i know i have to use fgets() fn. I need to store the input in an array..how should i proceed.. Commented Apr 20, 2014 at 21:07
  • errr, so no real user will fill the form values, but you need to take input from a file? like a console application? Commented Apr 20, 2014 at 21:14

1 Answer 1

1

After the OP made it clear that it should look like a console application that takes input from a file, the answers I can prepare are several:

First of all there is no need of HTML, since there will be no real user that will open the web browser, because everything will happen from the CLI, and there will be no input, but a file.

To take a file as input you may need fgets(). The file can be sent from the standard input, so fgets(STDIN) will do the work. However, this really depends how the file is formatted.

In one ideal case, I have file test.txt which has each on separated lines:

50
100
200

To use test.txt as standard input, you may need:

<?php
$input = explode("\r\n",stream_get_line(STDIN, 1024));
//Explode the news lines as array, to assign then as N, A and B



    $N=$input[2];
    $A=$input[0];
    $B=$input[1];

    while($N!=100) {   
        if($N%$A==0 & $N%$B==0) {
            echo"FizzBuzz\n";
        }
        elseif ($N%$A==0) {
            echo"Fizz\n";         
        }
        elseif ($N%$B==0) {
            echo"buzz\n";
        }     
        else
            echo"$N \n";

        $N=$N+1;
    }        
?>

Now you run the script from the console as

$ php path/to/file/file.php < path/to/input/test.txt

Ofcourse, you can make costumizations where openning the file is waiting for input which can be a file name

$stdin = fgets(STDIN); and then $ php path/to/file/file.php will wait for something to be typed.

If it's a valid file name you can use file_get_contents($stdin);

Or, you can use command line arguments, available in CLI mode $argv array:

$file = file_get_contents(__DIR__.DIRECTORY_SEPARATOR.$argv[1]);
$input = explode("\r\n", $file);

This will open the file, with the given name as first parameters, in the directory of the PHP file

$ php path/to/file/file.php test.txt
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.