menu

arrow_back How do I create an inbox system in php?

by
1 vote
Hello, I mean without using the framework, where can you store such and display new messages? The first idea I had was a messages table in the database where the text of the message is stored, to whom, from whom, read or not. As a person goes to the site, we check all messages in this table and look for those that are addressed to him personally, then the resulting list checks whether it is read or not, all messages marked as unread we put in the block New messages on the site and all. But I think this system is terribly cumbersome and resource-intensive, are there other ways out?

1 comment

messages
id | text | to_user_id | is_read
select * from messages where to_user_id = :user_id and is_read = :is_read

:user_id - user id
:is_read - whether the message is read or not
Don't forget the foreighn key for user_id.
You can select from a table of 10 million, everything works quickly. If there are problems after 50 million, it's solvable. (Partitioning, partitioning or separate storage for hot data)

3 Answers

by
0 votes
where to store such

in the database

and display new messages?

in the browser

I find such a system terribly cumbersome and resource-intensive, there are other ways out?

If this system seems too complicated for you, try your hand at something else. Learn how to output text to the browser, or make queries to the database.
Over time, you'll gain enough experience to make an inbox system

Today seems to be the day of the genius question.

4 Comments

Such a system does not seem complicated to me, I just asked if there are less resource-intensive systems, because every page load to check all the messages in the database - and there may be thousands - is not the approach of a programmer. If you can't answer the point, why did you have to scribble your answer?
There is a solution to this terribly complicated problem, which I have... I have shown you in my answer.
learn how to query the database, chumazik.
eternal_blue There may be millions of them. The database is doing just fine
di Okay, thank you.
by
0 votes
If you don't want to bother with the database, just create a separate folder for each user and store messages as files inside the folder. One text file - one message.

5 Comments

Nadim Zakirov , no, I can see how to do it on the files.
I mean, it's not the most convenient option with files.... Especially if there aren't many entries, the database can easily handle it.
And so, yes, quite a working version can be done, no one argues)
Kirill Gorelov , in the name of the file, you write the date it was received or sent, as well as any other marks you need. For example, if the message is read or not. You can also store information directly inside the text file already in the form of JSON, but then you have to rewrite the file when adding marks. The option with renaming is easier and more reliable.
Kirill Gorelov the worker yes, but it is necessary to check files after overwriting, otherwise it is possible to lose data. Function file_put_contents() does not guarantee file saving, sometimes it can save empty, deleting everything that was in the text file (this is personal experience), so it is necessary to write a wrapper for it, which will check the result.

P. S. This is not so much for you as a note to the author of the question)
Nadim Zakirov Ahhhhhh, I see. I thought it was me))
Then it is relevant and quite on point.)
Nadim Zakirov And how will you mark a letter read or unread?
What about search? Files are not the most convenient storage structure for such a task....
by
0 votes
We check all messages in this table and look for those that are addressed to him personally, then we check the list to see if they are read, and we put all messages marked as unread into the block New messages on the site
Judging by the question, you do not understand how the database works. Learn how to work properly with the database and your question about resource consumption will disappear by itself.

2 Comments

Judging by your answer, you don't understand anything at all. I have to describe everything for people like you, so you don't understand?
eternal_blue Look, buddy, you're wrong.
You really don't know how it should work and you're snapping at the good advice of experienced comrades. You're still growing and growing before such a system would eat up resources. Such a system any experienced programmer runs without any frameworks for one night, even on a hosting for 30 rubles, or even free. Even if there will be millions of such messages.

Need some real advice? Listen to what other participants are telling you, and in particular Rsa97 . Make a table structure, write there a couple of test messages and try to change the status of your message, for starters.

If you're worried about resources (unlikely though) measure how much memory your script uses, be pleasantly surprised and keep drinking your system.

Ask competent questions, for example: here is my code (you insert your code), why it is slow.

I have to spell out everything for people like you, so you don't get it?

You're the one who came here for advice, so be kind enough to write down your idea so that you can be helped and understood.