Jump to content

Tagging and Deleting Array from Textfile


pherank

Recommended Posts

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

$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']);

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.