Read specific line from text file using PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Johnymap
    New Member
    • Dec 2007
    • 10

    Read specific line from text file using PHP

    Hi everyone.

    The text file i am using has the following format:

    "2001_1.rtf " (1 January 2001) Ookame v Tombale (CCF No. 203 of 2001) [2001] BWHC 1
    "2001_2.rtf " (1 January 2001) Kgaimena v Leoifo (MISCA F.572 of 2001) [2001] BWHC 2
    "2002_1.rtf " (7 June 2002) Olebeng v The State (C A No. F104 of 2001) [2002] BWHC 1
    "2003_1.rtf " (1 January 2003) Sebogodi v Sebogodi (Matrimonial Cause No.F68 of 2003) [2003] BWHC 1

    So i have a dropdown form object populated with years, if the user select 2001(for eg.) on the form, I have to read the last value of that year from the text file which is "2001_2.rtf " (1 January 2001) Kgaimena v Leoifo (MISCA F.572 of 2001) [2001] BWHC 2
    do some processing and write back some new data to the text file under this specific line not at the end of file.

    Please help me
  • nathj
    Recognized Expert Contributor
    • May 2007
    • 937

    #2
    Originally posted by Johnymap
    Hi everyone.

    The text file i am using has the following format:

    "2001_1.rt f" (1 January 2001) Ookame v Tombale (CCF No. 203 of 2001) [2001] BWHC 1
    "2001_2.rt f" (1 January 2001) Kgaimena v Leoifo (MISCA F.572 of 2001) [2001] BWHC 2
    "2002_1.rt f" (7 June 2002) Olebeng v The State (C A No. F104 of 2001) [2002] BWHC 1
    "2003_1.rt f" (1 January 2003) Sebogodi v Sebogodi (Matrimonial Cause No.F68 of 2003) [2003] BWHC 1

    So i have a dropdown form object populated with years, if the user select 2001(for eg.) on the form, I have to read the last value of that year from the text file which is "2001_2.rt f" (1 January 2001) Kgaimena v Leoifo (MISCA F.572 of 2001) [2001] BWHC 2
    do some processing and write back some new data to the text file under this specific line not at the end of file.

    Please help me
    Hi,

    Short of changing this to use a database I would recommend that you take alook at the file functions in php.

    It is possible to take a file to an array with each line being an elemetn in the array. Then wne an item is selected you can search for it in the array using PHP's array functions and that should give you the answer you are after.

    So try file to get the array, thenuse a foreach to traverse the array or to get sspefic key.

    If you need to read from and write to the text file you will need to look at the low level file functions as well.

    However, all of this headache would simply go away if you could use a database for this.

    Cheers
    nathj

    Comment

    • Johnymap
      New Member
      • Dec 2007
      • 10

      #3
      Thanks Nathj

      I managed to read the text file and write to it. The only problem is getting the correct line from the array. Can you please help me specifically with the code to do this.

      Thanks again in advance.
      Johnymap

      Comment

      • Johnymap
        New Member
        • Dec 2007
        • 10

        #4
        Originally posted by nathj
        Hi,

        Short of changing this to use a database I would recommend that you take alook at the file functions in php.

        It is possible to take a file to an array with each line being an elemetn in the array. Then wne an item is selected you can search for it in the array using PHP's array functions and that should give you the answer you are after.

        So try file to get the array, thenuse a foreach to traverse the array or to get sspefic key.

        If you need to read from and write to the text file you will need to look at the low level file functions as well.

        However, all of this headache would simply go away if you could use a database for this.

        Cheers
        nathj
        Thanks nathj

        unfortunatelly I am not allowed to use a database for this matter, its just some co. policy. So, do you have some code that i can use to solve this. i have looked almost everywhere for help but nothing seems to be helping.

        Please help

        Comment

        • nathj
          Recognized Expert Contributor
          • May 2007
          • 937

          #5
          Originally posted by Johnymap
          Thanks nathj

          unfortunatelly I am not allowed to use a database for this matter, its just some co. policy. So, do you have some code that i can use to solve this. i have looked almost everywhere for help but nothing seems to be helping.

          Please help
          Hi Johnymap,

          Sorry for the delay in geting back to you - been busy with work.

          If you have to use text files and arrays then it would seem that you need to develop a loop through the array. So that when a user selects the item from the dropdown list you loop through the array from start to finish and return the last matching item.

          So if you find a matching itme you store that in a variable and then continue with the loop. By the time the loop completes you will have the latest item for that year in a variable and you can then write that variable as the value to a text box or simply to the screen.

          This of course assumes that the array is in the order oldest to newest.

          I hope this helps, I'm sorry i haven't time at the moment to write any code for you.

          Cheers

          PS It may be easier to change company policy - I know I did that once and life is better for it.

          Comment

          Working...