PHPCMS custom tags cannot be paged. Solution

Source: Internet
Author: User

PHPCMS provides a lot of labels to facilitate data calling. However, in secondary development, you sometimes have to write your own PHPCMS labels, but the pages cannot be displayed when pages are displayed, I will introduce the correct solution for developing PHPCMS tag paging.

For example, the lists tag of PHPCMS:

The code is as follows: Copy code

{Pc: content action = "lists" catid = "2 & Prime; where =" thumb! = "AND status = 99 & Prime; order =" id DESC "num =" 4 & Prime ;}

With the built-in lists tag, you can easily call the list of a category. In the template, you can directly use {$ pages} to display pages, in/modules/content/classes/content_tag.class.php, a custom PHPCMS list tag is defined, but the custom list tag is called {$ pages}. The pagination is incomplete, so how to paging?

The code is as follows: Copy code
If (isset ($ page )){
$ Str. = '$ pagesize ='. $ num .';';
$ Str. = '$ page = intval ('. $ page .')? Intval ('. $ page.'): 1; if ($ page <= 0) {$ page = 1 ;}';
$ Str. = '$ offset = ($ page-1) * $ pagesize ;';
$ Datas ['limit'] = '$ offset. ",". $ pagesize ';
$ Datas ['action'] = $ action;
$ Str. = '$ '. $ op. '_ total = $ '. $ op. '_ tag-> count ('. self: arr_to_html ($ datas ). ');';
$ Str. = '$ pages = pages ($'. $ op. '_ total, $ page, $ pagesize, $ urlrule );';
}

We need to understand the principles of the PHPCMS tag and understand it. In/libs/classes/template_cache.class.php, the template tag parsing class is defined. The lists tag can be paged and the pages function is called, note that there is a _ tag-> count () method in row 8th. The parameter returned by this method is the first parameter of pages, that is, the total number of pages, and the custom list tag, there is no count method.

In/modules/content/classes/content_tag.class.php, the count method is as follows:

The code is as follows: Copy code

/**
* Paging statistics
* @ Param $ data
*/
Public function count ($ data ){
If ($ data ['action'] = 'lists '){
$ Catid = intval ($ data ['catid']);
If (! $ This-> set_modelid ($ catid) return false;
If (isset ($ data ['where']) {
$ SQL = $ data ['where'];
} Else {
If ($ this-> category [$ catid] ['child ']) {
$ Catids_str = $ this-> category [$ catid] ['arrchildid'];
$ Pos = strpos ($ catids_str, ',') + 1;
$ Catids_str = substr ($ catids_str, $ pos );
$ SQL = "status = 99 AND catid IN ($ catids_str )";
} Else {
$ SQL = "status = 99 AND catid = '$ catid '";
   }
  }
Return $ this-> db-> count ($ SQL );
 }

We can see Row 3. When the tag action is lists, a series of operations are performed to obtain the total number of operations. Therefore, we should add such a judgment for the custom list tag, when $ data ['action'] = 'custom action name', the total number of query conditions is calculated. For example:

The code is as follows: Copy code


Elseif ($ data ['action'] = 'c _ lists '){
List ($ catid, $ language, $ size, $ page) = explode ('-', $ data ['search']);
// $ Catid = intval ($ data ['catid']);
$ Catid = $ catid + 1;
$ Export age_setting = getcache ('language _ setting ', 'commons ');
$ Size_setting = array (
1 => 'unit = "MB" and size <= 10 ',
'Unit = "MB" and size> = 10 and size <= 50 ',
'Unit = "MB" and size> = 50 and size <= 100 ',
'Unit = "MB" and size> = 100 and size <= 100 ',
'Unit = "MB" and size> = 200 and size <= 100 ',
'Unit = "MB" and size> = 500 and size <1000 ',
'Unit = "GB" and size> = 1 and size <= 2 ',
'Unit = "GB" and size> = 2 and size <= 4 ',
'Unit = "GB" and size> = 4 ',
);
If (! $ This-> set_modelid ($ catid) return false;
If (isset ($ data ['where']) {
$ SQL = $ data ['where'];
} Else {
$ Thumb = intval ($ data ['thumb'])? "AND thumb! = ''":'';
If ($ this-> category [$ catid] ['child ']) {
$ Catids_str = $ this-> category [$ catid] ['arrchildid'];
$ Pos = strpos ($ catids_str, ',') + 1;
$ Catids_str = substr ($ catids_str, $ pos );
$ SQL = "status = 99 AND catid IN ($ catids_str)". $ thumb;
} Else {
$ SQL = "status = 99 AND catid = '$ catid'". $ thumb;
  }
If ($ language ){
$ SQL. = "AND language = '". $ export age_setting [$ language]. "'";
  }
If ($ size ){
$ SQL. = "AND". $ size_setting [$ size];
  }
 }
Return $ this-> db-> count ($ SQL );
}

The code is followed by the above.

In this way, the custom list tag of PHPCMS can also be displayed on {$ pages}.

PHPCMS does not modify the custom page format of the program

See source code

The code is as follows: Copy code

{If $ pages}
<Div class = "pages">
<Select name = "select_pages" onchange = "location. href = this. options [this. selectedIndex]. value;">
{Str_replace ("a href", "option value", str_replace (".. "," ", str_replace (" </a "," </option ", str_replace (" a class "," option class ", str_replace (" </span> ", "page </option>", str_replace ("<span>", "<option selected> No.", str_replace (">", "> ", $ pages)} </select >{/ pc}
</Div>
{/If}

Here, the PHP replacement function str_replace is used to replace any default generated code, so that PHPCMS does not modify the custom paging format of the program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.