我在互联网上找到了一个非常有用的脚本。无论如何,我不希望它低于0.所以它不会显示负数。此外,我希望当数字增加时,它可以增加到一个值。例如,我不希望该数字超过20且小于0.
我该怎么做?
脚本就是这个
<?php
// gets current value
if(isset($_POST['output'])){
$value = $_POST['output'];
} else {
$value = 0;
}
if($_POST['Add']){
// opens the file for reading
$read = fopen("file.txt", 'r');
// stores the file as variable cNumAdd
$cNumAdd = fread($read, 512);
// closes file
fclose($read);
// opens file again
$write = fopen("file.txt", "w+");
// adds 1 to the data on the file
$cNumAdd = $cNumAdd + 1;
// writes the data into the file
fwrite($write, $cNumAdd);
// closes file
fclose($write);
}
// comments are the same as above except for subtracting 1 from file
if($_POST['Remove']){
$read = fopen("file.txt", 'r');
$cNumRemove = fread($read, 512);
fclose($read);
$write = fopen("file.txt", "w+");
$cNumRemove = $cNumRemove - 1;
fwrite($write, $cNumRemove);
fclose($write);
}
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="submit" name="Add" value="Add Career">
<input type="submit" name="Remove" value="Remove Career">
<input type="text" name="output" value="<?php include("file.txt"); ?> ">
</form>
&#13;
答案 0 :(得分:0)
您需要做的只是将数字与0和20进行比较。如果大于20,请手动将其设置为20.如果低于0,请手动将其设置为0.
示例:强>
$num = (int) $_REQUEST['output'];
if ($num < 0) {
$num = 0;
} elseif ($num > 20) {
$num = 20;
}
答案 1 :(得分:0)
我认为简单的IF语句可以削减它
<?php
// gets current value
if(isset($_POST['output'])){
$value = $_POST['output'];
} else {
$value = 0;
}
//min and max definitions
define('MIN', 0);
define('MAX', 20);
if($_POST['Add']){
// opens the file for reading
$read = fopen("file.txt", 'r');
// stores the file as variable cNumAdd
$cNumAdd = fread($read, 512);
// closes file
fclose($read);
if($cNumAdd + 1 <= MAX){
// opens file again
$write = fopen("file.txt", "w+");
// adds 1 to the data on the file
$cNumAdd = $cNumAdd + 1;
// writes the data into the file
fwrite($write, $cNumAdd);
// closes file
fclose($write);
}
}
// comments are the same as above except for subtracting 1 from file
if($_POST['Remove']){
$read = fopen("file.txt", 'r');
$cNumRemove = fread($read, 512);
fclose($read);
if($cNumRemove - 1 >= MIN){
$write = fopen("file.txt", "w+");
$cNumRemove = $cNumRemove - 1;
fwrite($write, $cNumRemove);
fclose($write);
}
}
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="submit" name="Add" value="Add Career">
<input type="submit" name="Remove" value="Remove Career">
<input type="text" name="output" value="<?php include("file.txt"); ?> ">
</form>
&#13;
答案 2 :(得分:0)
我希望这对你有用。
// opens file again
$write = fopen("file.txt", "w+");
// adds 1 to the data on the file
$cNumAdd = $cNumAdd + 1;
// writes the data into the file
if($cNumRemove > 20) $cNumRemove = 20;
fwrite($write, $cNumAdd);
// closes file
fclose($write);
}
// comments are the same as above except for subtracting 1 from file
if($_POST['Remove']){
$read = fopen("file.txt", 'r');
$cNumRemove = fread($read, 512);
fclose($read);
$write = fopen("file.txt", "w+");
$cNumRemove = $cNumRemove - 1;
if($cNumRemove < 0) $cNumRemove = 0;
fwrite($write, $cNumRemove);
fclose($write);
}
?>