bga89031 Posted January 4, 2009 Share Posted January 4, 2009 yeah hi im trying to use this script to write to the second half of any line in a text file <?php $i = ""; $file = file("stats.txt"); $init = $file[$_GET["i"]]; $param = explode(",", $init); $name = $param[0]; $num = $param[1]; if (file_exists('stats.txt')) { $param[1]++; $fil = fopen('stats.txt', r); echo $param[1]+1; fclose($fil); $fil = fopen('stats.txt', w); fwrite($fil, implode(",", $param)); fclose($fil); } else { $param[1]++; $fil = fopen('stats.txt', w); fwrite($fil, implode(",", $param)); echo '1'; fclose($fil); } echo implode(",",$param); ?> where each line in the text file is composed of "string,number". basically, $i should determine the line number to write to and $num should work like a basic counter but i can't quite get this right- it replaces all of my text file with a single-digit "1". im new at this and i nothing i try seems to work. help? p.s. i realize this would be easier to do with mysql database but i find myself unable to acquire a decent working one at the moment Quote Link to comment Share on other sites More sharing options...
btherl Posted January 4, 2009 Share Posted January 4, 2009 Try this code: <?php if (is_numeric($_GET["i"])) { $file = file("stats.txt"); $init = chop($file[$_GET["i"]]); list($name, $num) = explode(",", $init); $num++; $file[$_GET["i"]] = implode(",", array($name, $num)) . "\n"; # $file is now updated - write it back $fp = fopen("stats.txt", "w"); foreach ($file as $line) { fwrite($fp, $line); } fclose($fp); } else { print "'i' was not set"; } ?> The chop() is related to the newline character, which separates lines in a file. It strips it off. Then on the implode() line I add it back in. Quote Link to comment Share on other sites More sharing options...
bga89031 Posted January 4, 2009 Author Share Posted January 4, 2009 -hug- thanks a lot, i really appreciate your help Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.