phpcms 列表页ajax点击加载更多

1、在phpcms/model创建一个自定义的模型查询product_model.class.php

<?php
defined('IN_PHPCMS') or exit('No permission resources.');

pc_base::load_sys_class('model', '', 0);
class product_model extends model {
    public function __construct() {
        $this->db_config = pc_base::load_config('database');
        $this->db_setting = 'default';
        $this->table_name = 'product';
        parent::__construct();
    }

}
?>

2、在phpcms/modules/content/index.php中添加如下代码

//列表页产品数据点击加载更多
    public function product_getjson() {
        $catid = intval($_GET['catid']);
        $page = intval($_GET['page']);
        $start = $page*6; 
        $this->category = pc_base::load_model('category_model');
        $cate = $this->category->select(array('parentid'=>$catid),"catid,siteid,parentid,child,arrchildid,catname,url,items");
        $cates = '';
        foreach($cate as $k=>$v){
            $cates .=",".$v['arrchildid'];
        }
        $this->product = pc_base::load_model('product_model');
        if($cates){
            $cates=trim($cates,',');
            $where = " catid in ($cates)";
            $data = $this->product->select($where,"catid,title,thumb,url,listorder,status,inputtime","$start,6");
        }else{
            $data = $this->product->select(array('catid'=>$catid),"catid,title,thumb,url,listorder,status,inputtime","$start,6");
        }
        foreach($data as $key=>$val){
            $result[$key] = array(
                'catid' => $val['catid'],
                'title' => $val['title'],
                'thumb' => $val['thumb'],
                'url' => $val['url'],
            );
        }
        echo json_encode(array("data"=>$result)); 
    }

3、模板页调用:phpcms/templates/default/content/list_products.html

{template "content","header"}
    {template "content","header_min"}
    <section class="productTop clearfix">
        <span class="productTopT">{$catname}</span>
        <span class="productTopN">
        {pc:get sql="SELECT * FROM klx_category WHERE catid = $catid  order by catid DESC" return="data" start="0"  }
         {loop $data $r}
            {if $r[parentid]!=0 && $catid!=93}
                {pc:get sql="SELECT sum(items) as sum FROM klx_category WHERE catid = $catid  order by catid DESC" return="data" start="0"  }
                    {loop $data $val}
                       {$val[sum]}
                    {/loop}
                {/pc}
            {else}
                {if $catid==93}
                {pc:get sql="SELECT sum(items) as sum FROM klx_category WHERE catid =93  order by catid DESC" return="data" start="0"  }
                {loop $data $val}
                    {pc:get sql="SELECT sum(items) as sum FROM klx_category WHERE catid in(93,69,70,71,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92)  order by catid DESC" }
                    {loop $data $val}
                       {$val[sum]}
                    {/loop}
                    {/pc}
                {/loop}
                {/pc}
                {/if}
            {/if}
         {/loop}
        {/pc}
        </span>
        <a class="productTopR">分类筛选</a>
    </section>
    <nav class="productNav">
        <ul class="clearfix" id="DataUl">
            {pc:content action="lists" catid="$catid" order="id desc" num="6" page="$page"}
              {loop $data $r}
              <li class="productLi">
                <a href="{$r[url]}" class="productLiA">
                    <div class="productImg">
                        <img src="{$r[thumb]}" />
                    </div>
                    <p>{$r[title]}</p>
                </a>
              </li>
              {/loop}
            {/pc} 
            <div id="moreData"></div>
        </ul>
        <div class="moreWrap active j-productlist-more" data-id="{$catid}" id="moreWrap">
            <span>显示更多</span>
        </div>
    </nav>
    <script type="text/javascript">
    $(function(){
        var length = $("#DataUl li").length;
        if(length<6){
            $("#moreWrap").remove();
        }
        var i=1;
        $(".moreWrap").click(function(){
            var catid=$(this).attr("data-id");
            $.getJSON("/index.php?m=content&c=index&a=product_getjson&catid="+catid+"&page="+i, function(json){
                if(json.data){ 
                    var product_list = '';
                    var _html2 = '';
                    $.each(json.data, function(i, n){
                        _html2 += '<li class="productLi"><a href="'+json.data[i].url +'" class="productLiA">';
                        _html2 += '<div class="productImg"><img src="'+json.data[i].thumb +'" /></div>';
                        _html2 += '<p>'+json.data[i].title +'</p>';
                        _html2 += '</a></li>';
                    });
                    product_list += _html2;
                    $('#moreData').append(product_list);
                }else{
                    $("#moreWrap").show().html("没有更多的数据...");  
                }
            }); 
            i = i + 1;
        });
    });
    </script>
    {template "content","product_left"}
    {template "content","footer_min"}
{template "content","footer"}

 

posted @ 2016-10-08 11:30  _DongGe  阅读(1240)  评论(1编辑  收藏