Fight with lists or application of classes for a conclusion of lists in PHP
The list, the list, the list … you Open a file of any Internet - project based on technology php+mysql, and the first, that you see are pieces of a code, about such kind:
$q = ' select id, title from rubrics';
$result = mysql_query ($q) or die (' select query failed ');
print ' <ol> ';
while ($line = mysql_fetch_array ($result, mysql_assoc)) {
print ' <li> <a href = "? id = '. $line [' id ']. ' "> ';
print $line [' title '];
print ' </a> </li> ';
}
print ' </ol> ';
The list of links to separate pages of the unit of the site, stored{kept} in a database, firms in the catalogue, subscribers of dispatch in the interface of administration, statistics of a resource and many other things is in a similar way deduced.
Each time, typing{collecting} a similar code in the php-editor, you see, that again and again you make the same job with the minimal variations. Distinctions only in a code of sql-search, type and quantity{amount} of deduced{removed} fields and in html-tags, i.e. in design. And if you do{make} one job two times it is an occasion not only to start missing, but also to reflect on how her to facilitate. And the design of a site often is a little to change attempts demand updatings this site of a code, so also his subsequent debugging.
During several years of job with php at me the certain standard of a spelling of a code for a conclusion of lists and as a result it wanted to generalize this experience was developed and to make something standard, suitable for any situation connected to a conclusion of results of sql-search as the list by means php.
In result all simple lists have been prepared on six parts (it is their most simple model):
Frame of the list: it inside what the list is, is in him even one element whether or not.
The empty list: if our search does not return any line all the same it is necessary to deduce{remove} some explanatory on a site, for example, „ the list is empty “, or „ the editor in holiday, news any more will not be “, or to leave here empty value.
The beginning of the list: if in the list there is even one element the list can be begun any phrase, for example, „ the beginning of the list “ or to place here heading which in case of the empty list can and not be deduced{removed}.
The end of the list: the previous item{point} - a phrase „ the end of the list “ or any total on all lines which in case of the empty list can and be absent is necessary for the same purposes, as.
Element of the list: actually it also is a conclusion of the data of each line of search; as against the previous items{points} this element is applied to each line of search, deducing her in the same design inside a frame of the list.
Separator of elements of the list: sometimes elements of the list are separated something from each other - points, vertical hyphens, a dash and other, and in the beginning and the end of the list these separators can be absent.
The above-stated structure I name a pattern of the list. It is necessary to write a code of the certain class which, having on an input{entrance} the sql-search, generates the object containing result of performance of this search. In a class the method which on the basis of the pattern developed above will give out to us a required html-code also is required.
It is possible to do without, certainly, and function which will give out required, having received as parameters sql-search and a code of a pattern. But in my opinion with classes and OOP in programming to work where is more tasty{delicious}. In addition I am not going to stop on such simple model of the list, and the further development of a code with classes are supported where easier. And when our providers will replace cursors php from the fourth version with the fifth, classes will enter in php in the same volume, as in c ++.
Without thinking twice, the class has been named listitems; unfamiliar with English I send on multitran.ru. We write his code:
class listitems {
var $query;
var $template;
var $count = 0;
var $items = array ();
var $listitems = ";
function listitems ($query = ") {
$this-> query = $query;
if (! empty ($this-> query)) {
$result = mysql_query ($this-> query)
or die (' sql query is failed ');
while ($tmp = mysql_fetch_object ($result))
$this-> items [] = $tmp;
$this-> count = count ($this-> items);
}
}
}
The designer of a class accepts a code of sql-search as parameter. By the moment of creation of a copy of a class function should be already called you mysql_connect. After that in property items object we shall receive result of performance of search as the numbered file which each element is standard php-object which names of fields correspond{meet} to names of fields of search, and values, naturally, correspond{meet} to the values of fields chosen from a database.
Now it is a little about a pattern of the list. I prepared lists, but have said nothing about their program realization. For the beginning we shall name each item{point} any obscure name in English.
list - a frame of the list
empty - the empty list
begin - the beginning of the list
end - the end of the list
item - an element of the list
delimiter - a separator of elements of the list
Then we shall create a file in which each element of a pattern is indexed by the accepted name. For a spelling of a code of elements of a pattern I use one enough the widespread idea forced by php-programmers (and not only them) for long-term attempts to separate design from a code (this clause{article} also concerns to them). Now there was a certain standard of a spelling of patterns for the php-programs, using so-called macrocodes, or makrosy.
More often the design using angular brackets with any text or digital value inside for the instruction{indication} of that place in a pattern where it is necessary to insert the necessary information is applied. To the same purpose are applied tegi comments html <! --> with text or number which are identified by a php-cursor and are replaced with the required information.
In my opinion, the main lack of such technology consists that if php for any reason has not processed this element of a code in a browser you will not see anything that would specify this mistake as comments html are invisible by definition, and designs with angular brackets look for a browser as unknown to him html-tags which also are not displayed. And if the perfect mistake is invisible, she will repeat and will never be debugged. Besides patterns are complex enough and volumetric under the maintenance{contents} and to develop them it is necessary in any wisiwig-editor. Try to edit invisible tags and comments in him, and at once be convinced, as far as it is inconvenient. Application of square brackets as [name] is not so successful, as even in usual texts they are distributed enough.
In general, for an insert in patterns of the varied information the following design has been thought up: {%% name %%} where name is an identifier of that value with which we want to replace this makros. Such makros it is seen in a conclusion of a browser, in any wisiwig-editor, moreover, many popular html-and php-editors of an initial code can be adjusted to illumination of this design by the color which is distinct from other code as in the standard html and php such sequence of symbols is not used. Besides this makros does not demand for the spelling of special symbols html.
The identifier name should submit to one more rule. As you have already noticed, in a code of a class listitems, the sql-search will be transformed to a file of objects at which names of fields correspond{meet} to names of fields of search, and values - to values. Distributing it is farther, we shall accept, that the identifier name should correspond{meet} to a name of that field which value will replace makros without taking into account the register of symbols. That is, if in your database there is a table which in a field name contains names of the countries of the world, and in a field population - number of their population to deduce{remove} the list of the countries as
THE NAME OF THE COUNTRY - THE POPULATION
The element item should look so:
{%% name %%} - {%% population %%} chel.
To specify, where in a frame of the list the sequence of his elements is deduced, and also their quantity{amount}, in a class listitems additional fields and corresponding to them makrosy are stipulated.
listitems - in this field the html-conclusion on a measure perebora elements of the list collects, and makros {%% listitems %%} should be present necessarily at an element list a pattern as he is replaced with a code of the list.
count is a number of elements of the list, and makros {%% count %%} it is replaced with value of this field.
In view of the aforesaid, the code specifying a pattern, will look so:
$template = array (
' list ' => ' {%% listitems %%} ',
' empty ' => ' <p> " It is the world where there are no countries and people. ',
' begin ' => ' <p> the Population {%% count %%} the countries of the world. <ul> ',
' end ' => ' </ul> ',
' item ' => ' <li> {%% name %%} - {%% population %%} chel. </li> ',
' delimiter ' => ' '
);
Now I shall result a code of a method of object listitems which inserts any object $o into each element of a pattern $t, coded on the rules determined above.
function insertobjecttotemplate ($o, $t) {
foreach ($o as $n => $ v)
if (gettype ($v)! = ' object ' ** gettype ($v)! = ' array ') {
$p = ' {%% '. strtoupper ($n). ' %%} ';
if (eregi ($p, $t)) $t = eregi_replace ($p, strval ($v), $t);
}
return $t;
}
Having changed regular expression in the third line of a code, you can change a kind makrosa according to accepted on your project. For example,
$p = ' ['. strtoupper ($n). '] ';
Will allow to use makros a kind [name], and
$p = ' {[[:space:]] {0}, '. strtoupper ($n.) ' [[:space:]] {0,}} ';
- Same makros with braces and any number probel`nykh symbols between name and brackets. I use in the projects makrosy with an opportunity of addition probel`nykh symbols before and prosle name, and this line looks so:
$p = ' {%% [[:space:]] {0,} '. strtoupper ($n.) ' [[:space:]] {0,} %%} ';
And last function of a class listitems deduces a file of objects items in a pattern from six elements according to all aforesaid.
function getoutput ($arg) {
$list = empty ($arg [' list '])? ": $arg [' list '];
$begin = empty ($arg [' begin '])? ": $arg [' begin '];
$end = empty ($arg [' end '])? ": $arg [' end '];
$item = empty ($arg [' item '])? ": $arg [' item '];
$delimiter = empty ($arg [' delimiter '])? ": $arg [' delimiter '];
$empty = empty ($arg [' empty '])? ": $arg [' empty '];
foreach ($this-> items as $id => $ one) {
$this-> listitems. = $this-> insertobjecttotemplate ($one, $item). $delimiter;
}
$this-> listitems = $this-> count == 0?
$empty:
$begin. substr ($this-> listitems, 0,-strlen ($delimiter)). $end;
$res = $this-> insertobjecttotemplate ($this, $list);
unset ($this-> listitems);
return $this-> insertobjecttotemplate ($this, $res);
}
First six lines of a code are necessary to prevent a conclusion of error messages in case of discrepancy of a pattern $arg to the agreements entered by us. Further on a cycle all elements of a file items get over and are inserted into an element item a pattern with addition of a separator delimiter. Upon termination of a cycle in case the quantity{amount} of elements in items is more than zero listitems it is replaced empty if no to it are added begin and end with rejection of the last delimiter. Further the copy of our class is inserted in list and more time into the received result but already without a field listitems. It is necessary to enable to place a field count in begin, end, item or delimiter, that sometimes it happens it is necessary.
In general, already almost all is said. But as it is necessary in any clause{article} on programming, I shall result one example ready to testing that my reader whom, as well as any programmer, is lazy enough, could estimate this work on the server.
Download a file listitems.rar and unpack it in one of catalogues of your server. The code of a class listitems is in a file cls_lstitems.inc, a test example - in a file test.php, a sql-code for creation of the test table in your database - in a file test.sql. In the second - fifth lines of a file test.php ukazhitete your data for connection to mysql, create the test table of a file test.sql and start a script test.php. Result of his job will be the list of the countries of the world with their population according to all available information from cia world factbook.
Raskommentirujte 20-? a line of a script. Now the element item differs from initial tags b and, accordingly, the population of the country is allocated with a fat font. Raskommentirujte 22-? and 23-? lines, and the list becomes numbered. If in 53 line to replace parameter of function $template on $template1 you receive the list in a tabulared kind. The pattern $template2 allows to deduce{remove} only names of all countries of the world through a point with a blank and a finishing point. If 51-st line of a script to replace on
$list = new listitems (");
That can be seen at how the class listitems processes empty sql-searches.
Certainly, the code of a class listitems is much more complex resulted in the beginning of clause{article} of a cycle. But, entering such class, we have made one very big thing. In this example the program code (class) is completely separated from design (pattern) and contained in the deduced{removed} list information (sql-search). Add to this an opportunity to store{keep} any quantity{amount} of patterns in a database at the expense of their same structure, choosing them by name or to number{room} (identifier) and editing through any interface of administration without necessity of editing a program code. Now it is not necessary to start designers in a program code, it is enough to correct a pattern only. There is no necessity to correct a code and in case of addition of the new data: if you want to add in your list the area of territory of each country it is necessary to add only in the table mysql a new field square, and in a field item a pattern - makros {%% square %%}. All this very strongly facilitates a life to the developer of a cursor of a site and cms (content management system).
In this clause{article} I result the most simple model of a pattern of the list. At present a code of a class listitems, used by me where it is more complex . On a site http://asiadata.ru/ which I support as the web designer, this class deduces all lists, allowing to group them by any quantity{amount} of elements to allocate one or several of them to sort on one or several fields, both in search, and in the file items, and also to place links and many other things. If readers of clause{article} will interest written, write on mailto:direqtor@mail.ru, and I shall continue and I shall develop the subject mentioned here.

|