Jump to content

Writing to next text-file-line problem


SwarleyAUS

Recommended Posts

hey guys

 

coding a website atm that allows you to, once youve registered, change your account details. the user data is stored in a text file line by line, with each user having a varying number of characters for username, password, name, surname, phone number, email etc. these details are all separated by "|".

 

my problem is that when the user adjusts their information, the line their data is on in the text file either doesnt get overridden fully (if their new input of characters is less than originally) and the old data is pushed to the new line because of a "\n", or it overwrites information on the next line (if their new input of characters is more than originally) and then does a new line.

 

tried to do a cutline program, but thats a bit beyond me

 

thanks guys

 

 

CODE:

 

function cutline($file, $line_no =-1) { //not working but im guessing itd be something along those lines

 

$strip_return = false;

$size = count($data);

 

if($line_no == -1) {

$skip = $size-1;

} else {

$skip = $line_no-1;

}

 

for($line = 0; $line < $size; $line++) {

if($line != $skip) {

fputs($file, $data[$line]);

} else {

$strip_return = true;

}

}

return $strip_return;

}

 

if (isset($_POST['adjust'])) {

$file = fopen("members.txt", 'r+');

while (!feof($file)) {

if ($data[0] == $_SESSION['type'] && $data[1] == $_SESSION['password'] && $data[5] == $_SESSION['ph'] && $data[7] == $_SESSION['email']) {

echo "found user";

if ($_POST['new_username'] == "") {

$new_username = $_SESSION['type'];

} else {

$new_username = $_POST['new_username'];

$_SESSION['type'] = $new_username;

}

 

if ($_POST['new_password'] == "") {

$new_password = $_SESSION['password'];

} else {

$new_password = $_POST['new_password'];

$_SESSION['password'] = $new_password;

}

 

if ($_POST['new_ph'] == "") {

$new_ph = $_SESSION['ph'];

} else {

$new_ph = $_POST['new_ph'];

$_SESSION['ph'] = $new_ph;

}

 

if ($_POST['new_address'] == "") {

$new_address = $_SESSION['address'];

} else {

$new_address = $_POST['new_address'];

$_SESSION['address'] = $new_address;

}

 

if ($_POST['new_email'] == "") {

$new_email = $_SESSION['email'];

} else {

$new_email = $_POST['new_email'];

$_SESSION['email'] = $new_email;

}

 

fseek($file, $line);

cutline($file, $line);

$data = fwrite($file, $new_username."|".$new_password."|".$_SESSION['name']."|".$_SESSION['surname']."|".$_SESSION['dob']."|".$new_ph."|".$new_address."|".$new_email."|".$_POST['new_reglevel']."|".$_POST['new_regtime']."\n");

fclose($file);

exit();

} else {

  $line+1;

}

} fclose($file);

}

 

 

TEXT FILE (members.txt):

 

jimBob|jimBob|Jim|Bob|20/02/1965|95555555|Mentone|jim_bob@gmail.com|Gold|1 Year

admin|123|admin|admin1|-|-|-|admin1@gmail.com|-|-

blah|blah|-|-|-|-|Gold|Lifetime

 

 

eg - TEXT FILE after user data changed and less characters are inputted:

 

jim|abc|Jim|Bob|20/02/1965|95555555|blahaha|jim_bob@gmail.com|Gold|1 Year

ar

admin|123|admin|admin1|-|-|-|admin1@gmail.com|-|-

blah|blah|-|-|-|-|Gold|Lifetime

 

 

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.