Page 1 of 1

Need som help with logics - Editing lines in a flatfile

Posted: Sat Apr 30, 2005 4:51 pm
by PT
Ok, what I have got is an array containing some e-mail addresses. One e-mail address is one line in my db-file. What I want is for the user to be able to select one or more of these e-mail addresses and edit them. The selected e-mail adresses are stored in a new array. How can I in the best way check if the e-mail address already exist (this is the real problem; I cannot find out if the e-mail address is really changed or not), and if it does not, create a new array containing all the e-mail addresses, both those that was not selected and those that were?
I am really stuck on this one. It will be used in a flatfile mailinglist system.

...

Posted: Sat Apr 30, 2005 6:08 pm
by Calimero
Have you looked at the manual:

Load the file - I think it is fopen(), and then just do a simple checking with preg_match() or in_array(), or just with "==" comparison, to see if the record exists.

Other way is to load the file and in PHP constuct and array of these values.
Again just take new records and cross-reffernce them.

After that, just write to the file what ever you need.

Re: ...

Posted: Sat Apr 30, 2005 6:19 pm
by PT
Calimero wrote:Have you looked at the manual:

Load the file - I think it is fopen(), and then just do a simple checking with preg_match() or in_array(), or just with "==" comparison, to see if the record exists.

Other way is to load the file and in PHP constuct and array of these values.
Again just take new records and cross-reffernce them.

After that, just write to the file what ever you need.
Ok, I see that I may have expressed myself a bit unclear.
I have an array containing all the e-mail addresses:

Code: Select all

$users = array(
'sum@thing.com',
'du@yo.com',
'more@jusa.com',
);
Then the user finds out that he will edit 'du@yo.com'. He checks a box, and submits the form. A new page with a form where he can write in the new e-mail address loads. Now I want to check if the new e-mail address he typed in already exists. The most logic thing to do would be to do this:

Code: Select all

if(in_array('do@yo.com', $users)) {
echo 'Message telling user that the e-mail address already exists.';
} else {
do the editing
}
But what if the user did not changes the e-mail address, how can I then find out if it already exists? If I do an 'if(in_array('do@yo.com', $users))' the e-mail address already exists. You got my problem?

...

Posted: Tue May 03, 2005 10:46 am
by Calimero
I don't understand why do you do checking that way ?!?!


So your goal is just to check that user doesn't insert existing emails in the database, right ?

If so just check the data that was inputed - create an array of those selected, but check to see which (of the members of that array ) the user realy changed ( trough checking the input of the FORM from the page - give one textfield for each selected email, if the field is empty() use array_splice() or unset() on the coresponding member of an array - the array that contains the selected emails ).

This will leave only those mails that user has changed.


To be even more safe - if someone tries to bug your system - when the user submits the FORM check each member of an array with the corresponding textfield and see if they are the same, and if they are - kick that member of an array out.

Is this what you needed?

Re: ...

Posted: Tue May 03, 2005 11:18 am
by PT
Calimero wrote:Is this what you needed?
Yep, I think so. The solution was actually just to leave the textfields empty.
Thanks for the 'brainstorming' :)