商城公告功能開發總結

效果以下
php

1.定在頭部css

position: fixed;
        z-index: 999;
        top: 0;
        opacity:1;

2.ajax處理json數據html

// 獲取商城公告
        function getNotice() { // 獲取公告函數
            var res;
            $.ajax({
               type: "POST",
               url: "{sh::U('Store/Mall/ajaxGetNotice',array('mid'=>$mid))}",
               dataType:'json', // 設爲json以後,就可以很好的處理獲取的json數據,json.status
               async: false,
               success: function(json){
                    res = json;  
               }
            });
            return res;
        }

設置dataType:'json'以後,json數據就直接能夠經過json.的方式處理了。jquery

3.最後加載,頁面更好看。ajax

$(document).ready(function(e) { // 主函數
            // 獲取公告
            var action_name = "{sh::ACTION_NAME}"; // 頁面使用thinkphp常量
            var json = getNotice();
            if ( action_name == 'index' && json.status == 1) { // 首頁而且公告存在
                $(".top").css("margin-top", "70px"); // jquery設置css
                $(".main-sidebar").css("top" ,"70px");
                var html = '';
                $.each(json.info, function(i, n){ // n爲文本內容
                    html += "<li><strong>"+n.content+"</strong></li>"
                });
                $(".top-notice").show();
                $('#notice ul').html(""+html);
                $('#notice').unslider();  // 輪播
            }
        });

4.獲取sql語句的thinkphp處理sql

// 獲取公告
    function ajaxGetNotice() {
        if (IS_AJAX) {
            $this->mid;
            // 獲取有效的,且結束時間大於當前時間的,或者日期等於0的公告
            $mallNoticeModel = M('Mall_notice');
            $where['mall_id'] = $this->mid;
            $where['status']  = 1;
            $where['endtime'] = array(array('eq',0),array('gt',time()), 'or') ;
            //SELECT * from sh_mall_notice where mall_id = 9 and status = 1 and (endtime = 0 or endtime>1458354366);
            $notice = $mallNoticeModel->where($where)->order('sort desc')->select();
            if (!empty($notice)) {
                $this->ajaxReturn(array('status'=>'1','info'=>$notice,'msg'=>"獲取成功"),'JSON');
            } else {
                $this->ajaxReturn(array('status'=>'2','info'=>$notice,'msg'=>"公告不存在"),'JSON');
            }
        }
    }

$where['endtime'] = array(array('eq',0),array('gt',time()), 'or') ;
巧妙的處理了這種邏輯關係。thinkphp

相關文章
相關標籤/搜索