Answer the question
In order to leave comments, you need to log in
How to design a simple forum architecture in PHP?
Hello.
As a test task, I received a task to write a simple forum.
Functional description:
There is no login and registration (messages based on the principle of anonymous comments). The main page - the top block is a list of topics, the middle block is a list of messages in an open topic, the bottom block is a form for sending a message. The message is sent via AJAX. From additional functions: search, statistics of PHP and MySQL work at the bottom of the page, pagination of topics. As a test, it is necessary to fill the database with 150k topics and 600k messages, after which each page of the topic should open no more than 2 seconds.
Tell me how to competently implement all this architecturally, what structure of files and code to choose.
If there are any references in mind - I would be happy to read it.
Thanks in advance!
Answer the question
In order to leave comments, you need to log in
If there are no quality requirements, then one index.php and a couple of files will suffice: you can put a connection to the database and queries to the database in one - such files are also called models, put the page output separately in the other, i.e. html. I don’t know exactly where to put AJAX, you can write directly in html if there is not enough code (usually AJAX is done through framework tools). In this case, you will get that the main file is the controller, the database file is the model, and the html file is the view (template).
Your controller will be able to make queries to the database and display a template with filled data.
1. You check whether something came from the form and what page is requested. $_POST $_GET (I hope you are aware of XSS)
2. If it comes, then write to the database, if there are no requirements for not repeating messages.
3. You pull out the names of topics from the database and fill in the header. Here you will need to pull out only those topics that you need. If there are no requirements, then where id: $id limit 5; $id=rand(1, 150,000).
4. Search. If a string came to search, you need to generate a query with the Like condition.
You can search for text from the beginning of the field:
SELECT Table.* FROM Table WHERE Table.Field Like 'text*'
or in any part of the field - Like '*text*'
5. PHP performance statistics. memory_get_usage() - for the amount of memory used, microtime() - for the script execution time. According to MySQL microtime() - query execution time.
6. Pagination of topics. You count the number of topics, divide the number of topics by the number of topics on one page and, depending on the requested page number, display the requested topics by querying the database where id<:id $id is taken from $_GET.
More or less like this. Perhaps it will slow down the search. In this case, it will be necessary either to change queries to lightweight ones (I'm not good at queries), or memcache, or sphinx. Perhaps something else.
PS
In reality, complex sites are usually written in frameworks. For example Yii.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question