php - Deleting specific data from a text document using fwrite? -
i writing "unfriend" script mini social network, unfriend action needs remove id called: $user
friend list , id called: $my_id
. have made scripts add user id's each users files not sure how delete this?
my friend_list
file: 1,2,3 // user 1
their friend_list
file: 1,2,3 // user 3
i use add each id text file:
if($action === 'accept'){ mysql_query("delete `friend_req` `from`='$user' , `to`='$my_id'"); $fp = fopen($user_data['friend_list'], 'a'); fwrite($fp, ",".$user.""); fclose($fp); $their_id = friend_list_from_user_id($user); $fp = fopen($their_id, 'a'); fwrite($fp, ",".$my_id.""); fclose($fp); }
but remove $my_id
friend_list
, $user
id friend_list
i'm thinking need use isn't working:
if($action === 'unfriend'){ $fp = fopen($user_data['friend_list'], 'a'); unset($user); fclose($fp); $their_id = friend_list_from_user_id($user); unset($my_id); fclose($fp); }
but doesn't seem work, should delete specified usernames respective file?
oh, , also, may/may not of use:
$myfile = $file; $fh = fopen($myfile, 'r'); $thedata = fgets($fh); fclose($fh); $friend = explode(',', $thedata); for($i=0; $i < count($friend); $i++){ if($i >= 0) { if($friend[$i] == $user_id) { $their_user_id = $user_id; } }
to solve issue like:
//get contents of file $contents = file_get_contents($user_data['friend_list']); $contents = str_replace(",".$friend_id, "", $contents); //remove friend's id //open file again , rewrite whole thing new contents without id $fp = fopen($user_data['friend_list'], 'w+'); fwrite($fp, $contents); fclose($fp);
Comments
Post a Comment