pherank Posted March 8, 2008 Share Posted March 8, 2008 I've got some (admittedly) messy code that reads name/password entries from a textfile and displays them to the administrator: <?php $changed = false; $fp = fopen("clients.txt", "rb"); # do we want to delete a record if (isset($_GET['delid'])) { # remove deleted item from the array unset($fp[$_GET['delid']]); $changed = true; } if ($changed) { # now create new output file, writing in ini file format while (!feof($fp) ) { ksort($fp); # associative array arguments are keys not indexes foreach ($fp as $key=>$dataArray) { fwrite($fp, "[$key]\n"); foreach ($dataArray as $k => $v) { fwrite($fp, "$k=$v\n"); } fwrite($fp, "\n"); } fclose($fp); } } while (!feof($fp) ) { $line_of_text = fgets($fp); $parts = explode('=', $line_of_text); print $parts[0] . $parts[1]. " [<a href='$_SERVER[php_SELF]?delid=$key'>X<a>]<br />"; } fclose($fp); ?> What I'm trying to figure out is how to mark any particular item for delete and remove that array from the textfile. The textfile named clients.txt looks like this: frank blowfish sally tuna I can display the textfile entries to screen but I can't quite figure out how to tag the individual items and successfully delete those entries. Here's a Zip of the files I'm working with: http://www.tornedgedesign.com/_test/login/register.zip Link to comment https://forums.phpfreaks.com/topic/94992-tagging-and-deleting-array-from-textfile/ Share on other sites More sharing options...
Barand Posted March 8, 2008 Share Posted March 8, 2008 $fp is not an array, it's a file handle for use in filesystem function calls on that file. If you have the text file in ini file format frank=blowfish sally=tuna peter=carp paul=shark mary=marlin then <?php $clients = parse_ini_file('clients.txt'); echo '<pre>', print_r($clients, true), '</pre>'; ?> gives this array clients = Array ( [frank] => blowfish [sally] => tuna [peter] => carp [paul] => shark [mary] => marlin ) Now you can unset ($clients['paul']); Link to comment https://forums.phpfreaks.com/topic/94992-tagging-and-deleting-array-from-textfile/#findComment-486636 Share on other sites More sharing options...
pherank Posted March 8, 2008 Author Share Posted March 8, 2008 Your code prints out as: Array ( ) So I don't see any values. I'm not sure what the deal is. Link to comment https://forums.phpfreaks.com/topic/94992-tagging-and-deleting-array-from-textfile/#findComment-486656 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.