View Full Version : Flat File Creation Content Sorting and Editing with PHP

03-12-2010, 01:21 AM
Hi All,

I've looked all over the web and can't seem to find the answer anywhere... I can find snippet after snippet, and parts of other scripts that do SOME of this, but not the WAY I need it to.

First, I have to create a NEW flat file each time a form is sent containing a new topic. This file will be the name of the topic plus the extension '.txt' and be created in the current directory. The name will be supplied by a field named 'NewTopic' and will also need to be passed by the form through the sendmail function at the same time the flat files are updated.

The content of the file created must contain the PHP include string which must be concatonated from the segments ( <?php include ' )
plus the name of the topic being created, plus the segment ( .php'; ?> ), all on the same line with NO SPACES added during concatonation, and all 'spaces' contained within the topic name string need to be converted to UNDERSCORES... ( _ ) to prevent javascript errors should a space be encoded as '%20' for any reason.

It would also require a common file (NumberOfTopics.txt) containing only the number of topics in the (Flat File) database. This number must be in a seperate file, because it is used by other functions to determine the number of topics used and to be used by another script to comply with yet another script's access limits. (It's not relevent to this script, but just so you know why there needs to be a seperate COUNT file!)

Therefore, whenever the page containing the form is first loaded, the script will need to access another flat file containing a list of topics, (Topics.txt) listed one per line, and populate a dropdown selector box on the form as it loads.

Before the form is sent, the drop down selector box is queried and the currently selected topic is passed to the post or put command that sends the form via sendmail, while updating the flat file database on one or more of the following conditions.

If a NEW TOPIC is selected, the name of the new topic in the new topic field will be added to the flat file database of topics... (Topics.txt)

Also, simultaneously, the file 'NumberOfTopics.txt' is updated, by incrementing the number in the file by ONE!

Also, the post command to sendmail must include the topic number drawn from the flat file 'NumberOfTopics.txt', incremented by ONE, the name of the new topic from the 'New Topic' field to be added when the user presses the send button, and the contents of the 'Topic' drop down selector field currently selected on the form.

For example, If there are 45 topics in the topic list, the number in the file 'NumberOfTopics.txt' must be the same.

Each time a new topic is added, the file 'NumberOfTopics.txt' must be incremented by ONE and the file 'Topics.txt' must have the new topic name inserted into the file in 'ALPHABETICAL ORDER', which will require that the topics in the file be sorted before writing to update the file.

There is more that needs to be done, but after carefully analyzing the process, it will be much simpler to attack the problem piece meal.

Once the steps listed here are accomplished I can explain the next step in the process.

I am new to this but have read and experimented enough to be able to follow directions, but writing the script is beyond me.

I am using PHP include commands to accomplish most of this, so if you can break it down to seperate sections, that I can digest easily, I would appreciate it... and if you can help, I'll gladly credit you with the help and link to your website if you want.

Thank you for your help.


03-12-2010, 01:33 AM
Just a question out of curiousity...
Is there a reason you are not using MySQL for this project?

03-12-2010, 02:05 AM
Yes, That is because the 'Topics.txt' file is part of another program and I am trying to interface a way to update the topic list using a form on a webpage.

The 'Topics.txt' file is also used by another program which draws it's content based on the list of topics within this file.

The 'NumberOfTopics.txt' file is used by another script to limit the number of topics displayed in the menu displayed on member's custom menu, so that the page itsself doesn't 'explode' into the lower frame.

All of the other programs and scripts use flat file databases, and in order to retain compatibility, my modifications to the webpages must also interact with these flat files.

Thank you for your response, I hope you can help...


03-12-2010, 02:13 AM
Let's break it down into parts ...

First make an HTML form that will be used for a new topic.
Upload that form, or put it on a page and show me that form.

Also, what is your level of PHP experience?
I won't know where the scripts will be in reference to other directories.
Are you able to alter the scripts I make to work with the directories you are using?

03-12-2010, 03:48 AM
Yes, Thank you,

Please look here for the form...


The form will not send yet... I have excluded certain parts to prevent abuse from the general viewing public

I can handle the directory modifications. I am somewhat limited on php, but I think I can follow along... I onlu use the PHP include command with any frequency and success, tho'.

Thanks again for the help.:thumbsup:

03-12-2010, 03:59 AM
Also ...

Is there already an existing file called, NumberOfTopics.txt ?
Can you show me the contents of that?

And also display one of the existing .txt files that you use in your "includes".
I want to see what one of those look like.


Your form is now being sent to a Perl (CGI) formmail script.
You do have PHP installed and working on your webhost account?
Most webhosts now have PHP.

It's bedtime for me now, but I'll do some work on it (I'd like to see some existing files).
Doing this on this forum is awkward.

You can PM me with your real email address ... that might be easier.


03-12-2010, 04:59 AM
Is there already an existing file called, NumberOfTopics.txt ?
Can you show me the contents of that?




And also display one of the existing .txt files that you use in your "includes". I want to see what one of those look like.

The Chat Button is an example of how I use PHP include Statements to include things... this is the only php command I can use effectively, at this time, but I'm always willing to learn something new...

The two files above will show the content and layout

Each of the Topic Files created would look like this...


Your form is now being sent to a Perl (CGI) formmail script.

I will have to work out the sendmail for this form...

You do have PHP installed and working on your webhost account?
Most webhosts now have PHP.

Yes! Php is installed and working

It's bedtime for me now, but I'll do some work on it (I'd like to see some existing files). Doing this on this forum is awkward.

You can PM me with your real email address ... that might be easier.

I have installed a chat button... start there please?

Just refresh the page...

Thank you again.:thumbsup:


03-12-2010, 02:12 PM
On this page:

What is the purpose of "Selecting a Topic"?

And are you currently displaying those selection boxes using PHP?

03-12-2010, 03:33 PM
I PM'd you with a message.

And this page does not exist:


03-12-2010, 04:54 PM
What is the purpose of "Selecting a Topic"?
In this case, Selecting the topic is used to indicate the interest of the member when performing several different tasks.

First, when registering as a new member, they can sign up for one of several newsletters, secondly, they can specify areas of interest or specialty.

The topics are part of a newsletter script, which I can't even begin to understand, nor can I even modify it... But I can manipulate the 'OUTPUT' after the pogram generates a file, and I can change the contents of the files created, in order to change the way the menus and content and its placement is organized.

But I can't seem to get the newsletter script to let me add edit or delete topics without great and complex procedures. There are 7 steps that take hours of reading and locating tiny snippets of code which is so obscure and obfuscated I can't understand how anybody could have written it in the first place. But I have thousands of members using this program and script and it includes over 1000 topics, and their settings, which are 'LOST' in the program due to it's complexity (Beyond Me!)

The simpler way is to do what I am trying to do here, by simply adding, editing or deleting the contents of the flat file output to update and edit the list of topics.

This list is also used by another script to insert links to specific content as predetermined by the member when registering and when selecting topics of interest, for their choice of newsletter(s), and other website content.

And are you currently displaying those selection boxes using PHP?

Yes, but the current output is in the form of 'HyperLinks' which, when the number of links exceeds 20, the list output explodes the upper frame into the lower frame.

Thus, the need to convert the output into a dropdown menu!

I hope his helps... I am not real good at this, and it took me several hours to figure out how to explain what is happening, and why, and then to determine how the form needs to work... As I said previously, there is a little more to it that needs to be done, but in order to make it simple and easy enough for ME to understand, I broke the problem down into 3 parts.

The second part is a script that will insert the content of the Topic File (Advertising.txt) which is the PHP call of Advertising.php, which, in turn, contains the CONTENT to be put on the page beng viewed.

This script will call the content using additional PHP calls (Includes) which can be appended or edited, just like the Topics.txt file.

The third part can wait, but is simply a matter of adding a list of all registered members that have read an article, or viewed a video or other content, for tacking purposes, such as to prevent the same content from appearing more than once...

This is the same as the flat file containing the list of topics, but the member must type their e-Mail address into a field at the bottom of the article or other content, and press the 'I read this' Button to 'VOTE' or Sound Off!

This form would act the same as the 'Add Topic Form' in that it would also trigger an e-Mail to the webmaster (Me!) containing the e-Mail Address of the Member, and any content entered into the Comment (Sound Off!) field and a 1 of 10 radio button selector. for voting.

However, In this case, only the e-Mail address and the 'Vote' content need be 'Appended' to the Flat File.

I have tested your script and it does the first part fine... thank you very much.

I did notice that the list is sorted, but that the topics that are capitolized are listed topmost, but those being lowercase are listed bottommost... can the list be sorted regardless of capitolization?

Also I tried to delete some topics, but the process failed... perhaps because you haven't completed that function, yet?

Again, I do appreciate your help... thanks a zillion!:):):)

