0

based from the accepted answer from this post

how to make it continuously wait for next input. for example

<?php
echo "Please Input userid ? : ";
$handle = fopen ("php://stdin","r");
$line = fgets($handle);
//THEN LET'S SAY AFTER INPUT USER ID, PLEASE INPUT ADDRESSID 
//AND SON ON AND SO FORTH
//HOW DO WE MAKE THE WAITING OF INPUT CONTINUOUS ?
fclose($handle);
echo "\n"; 
echo "Thank you, continuing...\n";
?>

I want to ask "please input USERID" , after user input userid, "please input add id" then after user inputs add id, follows another question.. the only time code stops if it already satisfies the questions. so how to make the waiting of input continuous ?

sasori
  • 5,249
  • 16
  • 86
  • 138

4 Answers4

3

Make it in loop

$questions = [
    'Please Input 1 ? :' => 1,
    'Please Input a ? :' => 'a',
];

function askQuestion($question, $correctAnswer)
{
    echo $question;

    $handle = fopen ("php://stdin","r");
    $line = fgets($handle);

    if(trim($line) != $correctAnswer){
        echo "ABORTING!\n";
        exit;
    }

    fclose($handle);

    echo "\n";
}

foreach ($questions as $question => $answer) {
    askQuestion($question, $answer);
}

echo "Thank you, continuing...\n";
Justinas
  • 41,402
  • 5
  • 66
  • 96
1

All you need to do is wrap your logic in a loop. And with the STDIN constant you don't even need the fopen/fclose part.

<?php

$questions = [
  'Please input 1 ?' => '1',
  'Please input a ?' => 'a',
];

foreach($questions as $question => $expectedAnswer) {
    echo $question . "\n";
    $answer = fgets(STDIN);

    if(trim($answer) !== $expectedAnswer) {
        die('ABORTING');
    }

    echo "Thank you, continuing...\n";
}
echo "All done\n";

maxleroy
  • 126
  • 3
0

For creating command line application in PHP I recommended:

symfony/console

There is something like QuestionHelper. You can use it form your problem.

Adam
  • 71
  • 6
  • I changed my question – sasori Dec 20 '19 at 08:14
  • I think this is also answer for you new question. In QuestionHelper is method ask() Look at that (https://symfony.com/doc/current/components/console/helpers/questionhelper.html#asking-the-user-for-information) – Adam Dec 20 '19 at 08:17
0

If I understood correctly then you will not necessarily be expecting a particular answer but just wish to wait for the user to answer a series of questions? Below the user input is accepted if it is not an empty string - otherwise the question will be repeated. To make this wait indefinitely set the time limit to zero - ie: set_time_limit(0);

<?php

    set_time_limit( 100 );

    /* Things to ask... */
    $questions=array(
        'UserID',
        'Title',
        'DOB',
        'Address',
        'Marital status',
        'Preferred language',
        'Favourite car'
    );


    function ask( $char, $index ){
        printf( "[ %d ] Please enter %s\n", $index, $char );

        /* capture user input */
        $input=trim( fgets( STDIN ) );

        /* offer the ability to escape from endless loop if no time limit set */
        if( strtolower( $input )=='exit' or strtolower( $input )=='quit' ){
            exit( PHP_EOL . 'goodbye' );
        }

        if( !empty( $input ) ){
            /* do something with the user input - save to db, write to textfile, echo */
            echo ' - '.$input . PHP_EOL . PHP_EOL;
        } else{
            /* 
                If the criteria for the answer are not met, 
                repeat the question
            */
            ask( $char, $index );
        }
    }


    foreach( $questions as $index => $char ){
        ask( $char, $index + 1 );
    }

    echo PHP_EOL . 'Finished!';
?>
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
  • perfect, this is what i want exactly, now I can just modify and integrate my own code and bussiness logic....thank you sir! – sasori Dec 21 '19 at 08:17