could someone please help with a simple loop?

Discussion in 'PHP' started by popemobile, Oct 19, 2011.

  1. #1
    Hi, I am new to programming and I made a simple PHP to save data to a txt file. I am going mad trying to create a loop for it though.

    I wanted to start with a simple y/n to finish the program or to continue and add more data.

    My experience is with c# and I would use the 'do while' and 'switch' commands.

    With PHP I can't seem to figure out how to change a simple variable ($yesno) depending on a users input!

    Hope this made sense. Here is my basic code:

    <?php

    $File = "textStore.txt";
    $Handle = fopen($File, 'a');

    echo "Type data to be saved: ";

    $input = fgets (STDIN);

    echo "You typed: ".$input;

    fwrite($Handle, $input);

    print "Data Added";

    fclose($Handle);

    ?>
     
    Solved! View solution.
    popemobile, Oct 19, 2011 IP
  2. #2
    PHP is a server-side script which means it is not possible to take client side commands while the server is processing the script.


    PHP takes user input through form submission or url parameters - Look at the global variables $_GET and $_POST. You would have to complete a section, display the information to the client and wait for user input on the page through form submission before the script could be loaded again.


    If you want it live, what you could do is set one script running which processes your information using a loop and writes this to a file, and then checks a database variable to see if it is still permitted to progress.

    Another script can be ran which periodically outputs this file to the user (periodically called using an ajax request - javascript maybe?)

    One final script which takes user input through another ajax request which sets the database value to a boolean.
     
    Divided, Oct 20, 2011 IP
  3. popemobile

    popemobile Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks that made sense!
     
    popemobile, Oct 20, 2011 IP