Answer the question
In order to leave comments, you need to log in
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 и записываем в массив
}
// дальше собираем все в одну строку и выводим
Answer the question
In order to leave comments, you need to log in
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question