Greets;

I have a flat file DB, as what i need is small and doesnt warrant an SQL db.

I have all my records displayed on a master page, all i want to do now is add a button to delete a record as it is clicked, any help wuld be appreciated.

This is the code to display the entries in my db,

<?PHP 
$filename='biobedall.txt';
$task=$_POST['task'];

{ 
    $lines = file($filename); 
    $shoutbox = implode("", $lines); 
    $posts = explode(";",$shoutbox); 
    foreach (array_reverse($posts) as $post) 

{ 
    $content = explode(", ", $post); 
    if ($content[1] != "") 


    { 
        echo "<b>".str_replace(":semicolon:", ";", str_replace(":comma:", ", ", $content[1]))." "; 
        echo str_replace(":semicolon:", ";", str_replace(":comma:", ", ", $content[0]))." </b>";
        echo str_replace(":semicolon:", ";", str_replace(":comma:", ", ", $content[5]))."<br>";
        echo str_replace(":semicolon:", ";", str_replace(":comma:", ", ", $content[4]))."<br>";
        echo str_replace(":semicolon:", ";", str_replace(":comma:", ", ", $content[3]))."<br>";
        echo "Added on: ".date('m/d/Y', $content[6])." ";  [B][I]#here is where id like a link/button to delete a record as it appears[/I][/B]

   }    

}
}    

?> 

Thanks in advance

dan

    danlindley wrote:

    I have a flat file DB, as what i need is small and doesnt warrant an SQL db.

    So instead you've decided to reinvent the wheel, albeit a slightly smaller one? :p My first suggestion would be to simply use the SQL DB - it's essentially a flat-file database system that's been tweaked and optimized for years by many experienced developers.

    Otherwise, it looks like you'd have to either store some unique ID along with each line that could then be searched for when deleting, or else you'd simply pass the line number along in a delete link (though what happens if someone adds something after the page loads for you and before you click the delete button? - answer is that the wrong item gets deleted).

      Unfortunately, I'm doing my best as server admin wont allow me a database for my small project, hence the flat file.

      So anyadvice here on how to do this?

      thanks again;

      dan

        Unfortunately, I'm doing my best as server admin wont allow me a database for my small project, hence the flat file.

        Free hosting, I presume?

        Anyway, most PHP installations should have SQLite available by default. If it is available for you, use it. You will not have the features of a full relational database management system at hand, but SQLite was designed to be a replacement for flat file database formats, and it does keep everyone in one database file.

          It is not a free hosting, the database administrator staff will not approve ANY database for this project, they think it doesn't warrant it, So i am using a flat file system.

          I could really use some help on this matter. Thanks.

            It is not a free hosting, the database administrator staff will not approve ANY database for this project, they think it doesn't warrant it, So i am using a flat file system.

            Unfortunately, a flat file database is a database. I am afraid that you simply have to ask the database administrator staff for permission.

            Or, if they do allow you to use a flat file database, then it implies that SQLite can also be used, since SQLite does not require a separate relational database management system software setup. It is an embedded database engine. The only question would be if the SQLite extension (which uses the deprecated SQLite2) is available on your PHP installation, or better yet, the PDO extension is available with the SQLite3 driver enabled.

              If no SQLLite you can also use XML and search using xpath. Simple with SimpleXML if you have PHP5.

              Can be done in 4 but not so fun
              http://uk3.php.net/manual/en/function.xpath-eval.php

              <xml>
                  <items>
                      <item id="1">
                          <title>title 1</title>
                      </item>  
              </items> </xml>

              Xpath attribute search. Returns item where attribute id=1
              /xml/items/item[@id=1]

              <xml>
                  <items>
                      <item>
                          <id>1</id>
                          <title>title 1</title>
                      </item>  
              </items> </xml>

              Xpath node search. Returns item where node id=1.
              /xml/items/item[id=1]

              http://www.developer.com/xml/article.php/3383961

                Humble apologies, they won't allow a seperate DB (something to do with permissions for accessing the tables or something) anyhow, I'm stuck with the flat file as it is the only thing I'm comfortable using. My html forms are posting well to the text files anf I'm viewing them correctly thus far. Just need to create a page to 'manage' them. any ideas?

                  Humble apologies, they won't allow a seperate DB (something to do with permissions for accessing the tables or something)

                  SQLite does not even have the concept of permissions, because database user privileges do not make sense for the type of applications it was designed to support. As I said before, it is an embedded relational database engine that requires almost zero configuration and supports most of the features of SQL92. It is not a standalone relational database management system, and your situation is precisely the type of situation in which it excels.

                  anyhow, I'm stuck with the flat file as it is the only thing I'm comfortable using. My html forms are posting well to the text files anf I'm viewing them correctly thus far. Just need to create a page to 'manage' them. any ideas?

                  Since you are familiar with flat file databases, that should not be a problem. Just stick it all into an array and manipulate the array, then write back to file. Hopefully you have things like file locking down pat so your data does not get corrupted.

                    sorry, now I'm more lost than ever. I just need to know how to do this. Flat file is all im allowed to use. My PHP knowledge is minimal, and this is the 1st time ive done anything like this.

                      Flat file is all im allowed to use. My PHP knowledge is minimal, and this is the 1st time ive done anything like this.

                      hmm... okay, okay. What is your file format, and how do you identify that a record is to be deleted (or selected, for that matter)?

                        I'm just using a flat file... and then in the code above [4] identifies the 4th field in a list of records.

                        I'm not wating to delete field, just a whole record a a time

                          I'm just using a flat file...

                          Yes, but what is its format?

                          and then in the code above [4] identifies the 4th field in a list of records.

                          That's not useful, until we can identify particular records.

                          I'm not wating to delete field, just a whole record a a time

                          Of course, but how do you identify a record to be deleted?

                            I dont now how to, I'll maybe have to add an identifier, I was just hoping to put a button beside each redord to delete them.

                              this is an example of a record;

                              name, rank, allergies, notes, date, pos, 1195741828, 62.136.177.16;

                                I dont now how to, I'll maybe have to add an identifier

                                Yes, you need a key field.

                                I was just hoping to put a button beside each redord to delete them

                                You can do that, but that would be a user interface issue, not a backend flat file modification issue.

                                By the way, have you ever used a relational database like MySQL with PHP before?

                                  The only problem with simply using an arbitrary field (ex. name) to search for records is that how can we be sure that the 'name' field is unique for all entries? Since you're not using a database, I highly doubt you're checking for duplicate names - right?

                                  Anyway, it sounds like searching by name is your best bet. Thus, you could [man]explode/man each line on a comma to echo() out the fields separately. Then, have a button with a link such as "?action=delete&name=$name" where $name is the name for that row.

                                  In your script, you could do something like:

                                  if(isset($_GET['action'],$_GET['name']) && $_GET['action'] == 'delete') {
                                      $data = file_get_contents('the_file.txt');
                                      $data = preg_replace('/' . preg_quote($_GET['name'], '/') . ',.*\s+/', '', $data);
                                      $data = file_put_contents('the_file.txt', $data);
                                  }

                                    Anyway, it sounds like searching by name is your best bet. Thus, you could explode() each line on a comma to echo() out the fields separately. Then, have a button with a link such as "?action=delete&name=$name" where $name is the name for that row.

                                    That reminds me: the file format seems to be CSV, except that there is a terminating semi-colon on each line. Perhaps [man]fgetcsv/man could be used instead.

                                      Quite true - I didn't even think about that.

                                      I still think that the preg_replace() would be the easiest way to remove a specific line, though (now that I've updated the pattern and tested it).

                                        Write a Reply...