PHP and files. News without MySQL with flipping

The cornerstone for all beginner PHP programmers is organizing a site without using a database. I will not write about expediency. There are quite a few working systems, some very attractive. It is believed that the problem of "PHP and files" has long been resolved in favor of PHP + MySQL = standard. But this problem froze in eternity, and all the same, new adherents turn to this question and return those who already have experience in programming in PHP.

However, I was prompted to write this note by a request from a friend two years ago who needed to display news on the site without using a database. Moreover, a person did not even need an administration panel, since the site was located on his home computer. The acquaintance was convinced that nothing could be simpler - open a notebook, write down the necessary text there and send to the desired folder. I could not convince him that it was not for nothing that people came up with the admin panel.

I don’t remember how we solved the issue with the site. But now this idea has surfaced in my head. It’s even interesting to calculate and compare the number of operations when adding news to the site through the admin panel and notepad, given the fact that the server is on a working computer.

Just for fun - honestly looking for the shortest list of operations for everyone.

The classic way
1. Open a browser.
2. Type the address or click on the tab.
3. Optional. enter password or press confirm.
4. Type the text.
6. Click send.

We get 5-6 operations. Time spent opening the browser.

Method with files
1. Open notepad.
2. Type text
3. Save immediately to a folder on the server.

But there is something to it!

3 operations, maybe 4. There is no time spent opening a browser; notepad in Windows opens instantly.

We will not write about Linux / BSD, the vi editor is not for the content manager, although if the server is not on a working computer, it can still be faster - open ssh, create a file using vim or nano. Moreover, in vim, saving and closing a file is perfectly reduced to one command. =)

Now a little practice. This is just the beginning of an attempt to realize the idea of ​​news on files. The task is to write the simplest and most understandable code.

What would you like from the functional?

  • Display news on the main page,
  • Leaf through
  • Add via admin panel

What would not want?
  • Use the scandir or glob function.
  • Use arrays in general.

So, the implementation:

1. Create a folder in the www directory to store the files, I have bd / .

2. Create a file in bd with the name, say, all.dat - in it we will keep track of the news. The presence of this file eliminates the use of scandir or glob functions for counting files. Write the number 0 in it.

Administration panel
Create an ad folder in the www directory and put the following script there. I have index.php. Considering that this directory will be password protected and accessible only to one administrator, here some principles of good code can be neglected.

News entry form.


Everything is simple. We process the form. I make a minimum of conditions. If the text field is not empty, then we get the record from the file in which we record the records - all.dat. And in it we have 0.
We create a file with the name 0.txt, just in case, check for its presence and write the data to it.

Now check the existence of a new file and increase the counter by 1 in the all.dat file.

To display news on the main page, we will write such a script. Get the number of records from all.dat.
Then it’s quite simple if the variable for the page - p - is set and there is such a file, we get the contents.
Otherwise, we get the last record made.

Please note that the entries are upside down, that is, the file with a large number in the name is the latest news on the main one, and decrement is used to rewind.

Conclusion: it turned out to get rid of arrays, loops and functions glob, scandir.
It is just as easy to add deletion and editing to the script in the admin panel. It remains only to organize protection and you can test the script on a free hosting.

Files can be stored not in .txt but .html, which will allow you to register for each file, for example, your meta tags.
It will be easy to add this option to the admin panel.

Post Scriptum:
The purpose of this post is to draw attention to the overloaded professional solutions for news organizations and to find inspiration for writing micromotors.

Link for inspiration: an example of work and a link to an exact copy

Addition: based on this script, you can try to implement an automatic text slider . You can use ajax, I use the pseudo-ajax in the example below and a bit of animation (CSS3).
It is recommended to open it in Opera or Chrome: Link

You can add an option to the pause to the slider. If you calculate the groove using a script and set the time value taking into account the number of characters (and take into account, in addition, the average reading speed of an adult), then a completely automatic system for speed reading can turn out.
It remains to add only voice recognition of the text and you can move away from the screen.

Also popular now: