Reputation: 47
I'm having a basic problem with PHP. I want to input many data by requesting from a form:
do{
if (isset($_POST['x'])&&($_POST['y'])){
$x = $_POST['x'];
$y = $_POST['y'];
//núclero de processamento
if ($x > 0 && $y > 0){
echo "$x $y primeiro quadrante";
} elseif ($x > 0 && $y < 0){
echo "$x $y segundo quadrante";
} elseif ($x < 0 && $y < 0){
echo "$x $y terceiro quadrante";
} elseif ($x < 0 && $y > 0){
echo "$x $y quarto quadrante";
}
}
} while($x && $y != 0);
In this case i have to use a loop, but here's the deal, i have to input indefinite coordinates on the form and whenever i enter a 0 it should break the loop and process the data printing out the results for each data. BUT it processes a single form, reloads the page and loops forever. What am i missing?
Upvotes: 2
Views: 84
Reputation: 402
Put your if statement outside the loop
if (isset($_POST['x']) && isset($_POST['y'])){
$x = $_POST['x'];
$y = $_POST['y'];
do{
//núclero de processamento
if ($x > 0 && $y > 0){
echo "$x $y primeiro quadrante";
} elseif ($x > 0 && $y < 0){
echo "$x $y segundo quadrante";
} elseif ($x < 0 && $y < 0){
echo "$x $y terceiro quadrante";
} elseif ($x < 0 && $y > 0){
echo "$x $y quarto quadrante";
}
} while($x && $y != 0);
}
Upvotes: 1
Reputation: 779
The web and HTTP doesn't work that way, you can't continuously input data, wait some time and then input some more. Instead it is like htis:
As is hopefully shown above there is no moment of interactivity in this process. A request to a web server consists of one point in time. Otherwise you make a new/separate/different request later, to the same piece of code on the server. But the code doesn't remember anything about the last run(unless you specifically add code to save info about each request somewhere, but that's another ball game..).
The interactivity you need here would be one of
Upvotes: 1
Reputation: 1917
If you want to accomplish this using a website, you need a form, where the user can input the values, then the user have to submit this form, when the data is inserted. Now you can check the input (eg. do your work with it or check, if 0 was inserted). Now you can display the resulting data and the form to input the next data. If you want to work with the data you just worked with when the user submits the next form, you either need to store these values on your server (e.g. in a database) or inside the website (e.g. using a hidden html field).
Upvotes: 0
Reputation: 2621
If you execute a php script is a one time shot, all the code is going to be execute, there's not stop or interaction for the user. In your case your loop never stop because you want to change the value once it was executed (this is not possible). If you want to create an interactive app on the shell with php here is the official documentation http://php.net/manual/en/features.commandline.interactive.php.
hope it helps.
Upvotes: 1