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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.