There is a function in the class. When the page is updated, the elements change. I can not understand what exactly is being updated: a table, an array or a cycle, in order to implement the update of elements without rebooting. And how can this be done. Thank you.

Function and Block

protected function getRandomuser() { $randomuser = $this->user->getRandomElement(3); for ($i = 0; $i < count($randomuser); $i++) { $sr["name"] = $randomuser[$i]["login"]; $sr["link"] = $this->config->address."?view=userdata&amp;login=".$sr["name"]; $sr["avatar"] = $this->getAvatar($sr["name"]); $text .= $this->getReplaceTemplate($sr, "userread"); } return $text; } <div class="userread"> <img src="%avatar%" alt="avatar" class="featured" /> <a class="name" href="%link%">%name%</a> </div> 

 <?php $myObj = new Modules(); return $myObj->getRandomuser(); ?> Но вместо элементов выходят ошибка 

Fatal error: “Modules” not found in /home/shamil/domains/mrcloyd.ru/public_html/lib/get_random_user.php in line 3 Fatal error: Class 'Modules' not found in /home/shamil/domains/mrcloyd.ru /public_html/lib/get_random_user.php on line 3

Something not


 <?php $myObj = new Modules(); return $myObj->getRandomuser(); ?> 

Class where function is located. Help to make code.

 <?php abstract class Modules { protected $config; protected $user; protected $message; protected $banner; protected $data; protected $user_info; protected $friend; protected $twitt; public function __construct($db) { session_start(); $this->config = new Config(); $this->user = new User($db); $this->message = new Message(); $this->banner = new Banner($db); $this->friend = new Friend($db); $this->twitt = new Twitt($db); $this->data = $this->secureData($_GET); $this->user_info = $this->getUser(); } public function getRandomuser() { $randomuser = $this->user->getRandomElement(3); foreach ($randomuser as $row) { $sr["name"] = $row["login"]; $sr["link"] = $this->config->address."?view=userdata&amp;login=".$sr["name"]; $sr["avatar"] = $this->getAvatar($sr["name"]); $text .= $this->getReplaceTemplate($sr, "userread"); } $new_sr["items"] = $text; return $this->getReplaceTemplate($new_sr, "userreads"); } } ?> 
  • Use AJAX to update the data and on js / jquery update the data in the element without reloading the page. What have you done in this direction? - DaemonHK
  • Data taken from the cycle? - shamil shakirov
  • Yes, and you can replace the for foreach with foreach , for you don’t need much here - DaemonHK

1 answer 1

Let's understand:

PHP is an interpreted programming language that runs only on the server side.

It works as follows - an HTTP request comes to the server - PHP accepts it, processes it in a certain way (your script is executed) and returns a certain result (if you write websites, it will be HTML code, but you can use the same API for PHP ).

Consequently, the PHP code cannot return the result to you without reloading the page!

But, fortunately, there is AJAX technology that allows you to run server-side scripts (these are not necessarily PHP-scripts ) and return the result dynamically (without reloading the page).

And now we proceed to the solution of your question:

Let you have a PHP script called get_random_user.php with the following contents:

get_random_user.php :

 <?php $class = new Class(); //тот класс, в котором Ваша приведенная выше функция return $class->getRandomuser(); //только ее нужно будет сделать public! 

Now, on the frontend part of your site ( html ), write a JS script that will "replace" the result in the necessary block:

 $.ajax({ method: "POST", url: "/path/to/get_random_user.php" }) .done(function( msg ) { $('.userread').html(msg); }); 

There are several nuances that you should take into account in your program:

  1. With this JS code , the result of executing your PHP script should be in the form of HTML code .
  2. That JS-code that I used uses the jQuery library and its $.ajax , therefore, you will need to connect it on the "client" part.

If you use in your work any framework (for example Laravel or Yii ) that work according to the MVC pattern , then the path to your script ( url: in the Ajax function) can be http://site.local/path/to/controller/action

In general, a good practice in writing websites (and many other applications in other languages) is to use the MVC pattern . Many companies work on this template. And the sooner you master it and start using it, the faster you will grow as a developer.

The advantages of the MVC pattern are discussed in this question.

  • @shamilshakirov so you do not correct the comments, but the question itself, there is a "edit" button - Ep1demic
  • @shamilshakirov is all right, because I wrote you a rough version of the action, and you should already digest what was written and correct for your needs;) - Ep1demic
  • @shamilshakirov make require this class - require 'path/to/class/Modules'; - Ep1demic Nov.
  • @shamilshakirov You don’t understand the primitive, basic language constructs, I recommend that you first read about OOP constructs in PHP, and then write code ... You really cannot instantiate (create an object) an abstract class ... - Ep1demic
  • @shamilshakirov example will not help you, you need a theory, and then go on to examples. Нельзя создать объект класса, объявленного как abstract - Ep1demic