Jump to content

Help with replacing value in text file


yortzec

Recommended Posts

I am having difficulty replacing a number I have stored in a text file, with several tab delimiters separating each value. You can see my attached entries file so you can see how it is set up. I used a guestbook php script set up for me to do this, and now I just need to add one to a value placed in this text file. Here is the code I have so far, with the main idea of it copied from something similar to this already in the guestbook. If you could help me out, that would be fantastic. Thanks so much in advance!

 

P.S. The user would click a link that would begin the php trying to change the value of the number in the text file, because the link is "myscript.php?i=2" and the variable i is the number of the post that i want to change the variable in, beginning at 0. If you need any more info, please ask. Thanks!

 

Contents of myscript.php:

	$numOfEntry = isset($_POST['i']) ? intval($_POST['i']) : false;
    if ($numOfEntry === false)
    {
    	problem('You have not clicked the excellent link, just tried to go to the file yourself. Error.');
    }

$myline = array(0=>'',1=>'',2=>'',3=>'',4=>'',5=>'',6=>'',7=>'',8=>'');
$lines  = file('entry file location here.txt');
$myline = explode("\t",$lines[$numOfEntry]);
foreach ($myline as $k=>$v)
    {
	$myline[$k]=rtrim($v);
}
        $actualstring = $myline[8];
        $actualnum = (int)$actualstring;
        $numplusone = ($actualnum+1);
        $numplusonestr = strval($numplusone);
$myline[8] = $numplusonestr;
$lines[$num] = implode("\t",$myline)."\n";
$lines = implode('',$lines);
$fp = fopen('entry file location here.txt','wb') or problem('Entry file is not writable.');
fputs($fp,$lines);
fclose($fp);

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/190855-help-with-replacing-value-in-text-file/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.