
Someone will ask how coarse fuel filters in the picture on the right relate to PHP \ IT as a whole? Very simple! The script, which will be discussed further, is very similar both in purpose and in functionality with these devices. Everyone understands that it is not always a trivial task to implement a less versatile “ultrafine” filter (in our case, fulfilling any user wishes). However, to realize something really simple, but at the same time functional and fairly applicable, is quite realistic. What it is, in fact, for "real" and what it is eaten with - look under the cut.
PHP is not my main occupation, but even I have often had to deal with the situation when you need to implement a simple and functional data filter to display certain data to the user. The simplest example is admin panel in almost any system. Yes, it is easier for the developer to go into some PMA and use his hands to pull out any bun for himself, but customers, alas, for the most part are not able and should not.
As a result, I wrote a script, to the input of which in most cases (depending on the type of data, this will be discussed below), it is enough to submit only 2 data arrays of the form:
$FIELDS = array (
'active' => 'checkbox',
'sex' => 'select',
'type' => 'select',
'hp' => 'int',
'ep' => 'int',
'id' => 'int',
'bonus' => 'int',
'description' => 'text'
);
$NAMEFIELDS = array (
'active' => '',
'sex' => '',
'type' => ' ',
'description' => '',
'hp' => '',
'ep' => '',
'bonus' => ''
);
The first array is an enumeration of those fields and types that will be available for filtering, and the second is their symbolic display for the user interface. At the moment, I have implemented 4 types of processing: numeric single (just a number), a choice from a list prepared in advance (type select), textual and boolean (yes / no). Oddly enough, this turned out to be quite sufficient in order to fasten the filter to virtually any table and search it. As I described above, for powerful, complex crosstab searches, other mechanisms are used that will be individual for each system.
')
If the field is a multiple choice (for example, gender is male / female / unisex), the third parameter of the following form is passed to the function:
$additional = array (
'sex' => $SEX,
'type' => $TYPES
);
where $ SEX and $ TYPES are just the same arrays with a possible set of values.
Someone will start screaming why reinventing the wheel with “such-and-such” frameworks, but:
1. I do not like frameworks :) We will not argue why and why, it will go beyond the scope of this post, everything has its own purpose
2. It is always pleasant to do something less convenient for yourself.
An example of the script can be viewed
here (The same, but the PLO
here ). When sending requests, he will simply return the generated request. As you can see, by typing the same criteria (for example, if you select the ID field several times), you can get an even more flexible system for selecting data.
However, I do not pretend to discoveries, etc., I just share what I found convenient and useful for myself. Perhaps, it is useful to someone else. The code, of course, is not sharpened for group development and does not even contain a drop of the OOP, but it is quite understandable, readable, and if you need to fix something \ make your types \ sharpen "for yourself" - you will, I think, with by ease. Although, who needs it - probably did everything for a long time, who has not yet had time - will be able to peep at the mistakes in the author’s logic and not repeat them in his room.
Summing up, I will note
pros- Simply
- Universal enough
- Functional enough
minuses- In this example, a GET request is used, so It is worth considering that the length of the URL should not exceed 2048 characters (no one bothers you to use POST requests, it all depends on the task)
- Cannot make crosstab requests
- Written on the knee :)
PS Download all the source code can be about
here on the PLO
here (taken out of the class only 3 f-tsii, which can be attributed as soon as possible to f-tions of common use, so it’s better to be outside of it)
PPS Only the one who does nothing is not mistaken.