📜 ⬆️ ⬇️

PHP and files. News without MySQL with flipping

The cornerstone for all novice PHP programmers is the organization of the 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 solved in favor of PHP + MySQL = standard. But this problem is frozen in eternity, and still new adepts address this issue and those who already have experience in PHP programming come back.

However, I was prompted to write this note by a two-year-old request from a friend who needed to display news on the site without using a database. Moreover, the man did not even need an administration panel, since the site was located on his home computer. The acquaintance was convinced that it could not be anything easier - open a notebook, write down the necessary text there and send it to the right folder. I could not convince him that people invented a control panel for a reason.

I do not remember how we solved the issue with the site. But now this idea has surfaced in my head. It is even interesting to count and compare the number of operations when adding news to the site through the admin area and through a notebook, taking into account the fact that the server is on the working computer.

Just for fun - honestly looking for the shortest list of operations for each.
')
Classic way
1. Open the browser.
2. Type the address or click on the tab.
3. Optional. enter the password or click confirm.
4. Type the text.
6. Click send.

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

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

But this is something there!

3 operations, maybe 4. No time spent opening a browser, a notepad in Windows opens instantly.

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

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

What would you like from the functional?



What would not want?

So, the implementation:

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

2. Create a file in bd with the name, for example, all.dat - we will be counting the news in it. The presence of this file eliminates the use of the scandir or glob functions to count files. We write in it the number 0.

Admin panel
Create an ad folder in the www directory and put the following script there. I have index.php. Taking into account that this directory will be under the password and is accessible only to one administrator, then some principles of good code can be neglected.

News entry form.
<div align="center"> <form action="index.php" method="post"> <p>New</p> <br/> <textarea name="text1" cols="80" rows="25"></textarea> <p><input name="ok" type="submit" value="send" "/> <input type="reset" value=reset /></p> </form> </div> 


It's simple. We process the form. I make a minimum of conditions. If the text field is not empty, then we get an entry from the file in which we keep records - all.dat. And in it we have 0.
We create a file named 0.txt, check its presence just in case, and write data to it.
  <?php $text1 = $_POST['text1']; if (isset($_POST['ok']) && (!empty($text1))) { $all = file_get_contents('../bd/all.dat'); $file = '../bd/' . $all . '.txt'; if (!file_exists($file)) { $fp = fopen($file, "w"); fclose($fp); $fp = fopen($file, "r+"); fwrite($fp, $text1); } } ?> 


Now check the existence of a new file and increase the counter by 1 in the file all.dat.
 <?php $new = '../bd/' . $all . '.txt'; if (file_exists($new)) { $all+= 1; $fp = fopen('../bd/all.dat', "r+"); fwrite($fp, $all); fclose($fp); } ?> 


To display news on the main page, we will write such a script. Get the number of records from all.dat.
Then it is 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 entry made.
 <?php $p = $_GET['p']; $allcount = file_get_contents('bd/all.dat'); $entry = "bd/{$p}.txt"; if (isset($p) && file_exists($entry)) { $news = file_get_contents($entry); } else { $p = $allcount - 1; $entry = "bd/{$p}.txt"; $news = file_get_contents($entry); } ?> <div class="bform"><div class="headlines"><?=$news?></div></div> <div class="footer "> <a href="index.php">First</a> | <a href="index.php?p=<?=--$p?>">Next</a> </div> 


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

Conclusion: it turned out to get rid of arrays, cycles and functions glob, scandir.
You can also just add and edit to the script in the admin panel. It remains only to organize the 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 prescribe for each file, for example, its own meta tags.
It will be easy to add this option to the admin panel.

Post Scriptum:
The purpose of this note is to draw attention to the overload of professional solutions for news organizations and to find inspiration for writing micro-engines.

Link for inspiration: a case study 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 some animation (CSS3).
It is recommended to open in Opera or Chrome: Link

Fictions
In the slider, you can add an option to adjust the pause. If you calculate the groove using a script and set the time value based on the number of characters (and taking into account the average reading speed of an adult), you can easily get a fully automatic system for speed reading.
All that remains is to add voice recognition and you can move away from the screen.

Source: https://habr.com/ru/post/255957/


All Articles