Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Dec 2005
    Posts
    702
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP:"working with files&directories" / edit record/entry in a txt file , how ?

    I have a simple problem that if I solve I will can solve many others, if is easy answer[no databases involved, just "working with files&directories"):
    I make a telephone directory file [telephones.txt] with fields like phone, first name, last name, city, state, zip well I add files for add entry, retrieve entry just left edit entry ... I can retrieve an entry, but how save an edited entry in the txt file ?

    answer and:
    how in php I redirect user to a specific page eg. retrieve.php ?
    How include files in html/xhtml and how on javascript just like
    php's include("file.html"); ?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Flat files are a pain, that why databases are so much handier. My suggested options would be:
    1. XML. Overhead involved, but you can never go wrong with XML
    2. Simulate Random Access Files by creating explicit data sizes to each of your fields. This will allow you to scan, move, insert, and delete specific blocks nearly as easy as XML allows.
    3. When you edit values, extract a record from the file and append it at the end.
    4. Use an INI style file:
      Code:
      [firstEntry]
      firstName=Persons Name
      lastName=Persons Name
      ...
      [firstEntry]
      firstName=Persons Name
      lastName=Persons Name
      ...
      This is meant for configuration files however, and will allow easy read but writing will be complicated.


    My personal recommendation would be to use the DOM and XML files if available on your server.
    Redirection can be done in html via the:
    <meta http-equiv="refresh" content="0;http://newsite.com/page" />
    and in php:
    header('Location: http://newsite.com/page');

    As for file inclusion, using a server side language is all I can think of (php, perl/SSI, etc), otherwise AJAX is kinda like what you are asking for but that is for retrieving content data in general.
    Hope that helps!
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Regular Coder
    Join Date
    Dec 2005
    Posts
    702
    Thanks
    0
    Thanked 0 Times in 0 Posts
    "When you edit values, extract a record from the file and append it at the end."
    when I extract a record is deleted ? how to delete and extract toend ?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    How you extract is up to the programmer; a delimiter of some sorts is needed. In other words, you need to be able to determine where the location of specified data is.
    Another option I didn't mention is to use an Object for your data record, and you can serialize the data you need and store that per line in a file. This allows you to extract each record, load it into the object (easy in php with __sleep and __wakeup commands in PHP 5), and store this data in a structure of some sorts (such as an array). When you are done manipulation (updates, inserts and deletes), serialize each object back into the file.
    This is IMO the easiest approach if you have Object Oriented experience. If you are a little more inclined and experienced, I'd brute up a RAF technique which isn't too bad in php - but you need to explicitly specify your data lengths for it to work, and could result in wasted space. I won't go into how this is done though, if you are interested search up on 'Random Access Files' for the concept of how that is done.

    In any case, to be quite honest, files really have become a more advanced PHP technique. In the past, they were the primary storage for information, but with the increased popularity of databases they have become somewhat obsolete in many programmers eyes. Personally I don't think this is the case, but the downside is that I find a lot of new programmers paying little attention to how the file handling is done in php - especially in unix based systems.

    If you give me all the fields you need tracked (first name, last name, number etc), I can show you how to implement this in an object oriented approach and use a file system with your choice of a datastructure to use.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •