How to make a counter of views of material on the site
The view counter can be very useful on a wide variety of sites. It allows the visitor to understand how popular the material is and how many people have read it.
The view counter is the counter of requests for this material. The counter of the number of requests can be implemented in different ways, let's consider the simplest way. To implement it, it is enough to follow a few steps:
- Create a new column in the database (in the table that stores the content data) and name it, for example, "requests". Such a column must be of type int, by default the value is 0. To do this, you can use phpMyAdmin ;
- In the function or method responsible for displaying the material, you need to add functionality that will enter the value of the view performed into the database, in the previously created column. But first you need to request the current value, only then add the number 1 (value increment) and save the data;
- Output data on the number of views on the site on the material page or in any other block. To do this, simply request the data from the saved column and display it in the material design template.
Thus, making a view counter of a material is very easy - just follow a few simple steps that were discussed above.
Latest articles
- 03.04.24IT / Уроки PHP Уроки простыми словами. Урок 3. Все операторы PHP с примерами, с выводом работы кода на экран.
- 02.04.24IT / Уроки PHP Уроки простыми словами. Урок 2. Типы данных в PHP с примерами.
- 02.04.24IT / Уроки PHP Уроки простыми словами. Урок 1. Коротко о языке веб-программирования PHP. Основы синтаксиса.
- 09.11.23IT / Database Errors when migrating from MySQL 5.6 to 5.7 and how to fix them - database dump import failed with an error or INSERT does not work. Disabling STRICT_TRANS_TABLES strict mode or using IGNORE
- 08.07.22IT / Misc Convert office files DOC, DOCX, DOCM, RTF to DOCX, DOCM, DOC, RTF, PDF, HTML, XML, TXT formats without loss and markup changes