This is my PHP:
$file = fopen("1stock.js", "c+");
$number = fseek($file, 20, SEEK_CUR);
$fh = fgetc($number);
fwrite($file, $fh -1);
My 20th character is a number I want to do that number - 1
when the php file loads. Why is this not working?
I am also looking to do this for multiple other positions on a .js file, that looks a bit like this:
var item1_stockS = 5 //20th character including space.
var item1_stockM = 8
var item1_stockL = 3
I have a HTML form that looks like this:
<form action="stock.php" method="post">
Item1 S:<input value="remove" name="remove1S" type="submit" /> <br/> <script type="text/javascript">document.write(item1_stockS);</script><br/>
Item1 M:<input value="remove" name="remove1M" type="submit" /> <br/> <script type="text/javascript">document.write(item1_stockM);</script> <br/>
Item1 L:<input value="remove" name="remove1L" type="submit" /><br/> <script type="text/javascript">document.write(item1_stockL);</script> <br/>
So that when you click on one of the button it removes 1 item from the corresponding stock number.
When you do fgetc
the current position in the file is advanced one step, so when you write you write to after where the number is. You have to seek one step backwards before writing.
Also, be careful if the value is zero, as then you will write -1
which is two characters and will overwrite the newline after the digit.
The problem you have is that the argument to the fgetc
function should be the file handle . See the fgetc
documentation!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.