Good day.

do not judge strictly, fully yet do not understand what, where and why at 100% ...

there is a rather poor script. For example, we have three parts (different files are responsible for everything, generally dregs ...) title breadcrumb content

When you go to the news page, THREE requests are made to pull out this news! As they probably guessed, the title (browser's title page), bread crumbs (breadcrumb), and the news itself (content) were guessed. Naturally, this does not suit me. And I decided that it was easier to jot down a small class and when moving to the news, shove everything as an array into a class property, and according to the script, drag everything out of it.

Those. a class property has, let's say, a conditional "buffer". When entering the page, all the data in the property, and in breadcrumbs and the header, I already pull from it (the properties), avoiding requests.

Is this approach normal? Because nothing else comes to mind is as simple.

UPD

Although I do not care for the cons. But I do not like when they do not write the reason. It was thought that because of my stupid question. But he also suggested that, due to the lack of an example, I was asking about something. Here is an example.

class News { public $newsArray = array(); public function __construct() { if (isset($_GET['id']) && !empty($_GET['id'])) { $this->get($_GET['id']); } } public function get($id) { $query = mysql_query("SELECT * news WHERE id = '".(int)$id."'"); if (mysql_num_rows($query) > 0) { $r = mysql_fetch_array($query); $this->newsArray = $r; return $r; } else return false; } } 

UPD 2 So no one has any idea why it’s so bad and how to avoid three queries and make one if the data is needed in three different files? Explain noob please. After all, not everyone immediately began to write a mega-code.

  • Generally use global variables in this way not zergut - zenith
  • I wondered about this. And what then to do to avoid heaps of requests for pulling the same from the database? - drop_off
  • Well, if you want to get 10 news in one request, then make one request with something like WHERE id> $ min AND id <$ max - zenith
  • not. not 10, but 1 total. When viewing the full news, three requests are made. And to avoid this, I make one request and shove everything about the news in $ newsArray. And in all the files I’m just jerking what I need about the news from $ newsArray that I’m viewing. - drop_off
  • I have no idea how your database is organized, but if you have several fields in the news table and each field is part of the news, then your query is optimal. Only I would organize the news itself as a class and assign values ​​from fields to instance variables. But these are my cockroaches and I will not give them to you :) - zenith

1 answer 1

Use ActiveRecord, slightly modifying it. If you use Model :: find_by_id (looking for a single value by ID), you can assign results to an array with a key identifier if no such key exists, and if there is, return an array element with that key. He made such a bike in a project in which AR was already crookedly used.

  • Thanks, google already. But something suggests that it will be more difficult option above. But the question remains voiced above. What is bad option? It turns out that when a person goes on to read a specific news, then an array with the data of the news is recorded in $ newsArray. And in the code, I already get what I need from $ newsArray in different files. - drop_off