473,513 Members | 2,397 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Read specific line from text file using PHP

10 New Member
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
Jan 11 '08 #1
4 4515
nathj
938 Recognized Expert Contributor
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
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
Jan 11 '08 #2
Johnymap
10 New Member
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
Jan 15 '08 #3
Johnymap
10 New Member
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
Jan 16 '08 #4
nathj
938 Recognized Expert Contributor
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.
Jan 16 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
30958
by: sea | last post by:
I have text files in the following format: 123,34, ,345,890, 123,23 .. .. .. As you can see, the problem is that (1) the commas can occur in
0
1557
by: Majordomo | last post by:
-- >>>> --36742377 **** Command '--36742377' not recognized. >>>> Content-Type: text/plain; charset=us-ascii **** Command 'content-type:' not recognized. >>>> Content-Transfer-Encoding: 7bit **** Command 'content-transfer-encoding:' not recognized. >>>> >>>> something is fool
1
4293
by: Magix | last post by:
Hi, I have these string data: str_data1, str_data2, str_data3, which capture some value after a routine process A. Then I would like to write (append) these 3 string values into a text file each time after routine process A, the text file is named "mytext.dat" in following format with "#####" as separator. The maximum entries of them is...
2
5505
by: RyanS09 | last post by:
Hi- I have read many posts with specific applications of reading in text files into arrays, however I have not been able to successfully modify any for my application. I want to take a text file filled with a tab delimited list of 10 columns (floats) and read it into a 2D array. The length of the columns are all the same, however this will...
2
3366
by: JB | last post by:
I'm trying to read a text file, and store the position of a specified line, and then later seek back to that line and start reading again. I'm using simple code like the following: StreaReader sr = new StreamReader("test.txt"); String s = ""; while (S != null) { S = sr.ReadLine()
9
2968
by: =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post by:
I want to open a text file and format it into a specific line and then apply color to a specific location of the text and then display it in a RichTextBox after all of this is done. I can do all of the above after the file is loaded into the RichTextBox, and I am trying to speed the process up by doing it in a temp file.
11
11351
by: jo3c | last post by:
hi everybody im a newbie in python i need to read line 4 from a header file using linecache will crash my computer due to memory loading, because i am working on 2000 files each is 8mb fileinput don't load the file into memory first how do i use fileinput module to read a specific line from a file? for line in...
0
1380
by: Snoze | last post by:
Hello, I'm trying to make an application with C# that writes and reads some lines of a text file, but I want to read one specific line and save the text in a string variable. And the inverse process, save a string variable in a specific line of the text file. Also i think that it could be done writing in the textfile "first= Here the text of...
5
1637
by: CSharp-Jay | last post by:
So I have been programming in C# on a personal level as of lately, and am curious about something. Lately I have been experimenting around with file create/write, etc. I can open and read the whole file, but how do I read specific words into my program? Like for example, I have an ini file that my program writes to and needs to read from to...
4
3379
by: Keith G Hicks | last post by:
I'm trying to read a text file and alter the contents of specific lines in the file. I know how to use streamreader to read each line of a file. I'm doing that already to get the data into a database. What I need help with is on how to locate a specific line in the file, change it and then save the updated text file. Can anyone help me out or...
0
7269
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7177
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7394
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7559
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
5701
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5100
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3237
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
470
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.