Page 1 of 1

Saving data to flatfile

Posted: Wed Sep 13, 2006 5:27 pm
by blaze
hi all

hope someone can help with this as im totally head mashed & need a new perspective :(

Basically i have creating a php pm system that works great :D

Now in the admin CP i have an option to clean data which includes pms

all data is stored in flatfiles as i dont have sql and this script is designed for others like me.

all messages are stored in files like this for example:

pmd-Username.dat

inside this each pm is stored like this line by line:

pmcode|date|from|subject|message|status

the status part tells us if the message is 'read' or 'new'

i have got my script to seperate 'read' messages & 'new' messages

so i now have three possible options for the clean option in the admin CP

1. Clean - Read Messages
2. Clean - New Messages
3. Clean - All Messages

option 3 is done - no probs!!

the problem is option 1 or 2 - I know if i get one of them then the other is gonna be the same.

this is the code im having trouble with

Code: Select all

if ($pm == "cleanold")
{
$dir = opendir("$pmdir"); $c = 0;          //assign & open the dir for stored user pms
while ($file = readdir($dir))                   // loop through to collect all files
{
$c = $c + 1;
if ($c < 3) { continue; }
$file = explode("-", $file);            
if ($file[0] == "pmd")
{
$pmfile = "$pmdir/pmd-{$file[1]}";
$hnd = file($pmfile);
for ($x = 0; $x < sizeof($hnd); $x++)  // Open all files that start with pmd
{
list($id, $smdate, $sfrom, $ssub, $smess, $status) = explode('|', trim($hnd[$x])); 

$status = trim($status);                       // Make sure we are not processing black data

if ($status == "new")                           // Start seperation 'new' else 'read'
{
$ndata[] = $file[1]."|".$id."|".$smdate."|".$sfrom."|".$ssub."|".$smess."|".$status;
}
else
{
$rdata[] = $file[1]."|".$id."|".$smdate."|".$sfrom."|".$ssub."|".$smess."|".$status;
}
}
}
}
include ("done.htm"); exit();
}
this code works and is the building block for what i need help with, sorry if im goin on lol its hard to explain :wink:

now i have my 'new' & 'read' data, as this code is ''cleanold'' i only want to save $ndata

this is where im lost as i need to make sure i save the right messages back to the right users

note the following code

Code: Select all

$ndata[] = $file[1]."|".$id."|".$smdate."|".$sfrom."|".$ssub."|".$smess."|".$status;
}
else
{
$rdata[] = $file[1]."|".$id."|".$smdate."|".$sfrom."|".$ssub."|".$smess."|".$status;
i have already saved the username in $file[1] which = 'username.dat'

i keep looking at it and i see it lookin at me but i just can get it

any help would be much appreciated :!:

I hope you find this easy to read :oops:

ta

-::BlAzE::-

Posted: Thu Sep 14, 2006 4:07 am
by onion2k
Personally, I'd store each message seperately and use filenames to mark statuses like that. So your file would be called something like:

123.00000041.read.pm
123.00000042.read.pm
123.00000043.read.pm
123.00000044.read.pm
123.00000045.new.pm
123.00000046.new.pm

123 = user id
00000041 = message id
read = message has been read
new = message is new

When the user views a message you'd rename the file (using move() ) changing 'new' to 'read'. When the user wants to delete all their old messages you'd loop through the files in the directory you're storing them in checking the filenames .. any where the third part is 'read' get unlinked. Likewise for deleting new messages, you'd loop through the files deleting all the ones where the third part of the name is 'new'. It'd be fast as you're not opening files at all. It'd be easier than deleting specific lines from a file too (though not by much..)

Maybe make an index file too to store some info about each message (who it's from and subject line for example). Of course, then you'd be back where you are now coz you'd need to delete stuff out of the index..

Posted: Thu Sep 14, 2006 7:14 am
by blaze
hi onion2k

thanks for the reply

i like the idea i dont know about the index thing not sure i get what u mean

i would store the message in the file

i'll have to give it a go and see what happens

it means i would have to re write to whole pm system to :(

Posted: Thu Sep 14, 2006 8:16 am
by neophyte
Have you considered SQLITE -- flat file data system with SQL access.

http://us3.php.net/manual/en/ref.sqlite.php

Posted: Thu Sep 14, 2006 8:48 am
by blaze
hi neophyte

its not really an option

as i said.........

all data is stored in flatfiles as i dont have sql and this script is designed for others like me.

Posted: Fri Sep 15, 2006 2:35 am
by blaze
anyone? :D

Posted: Fri Sep 15, 2006 12:27 pm
by blaze
well after many hours ive cracked it

not sure if its the best way but it works a treat!

Code: Select all

// 1st wipe all messages from all users as we have the new onces in $ndata

for ($x = 0; $x < sizeof($rdata); $x++)  // for all read messages
{
list($user, $id, $smdate, $sfrom, $ssub, $smess, $status) = explode('|', trim($rdata[$x]));

$dat = "";                          //blank data to save
$aa[] =  $user;                   // collect all users in array
foreach ($aa as $a)             // each user 1 by 1
{
$xuser = $a;
$u_path = "$userdb/pmd-{$xuser}";           // get the file
$file = @fopen($u_path, 'w');                  
@fputs($file, $dat);                  // wipe all the messages
@fclose($file);
}
}
/////////////////////////////////////////////////

// now save new messages back to members
for ($x = 0; $x < sizeof($ndata); $x++)  // for all new/unread
{
list($user, $id, $smdate, $sfrom, $ssub, $smess, $status) = explode('|', trim($ndata[$x]));
$a = $user;
${$a}[] =  $id."|".$smdate."|".$sfrom."|".$ssub."|".$smess."|".$status;
foreach (${$a} as $xzx)
{
$xuser = $a;
$u_path = "$userdb/pmd-{$xuser}";
$file = @fopen($u_path, 'w');
for ($y = 0; $y < sizeof(${$a}); $y++)
{
@fputs($file, ${$a}[$y] . "\n");
}
@fclose ($file);
}
}
//////////////////////////////////////////////////
Mission complete!!

:cheers:

-=:BlAzE:=-