I have a problem with comparing 2 values and let 1 value change within the if statement. But ofcourse when I reload the page it picks up the first value again. I'm using this code to set some information in a database when a machine is turned on or off.
$urlMachineON = 'http://ift.tt/1GbRIuI';
// get content
$contentMachineON = file_get_contents($urlMachineON);
//remove first 2 characters
$truncate = substr($contentMachineON, 2);
//remove last 5 characters
$MachineOn = substr($truncate, 0, -5);
//MachineON can only be 1 or 0
$currentState = 2;
if ($MachineOn != $currentState)
{
$stmt = $conn->prepare("INSERT INTO machineactiviteit (Time, MachineStatus) VALUES(NOW(), ?)");
$stmt->bind_param('s', $MachineOn);
if ($stmt->execute() === TRUE)
{
$currentState = $MachineOn;
echo 'success';
}
else
{
echo $conn->error;
}
$stmt->close();
}
elseif($MachineOn == $currentState)
{
echo 'do nothing';
}
So when I do this he will always use the if statement since the $currentState
and $MachineOn
are always different from each other. In C# you have something like initalize component to set the value one time to a specific value. But I haven't found anything about that in php. So my question is can I set a value only once? Or should I solve this another way?
This is how it should work:
first attempt before: currentState = 2;
MachineOn = 0;
after: currentState= 0;
MachineOn = 0;
second attempt before: currentState= 0;
MachineOn = 0;
after: currentState= 0;
MachineOn = 0;
third attempt before: currentState= 0;
MachineOn = 1;
after: currentState= 1;
MachineOn = 1;
(I can change the MachineOn value with a button).
Aucun commentaire:
Enregistrer un commentaire