Hello,
I have been playing with Php for a little bit and am looking for some guidance on how to pursue a certain goal.
I work for a small newspaper and we would like to take the articles (saved as .txt files) and make them searchable on our webiste. (Example: Someone wants to find all the articles in which "Joe Shmo" was refered to)
Starting from the beginning and want to know what is the best method to do this? Php & MySQL? Or is Php going to be enough?
TIA
Newbie looking for path of enlightenment
Moderator: General Moderators
Well, if you just want to use PHP, the easiest way would be to use file_get_contents() to read each text file into a string, then use strpos() on the string to see if the search string is found. This method is cumbersome though, as you have to read in each file for every search. A much better way would be to read and store each file into MySQL. Then a simple query that says:
SELECT * FROM file_table WHERE content_of_file LIKE '%SEARCH_STRING%';
Would return all files that contained your search string.
P.S I absolutely LOVE your signature.
SELECT * FROM file_table WHERE content_of_file LIKE '%SEARCH_STRING%';
Would return all files that contained your search string.
P.S I absolutely LOVE your signature.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
some useful links are :
http://us2.php.net/manual/en/function.fopen.php
http://us2.php.net/manual/en/function.fread.php
http://us2.php.net/manual/en/function.f ... ntents.php
basically, what pickle says will work. however, i'm unsure how the contents of your txt file are working. If you have not only text, but titles, authors, and dates that you would like to seperate, then the above links should give you very good inside.
here is a good method to follow ( assuming that the following is how your txt file is sorted ).
Let's assume you have a file that has 5 parts : one being the title, the second being the author, the third being the date, the fourth being the date, and the 5th being pictures.
you will first want to create a database and name it something ( something that goes along with what you are doing, example : newspaper )
then, you will want to create a table with fields in which each section you want to store will be placed ( this allows more organization ), along with the proper field types ( text, longtext, date, etc... )
Then, you will want to open the file, and read the contents. Each entry you have in the txt file it probably going to have a pattern ( first line is the author, second the date, third the actual write up, etc.. ).
I'm not saying that this is how your is, but you get the picture. just find the pattern.
then, you will want to do a read of the file, and store what you find in that first read to a variable ( or you can just upload it straight to the table ).
then, when u get eol, or whatever distinguishes you are wanting to get the next field, do the same...
repeat, update the table.
then, just call it when you need it.
need anymore help? just ask, and we'll see if we can't point you in the right direction.
http://us2.php.net/manual/en/function.fopen.php
http://us2.php.net/manual/en/function.fread.php
http://us2.php.net/manual/en/function.f ... ntents.php
basically, what pickle says will work. however, i'm unsure how the contents of your txt file are working. If you have not only text, but titles, authors, and dates that you would like to seperate, then the above links should give you very good inside.
here is a good method to follow ( assuming that the following is how your txt file is sorted ).
Let's assume you have a file that has 5 parts : one being the title, the second being the author, the third being the date, the fourth being the date, and the 5th being pictures.
you will first want to create a database and name it something ( something that goes along with what you are doing, example : newspaper )
then, you will want to create a table with fields in which each section you want to store will be placed ( this allows more organization ), along with the proper field types ( text, longtext, date, etc... )
Then, you will want to open the file, and read the contents. Each entry you have in the txt file it probably going to have a pattern ( first line is the author, second the date, third the actual write up, etc.. ).
I'm not saying that this is how your is, but you get the picture. just find the pattern.
then, you will want to do a read of the file, and store what you find in that first read to a variable ( or you can just upload it straight to the table ).
then, when u get eol, or whatever distinguishes you are wanting to get the next field, do the same...
repeat, update the table.
then, just call it when you need it.
need anymore help? just ask, and we'll see if we can't point you in the right direction.
Ok. My understanding was MySQL would make it better. So I will begin to research and learn that side of things. Thanks Pickle.
Infolock,
Currently we don't have a pattern, but to make sure I understand we can save the text files like so:
Title
Author
Date of Publication
Body
Picture (if applicable)
In MySQL enter 5 different fields for the above lines in the text doc.
Each week add the new text docs to MySQL... and then a Php script would do the search and sort on the page for the hits.
Sound about right?
Infolock,
Currently we don't have a pattern, but to make sure I understand we can save the text files like so:
Title
Author
Date of Publication
Body
Picture (if applicable)
In MySQL enter 5 different fields for the above lines in the text doc.
Each week add the new text docs to MySQL... and then a Php script would do the search and sort on the page for the hits.
Sound about right?
well, that's one way..
another is just to make a form on one of your web site's pages where the user just types the required information.. then you can just post whatever they do directly to your database instead of having to go that route. but, yes, you can do it like that... it's just a very very long way of going about it
another is just to make a form on one of your web site's pages where the user just types the required information.. then you can just post whatever they do directly to your database instead of having to go that route. but, yes, you can do it like that... it's just a very very long way of going about it
I installed MySQL last night and played around with the sample table and such. And was able to insert a .txt file into the db.
We probably have 500 individual text files of old articles archived and add probably 30 or so each week.
Would the form be the best way to add the initial files to the db?
And while playing with MySQL the docs say VARCHAR is limited to 255 characters. How do I place a full article (600+ words in a text file) into the database and make it available for searches?
I apologize for such a rudimentary question... I just haven't found the answer in the docs I have searched.
Thanks for your time and patience infolock.
We probably have 500 individual text files of old articles archived and add probably 30 or so each week.
Would the form be the best way to add the initial files to the db?
And while playing with MySQL the docs say VARCHAR is limited to 255 characters. How do I place a full article (600+ words in a text file) into the database and make it available for searches?
I apologize for such a rudimentary question... I just haven't found the answer in the docs I have searched.
Thanks for your time and patience infolock.
http://www.mysql.com/doc/en/Column_types.html should help you choose the correct column types (in your case look at the TEXT types)