Help - Saving data to flatfile database

Discussion in 'PHP' started by :blaze:, Sep 13, 2006.

  1. #1
    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

    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

    
    
    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 blank 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();
    }
    
    
    PHP:
    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

    
    
    $ndata[] = $file[1]."|".$id."|".$smdate."|".$sfrom."|".$ssub."|".$smess."|".$status;
    }
    else
    {
    $rdata[] = $file[1]."|".$id."|".$smdate."|".$sfrom."|".$ssub."|".$smess."|".$status;
    
    
    PHP:
    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

    ta

    -::BlAzE::-
     
    :blaze:, Sep 13, 2006 IP
  2. vdd

    vdd Peon

    Messages:
    34
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try this (from 19):
    ...
    if ($status == "new")              // Start seperation 'new' else 'read'
    {
    $ndata[] = trim($hnd[$x]);
    }
    else
    {
    $rdata[] = trim($hnd[$x]);
    }
    } // end of for
    
    $userf=fopen($pmfile,'w');
     if (we want to save ndata)
      foreach ($ndata as $key=>$val)
       fputs($userf,$val."\n");
     else 
      foreach ($rdata as $key=>$val)
       fputs($userf,$val."\n");
    fclose($userf);
    
    } // end of if file
    } // end of while
    include ("done.htm"); exit();
    }
    PHP:
     
    vdd, Sep 13, 2006 IP
  3. :blaze:

    :blaze: Guest

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    hi vdd

    thanks but the code does not do what i want.

    all new messages for ALL users have been saved in this...

    
    
    $ndata[] = $file[1]."|".$id."|".$smdate."|".$sfrom."|".$ssub."|".$smess."|".$status;
    
    
    PHP:
    no to save it back i need to somehow put each user ($file[1]) in its own array along with there messages otherwise everone will end up with the same messages
     
    :blaze:, Sep 15, 2006 IP
  4. vdd

    vdd Peon

    Messages:
    34
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yeah, you are right. I haver not tested this code, and it contains 1 error.
    After this line:
    $pmfile = "$pmdir/pmd-{$file[1]}";
    PHP:
    Put this code:
    $ndata=Array();
    $rdata=Array();
    PHP:
    Sorry for mistake.
     
    vdd, Sep 15, 2006 IP