A
A
Alexey2017-03-15 19:29:39
PHP
Alexey, 2017-03-15 19:29:39

How to come up with logic for displaying users?

Hello! Please explain, using the example of user output, how to build the application logic so that the code does not repeat as much as possible and how to properly divide it into classes .
- On one of the pages there is a list of users,
- On the other page, my friends are displayed.
- On the third page, users in the group
All three output options are slightly different from each other visually (somewhere there is a button, somewhere there is no login, somewhere there is no avatar)
I can't understand...
It turns out I need three different php a file in which requests to the database with their own conditions will be made. Next, in each we connect one class "showUsers" and do something like:

while($row = $res->fetch_assoc()){
   $showUsers = new showUsers($tpl, $row); // передаем объект шаблонизатора и данные юзера
   $showUsers = setView('friends'); // указываем что выводим для показа на странице "мои друзья"
   $users[] = $showUsers->show(); // формируем html и записываем в массив
}
// дальше собираем все в одну строку и выводим

those. the bottom line is that we have one html formation class with different output options, and getting users from the database in each module is separate? Am I digging correctly?
And if this is the output of comments, the user's avatar name and so on are also displayed there. I can no longer combine with my class? How is this done, where can I see an example?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
MrTimon, 2017-03-15
@dzheka3d

The question is very broad. I can advise you to read about MVC (Model-View-Controller). In short, you need to have a User Model (class), a Model for the Users Collection, Templates for each output template option, and a php file that will send information about users to templates and output the result.
That is, requests come to the controller (index.php for example). In it, parse the url and determine which view (that is, which template) needs to be loaded. Then you create a class object with users and pass the resulting array of users to the view and display the result on the screen.
The principle is well described here. Hope this helps you.

A
Alexey, 2017-03-15
@masterfreelance

Model-View-Controller

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question