peter_anderson Posted February 7, 2010 Share Posted February 7, 2010 Hello there, I'm trying to update a tab delimited file using $_POST and var_dump. Here's my form code: <?php $array = file('game.txt'); foreach($array as $key){ $data = explode(',',$key); echo '<input name="game[][opponent]" type="hidden" value="'.$data[0].'" /> <input name="game[][score]" type="text" />'; } But, updating it is what I'm having difficulties with. I want to completely overwrite the content of the txt file and replace it with the updated data. Here's what I've got so far: // Get posted Data $data = var_dump($_POST['game']); $fp = fopen("game.txt","a"); // $fp is now the file pointer to file $filename if($fp){ fwrite($fp,$data); // Write information to the file fclose($fp); // Close the file echo "File saved successfully"; } else { echo "Error saving file!"; } How can I modify the posted data to be in the format of: opponent,score opponent,score etc? I would use a DB, but I'm doing it for a friends football team and he's not keen on using databases for some strange reason. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/191244-var_dump-tab-delimited-files/ Share on other sites More sharing options...
wildteen88 Posted February 7, 2010 Share Posted February 7, 2010 You'll want to loop through $_POST['game'] array $data = ''; foreach($_POST['game'] as $gameResult) { list($oppenent, $score) = array_values($gameResult); $data .= "$oppenent,$score\n"; } $fp = fopen("game.txt","w"); // $fp is now the file pointer to file $filename if($fp) { fwrite($fp,$data); // Write information to the file echo "File saved successfully"; } else { echo "Error saving file!"; } Quote Link to comment https://forums.phpfreaks.com/topic/191244-var_dump-tab-delimited-files/#findComment-1008351 Share on other sites More sharing options...
jl5501 Posted February 7, 2010 Share Posted February 7, 2010 Something like this <?php // Get posted Data $fp = fopen("game.txt","a"); // $fp is now the file pointer to file $filename if($fp) { foreach($_POST['game'] as $data) { $outline = $data['opponent'].','.$data['score']; fwrite($fp,$outline); // Write information to the file } fclose($fp); // Close the file echo "File saved successfully"; } else { echo "Error saving file!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/191244-var_dump-tab-delimited-files/#findComment-1008353 Share on other sites More sharing options...
peter_anderson Posted February 7, 2010 Author Share Posted February 7, 2010 Thanks for that wildteen88 & jl5501 It's now updating everything, but it's adding a new line after each variable (so its: opponent, score opponent, score I added a separate bit in, where it has a drop down list but it can select something. It's only one option per line eg: 1-0, 1-1 etc It's done in the same way as selecting the opposition. How would I stop it from adding a new line? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/191244-var_dump-tab-delimited-files/#findComment-1008366 Share on other sites More sharing options...
jl5501 Posted February 7, 2010 Share Posted February 7, 2010 $outline = trim($data['opponent']).','.trim($data['score'])."\n"; Quote Link to comment https://forums.phpfreaks.com/topic/191244-var_dump-tab-delimited-files/#findComment-1008367 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.