03-12-2010, 06:31 PM
If you put a check in the checkbox (to delete),
you still have to click the button (I changed the wording to "Update Topic").

Now, it sorts, but also converts all the first letters of topics to capital letters.
(that's the best I can make it work).

You can also delete a topic and add one at the same time by selecting one
in the top, checking box, and typing a new one in the bottom ... then click button.

Try it now, let me know.

03-12-2010, 09:12 PM
Thank you for all your help, but there is one more small thing about the script... I can get it to work just fine on your test server, but not on mine... Should I CHMOD anything or is there another step I've missed?

Thanks, once again for the great assist! This script will work for the one part I so needed to solve!

Great Job!

03-12-2010, 09:31 PM
You must CHMOD the two text files to 777 (or 0777, depending on your FTP program).

03-12-2010, 10:42 PM
Thanks again, that solved one thing, but for some reason the webpage doesn't refresh the content of the dropdown menu... It DOES update BOTH files, so therefore I believe the CHMODing worked for those files...

Is there something in the script or perhaps the form that needs to be changed to reflect MY server?

03-12-2010, 11:18 PM
It must be a browser refresh issue.
Set your browser settings/options for "automatic cache" or something like that.
To check it, make a change and manually view "Topics.txt" to see your change.
If the change is fine there, but not on the browser, click refresh.

03-13-2010, 12:46 AM
Tried your suggestion... didn't work...

The form ADDS just fine, but does NOT delete topics.

I also note that I can add the same topic twice!

It also does not seem to refresh the dropdown menu.

I cleared the cache, temporary internet files, closed all browsers and rebooted fresh and it remains the same.

Then I looked at the form page and I can't see how the items for the dropdown menu are populated from the Topics.txt file? Did I perhaps get the wrong form page?

Would you please look at the index.php source code and tell me if the dropdown menu is being populated from the Topics.txt file? If it isn't, that might be the problem?

Thanks again

03-13-2010, 01:47 AM
I PM'd the links again.
If the index.txt file opens as a webpage, let me know.
It is supposed to open as text file, but that depends on your browser.